end
def to_utf8(str)
- return str if str.blank?
+ return str if str.nil?
+ if str.respond_to?(:force_encoding)
+ str.force_encoding('ASCII-8BIT')
+ end
+ return str if str.empty?
+ return str if /\A[\r\n\t\x20-\x7e]*\Z/n.match(str) # for us-ascii
if str.respond_to?(:force_encoding)
str.force_encoding('UTF-8')
- else
- # TODO:
- # Japanese Shift_JIS(CP932) is not compatible with ASCII.
- # UTF-7 and Japanese ISO-2022-JP are 7bits clean.
- return str if /\A[\r\n\t\x20-\x7e]*\Z/n.match(str) # for us-ascii
end
-
@encodings ||= Setting.repositories_encodings.split(',').collect(&:strip)
@encodings.each do |encoding|
begin
with_settings :repositories_encodings => 'UTF-8,ISO-8859-1' do
s1 = "Texte encod\xc3\xa9"
s2 = "Texte encod\xe9"
- s3 = s2
+ s3 = s2.dup
if s1.respond_to?(:force_encoding)
s1.force_encoding("UTF-8")
s2.force_encoding("ASCII-8BIT")
with_settings :repositories_encodings => 'UTF-8,EUC-JP' do
s1 = "\xe3\x83\xac\xe3\x83\x83\xe3\x83\x89\xe3\x83\x9e\xe3\x82\xa4\xe3\x83\xb3"
s2 = "\xa5\xec\xa5\xc3\xa5\xc9\xa5\xde\xa5\xa4\xa5\xf3"
- s3 = s2
+ s3 = s2.dup
if s1.respond_to?(:force_encoding)
s1.force_encoding("UTF-8")
s2.force_encoding("ASCII-8BIT")
with_settings :repositories_encodings => 'ISO-8859-1' do
s1 = "\xc3\x82\xc2\x80"
s2 = "\xC2\x80"
- s3 = s2
+ s3 = s2.dup
if s1.respond_to?(:force_encoding)
s1.force_encoding("UTF-8")
s2.force_encoding("ASCII-8BIT")
assert_equal s1, to_utf8(s3)
end
end
+
+ def test_to_utf8_blank_string
+ assert_equal "", to_utf8("")
+ assert_equal nil, to_utf8(nil)
+ end
end