@@ -117,16 +117,15 module RepositoriesHelper | |||||
117 | end |
|
117 | end | |
118 |
|
118 | |||
119 | def to_utf8(str) |
|
119 | def to_utf8(str) | |
120 |
return str if str. |
|
120 | return str if str.nil? | |
121 | if str.respond_to?(:force_encoding) |
|
121 | if str.respond_to?(:force_encoding) | |
122 |
str.force_encoding(' |
|
122 | str.force_encoding('ASCII-8BIT') | |
123 |
e |
|
123 | end | |
124 | # TODO: |
|
124 | return str if str.empty? | |
125 | # Japanese Shift_JIS(CP932) is not compatible with ASCII. |
|
|||
126 | # UTF-7 and Japanese ISO-2022-JP are 7bits clean. |
|
|||
127 |
|
|
125 | return str if /\A[\r\n\t\x20-\x7e]*\Z/n.match(str) # for us-ascii | |
|
126 | if str.respond_to?(:force_encoding) | |||
|
127 | str.force_encoding('UTF-8') | |||
128 | end |
|
128 | end | |
129 |
|
||||
130 | @encodings ||= Setting.repositories_encodings.split(',').collect(&:strip) |
|
129 | @encodings ||= Setting.repositories_encodings.split(',').collect(&:strip) | |
131 | @encodings.each do |encoding| |
|
130 | @encodings.each do |encoding| | |
132 | begin |
|
131 | begin |
@@ -24,7 +24,7 class RepositoryHelperTest < HelperTestCase | |||||
24 | with_settings :repositories_encodings => 'UTF-8,ISO-8859-1' do |
|
24 | with_settings :repositories_encodings => 'UTF-8,ISO-8859-1' do | |
25 | s1 = "Texte encod\xc3\xa9" |
|
25 | s1 = "Texte encod\xc3\xa9" | |
26 | s2 = "Texte encod\xe9" |
|
26 | s2 = "Texte encod\xe9" | |
27 | s3 = s2 |
|
27 | s3 = s2.dup | |
28 | if s1.respond_to?(:force_encoding) |
|
28 | if s1.respond_to?(:force_encoding) | |
29 | s1.force_encoding("UTF-8") |
|
29 | s1.force_encoding("UTF-8") | |
30 | s2.force_encoding("ASCII-8BIT") |
|
30 | s2.force_encoding("ASCII-8BIT") | |
@@ -39,7 +39,7 class RepositoryHelperTest < HelperTestCase | |||||
39 | with_settings :repositories_encodings => 'UTF-8,EUC-JP' do |
|
39 | with_settings :repositories_encodings => 'UTF-8,EUC-JP' do | |
40 | s1 = "\xe3\x83\xac\xe3\x83\x83\xe3\x83\x89\xe3\x83\x9e\xe3\x82\xa4\xe3\x83\xb3" |
|
40 | s1 = "\xe3\x83\xac\xe3\x83\x83\xe3\x83\x89\xe3\x83\x9e\xe3\x82\xa4\xe3\x83\xb3" | |
41 | s2 = "\xa5\xec\xa5\xc3\xa5\xc9\xa5\xde\xa5\xa4\xa5\xf3" |
|
41 | s2 = "\xa5\xec\xa5\xc3\xa5\xc9\xa5\xde\xa5\xa4\xa5\xf3" | |
42 | s3 = s2 |
|
42 | s3 = s2.dup | |
43 | if s1.respond_to?(:force_encoding) |
|
43 | if s1.respond_to?(:force_encoding) | |
44 | s1.force_encoding("UTF-8") |
|
44 | s1.force_encoding("UTF-8") | |
45 | s2.force_encoding("ASCII-8BIT") |
|
45 | s2.force_encoding("ASCII-8BIT") | |
@@ -54,7 +54,7 class RepositoryHelperTest < HelperTestCase | |||||
54 | with_settings :repositories_encodings => 'ISO-8859-1' do |
|
54 | with_settings :repositories_encodings => 'ISO-8859-1' do | |
55 | s1 = "\xc3\x82\xc2\x80" |
|
55 | s1 = "\xc3\x82\xc2\x80" | |
56 | s2 = "\xC2\x80" |
|
56 | s2 = "\xC2\x80" | |
57 | s3 = s2 |
|
57 | s3 = s2.dup | |
58 | if s1.respond_to?(:force_encoding) |
|
58 | if s1.respond_to?(:force_encoding) | |
59 | s1.force_encoding("UTF-8") |
|
59 | s1.force_encoding("UTF-8") | |
60 | s2.force_encoding("ASCII-8BIT") |
|
60 | s2.force_encoding("ASCII-8BIT") | |
@@ -64,5 +64,10 class RepositoryHelperTest < HelperTestCase | |||||
64 | assert_equal s1, to_utf8(s3) |
|
64 | assert_equal s1, to_utf8(s3) | |
65 | end |
|
65 | end | |
66 | end |
|
66 | end | |
|
67 | ||||
|
68 | def test_to_utf8_blank_string | |||
|
69 | assert_equal "", to_utf8("") | |||
|
70 | assert_equal nil, to_utf8(nil) | |||
|
71 | end | |||
67 | end |
|
72 | end | |
68 |
|
73 |
General Comments 0
You need to be logged in to leave comments.
Login now