rosa-build/lib/ext/core/string.rb

28 lines
876 B
Ruby
Raw Normal View History

# -*- encoding : utf-8 -*-
require 'charlock_holmes/string'
# require 'iconv'
2012-02-04 20:12:37 +00:00
class String
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
# 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