2012-02-29 01:30:22 +00:00
|
|
|
# -*- encoding : utf-8 -*-
|
2012-03-21 19:55:14 +00:00
|
|
|
require 'charlock_holmes/string'
|
|
|
|
# require 'iconv'
|
|
|
|
|
2012-02-04 20:12:37 +00:00
|
|
|
class String
|
2012-03-21 19:55:14 +00:00
|
|
|
def default_encoding!
|
|
|
|
if ascii_only?
|
|
|
|
force_encoding(Encoding.default_internal || Encoding::UTF_8)
|
|
|
|
else
|
|
|
|
force_encoding((detected = detect_encoding and detected[:encoding]) || Encoding.default_internal || Encoding::UTF_8).encode!
|
|
|
|
end
|
2012-02-04 20:12:37 +00:00
|
|
|
end
|
2012-03-21 19:55:14 +00:00
|
|
|
|
|
|
|
# def enforce_utf8(from = nil)
|
|
|
|
# begin
|
|
|
|
# is_utf8? ? self : ::Iconv.iconv('utf8', from, self).first
|
|
|
|
# rescue
|
|
|
|
# converter = ::Iconv.new('UTF-8//IGNORE//TRANSLIT', 'ASCII//IGNORE//TRANSLIT')
|
|
|
|
# # If Ruby 1.9, else another RubyEngine (ree, Ruby 1.8)
|
|
|
|
# begin
|
|
|
|
# converter.iconv(self).unpack('U*').select{|cp| cp < 127}.pack('U*').force_encoding('utf-8')
|
|
|
|
# rescue
|
|
|
|
# converter.iconv(self).unpack('U*').select{|cp| cp < 127}.pack('U*')
|
|
|
|
# end
|
|
|
|
# end
|
|
|
|
# end
|
2012-02-04 20:12:37 +00:00
|
|
|
end
|