@@ -1,72 +1,68 | |||||
1 |
|
1 | |||
2 | module Redmine |
|
2 | module Redmine | |
3 | module CodesetUtil |
|
3 | module CodesetUtil | |
4 |
|
4 | |||
5 | def self.replace_invalid_utf8(str) |
|
5 | def self.replace_invalid_utf8(str) | |
6 | return str if str.nil? |
|
6 | return str if str.nil? | |
7 | str.force_encoding('UTF-8') |
|
7 | str.force_encoding('UTF-8') | |
8 | if ! str.valid_encoding? |
|
8 | if ! str.valid_encoding? | |
9 | str = str.encode("US-ASCII", :invalid => :replace, |
|
9 | str = str.encode("US-ASCII", :invalid => :replace, | |
10 | :undef => :replace, :replace => '?').encode("UTF-8") |
|
10 | :undef => :replace, :replace => '?').encode("UTF-8") | |
11 | end |
|
11 | end | |
12 | str |
|
12 | str | |
13 | end |
|
13 | end | |
14 |
|
14 | |||
15 | def self.to_utf8(str, encoding) |
|
15 | def self.to_utf8(str, encoding) | |
16 | return str if str.nil? |
|
16 | return str if str.nil? | |
17 | str.force_encoding("ASCII-8BIT") |
|
17 | str.force_encoding("ASCII-8BIT") | |
18 | if str.empty? |
|
18 | if str.empty? | |
19 | str.force_encoding("UTF-8") |
|
19 | str.force_encoding("UTF-8") | |
20 | return str |
|
20 | return str | |
21 | end |
|
21 | end | |
22 | enc = encoding.blank? ? "UTF-8" : encoding |
|
22 | enc = encoding.blank? ? "UTF-8" : encoding | |
23 | if enc.upcase != "UTF-8" |
|
23 | if enc.upcase != "UTF-8" | |
24 | str.force_encoding(enc) |
|
24 | str.force_encoding(enc) | |
25 | str = str.encode("UTF-8", :invalid => :replace, |
|
25 | str = str.encode("UTF-8", :invalid => :replace, | |
26 | :undef => :replace, :replace => '?') |
|
26 | :undef => :replace, :replace => '?') | |
27 | else |
|
27 | else | |
28 | str.force_encoding("UTF-8") |
|
28 | str = replace_invalid_utf8(str) | |
29 | if ! str.valid_encoding? |
|
|||
30 | str = str.encode("US-ASCII", :invalid => :replace, |
|
|||
31 | :undef => :replace, :replace => '?').encode("UTF-8") |
|
|||
32 | end |
|
|||
33 | end |
|
29 | end | |
34 | str |
|
30 | str | |
35 | end |
|
31 | end | |
36 |
|
32 | |||
37 | def self.to_utf8_by_setting(str) |
|
33 | def self.to_utf8_by_setting(str) | |
38 | return str if str.nil? |
|
34 | return str if str.nil? | |
39 | self.to_utf8_by_setting_internal(str).force_encoding('UTF-8') |
|
35 | self.to_utf8_by_setting_internal(str).force_encoding('UTF-8') | |
40 | end |
|
36 | end | |
41 |
|
37 | |||
42 | def self.to_utf8_by_setting_internal(str) |
|
38 | def self.to_utf8_by_setting_internal(str) | |
43 | return str if str.nil? |
|
39 | return str if str.nil? | |
44 | str.force_encoding('ASCII-8BIT') |
|
40 | str.force_encoding('ASCII-8BIT') | |
45 | return str if str.empty? |
|
41 | return str if str.empty? | |
46 | return str if /\A[\r\n\t\x20-\x7e]*\Z/n.match(str) # for us-ascii |
|
42 | return str if /\A[\r\n\t\x20-\x7e]*\Z/n.match(str) # for us-ascii | |
47 | str.force_encoding('UTF-8') |
|
43 | str.force_encoding('UTF-8') | |
48 | encodings = Setting.repositories_encodings.split(',').collect(&:strip) |
|
44 | encodings = Setting.repositories_encodings.split(',').collect(&:strip) | |
49 | encodings.each do |encoding| |
|
45 | encodings.each do |encoding| | |
50 | begin |
|
46 | begin | |
51 | str.force_encoding(encoding) |
|
47 | str.force_encoding(encoding) | |
52 | utf8 = str.encode('UTF-8') |
|
48 | utf8 = str.encode('UTF-8') | |
53 | return utf8 if utf8.valid_encoding? |
|
49 | return utf8 if utf8.valid_encoding? | |
54 | rescue |
|
50 | rescue | |
55 | # do nothing here and try the next encoding |
|
51 | # do nothing here and try the next encoding | |
56 | end |
|
52 | end | |
57 | end |
|
53 | end | |
58 | self.replace_invalid_utf8(str).force_encoding('UTF-8') |
|
54 | self.replace_invalid_utf8(str).force_encoding('UTF-8') | |
59 | end |
|
55 | end | |
60 |
|
56 | |||
61 | def self.from_utf8(str, encoding) |
|
57 | def self.from_utf8(str, encoding) | |
62 | str ||= '' |
|
58 | str ||= '' | |
63 | str.force_encoding('UTF-8') |
|
59 | str.force_encoding('UTF-8') | |
64 | if encoding.upcase != 'UTF-8' |
|
60 | if encoding.upcase != 'UTF-8' | |
65 | str = str.encode(encoding, :invalid => :replace, |
|
61 | str = str.encode(encoding, :invalid => :replace, | |
66 | :undef => :replace, :replace => '?') |
|
62 | :undef => :replace, :replace => '?') | |
67 | else |
|
63 | else | |
68 | str = self.replace_invalid_utf8(str) |
|
64 | str = self.replace_invalid_utf8(str) | |
69 | end |
|
65 | end | |
70 | end |
|
66 | end | |
71 | end |
|
67 | end | |
72 | end |
|
68 | end |
General Comments 0
You need to be logged in to leave comments.
Login now