Mercurial > hg > soundsoftware-site
comparison .svn/pristine/22/229053adf4d0f0cdc30b283c6478a18c47c6fe1d.svn-base @ 1298:4f746d8966dd redmine_2.3_integration
Merge from redmine-2.3 branch to create new branch redmine-2.3-integration
author | Chris Cannam |
---|---|
date | Fri, 14 Jun 2013 09:28:30 +0100 |
parents | 622f24f53b42 |
children |
comparison
equal
deleted
inserted
replaced
1297:0a574315af3e | 1298:4f746d8966dd |
---|---|
1 if RUBY_VERSION < '1.9' | |
2 require 'iconv' | |
3 end | |
4 | |
5 module Redmine | |
6 module CodesetUtil | |
7 | |
8 def self.replace_invalid_utf8(str) | |
9 return str if str.nil? | |
10 if str.respond_to?(:force_encoding) | |
11 str.force_encoding('UTF-8') | |
12 if ! str.valid_encoding? | |
13 str = str.encode("US-ASCII", :invalid => :replace, | |
14 :undef => :replace, :replace => '?').encode("UTF-8") | |
15 end | |
16 elsif RUBY_PLATFORM == 'java' | |
17 begin | |
18 ic = Iconv.new('UTF-8', 'UTF-8') | |
19 str = ic.iconv(str) | |
20 rescue | |
21 str = str.gsub(%r{[^\r\n\t\x20-\x7e]}, '?') | |
22 end | |
23 else | |
24 ic = Iconv.new('UTF-8', 'UTF-8') | |
25 txtar = "" | |
26 begin | |
27 txtar += ic.iconv(str) | |
28 rescue Iconv::IllegalSequence | |
29 txtar += $!.success | |
30 str = '?' + $!.failed[1,$!.failed.length] | |
31 retry | |
32 rescue | |
33 txtar += $!.success | |
34 end | |
35 str = txtar | |
36 end | |
37 str | |
38 end | |
39 | |
40 def self.to_utf8(str, encoding) | |
41 return str if str.nil? | |
42 str.force_encoding("ASCII-8BIT") if str.respond_to?(:force_encoding) | |
43 if str.empty? | |
44 str.force_encoding("UTF-8") if str.respond_to?(:force_encoding) | |
45 return str | |
46 end | |
47 enc = encoding.blank? ? "UTF-8" : encoding | |
48 if str.respond_to?(:force_encoding) | |
49 if enc.upcase != "UTF-8" | |
50 str.force_encoding(enc) | |
51 str = str.encode("UTF-8", :invalid => :replace, | |
52 :undef => :replace, :replace => '?') | |
53 else | |
54 str.force_encoding("UTF-8") | |
55 if ! str.valid_encoding? | |
56 str = str.encode("US-ASCII", :invalid => :replace, | |
57 :undef => :replace, :replace => '?').encode("UTF-8") | |
58 end | |
59 end | |
60 elsif RUBY_PLATFORM == 'java' | |
61 begin | |
62 ic = Iconv.new('UTF-8', enc) | |
63 str = ic.iconv(str) | |
64 rescue | |
65 str = str.gsub(%r{[^\r\n\t\x20-\x7e]}, '?') | |
66 end | |
67 else | |
68 ic = Iconv.new('UTF-8', enc) | |
69 txtar = "" | |
70 begin | |
71 txtar += ic.iconv(str) | |
72 rescue Iconv::IllegalSequence | |
73 txtar += $!.success | |
74 str = '?' + $!.failed[1,$!.failed.length] | |
75 retry | |
76 rescue | |
77 txtar += $!.success | |
78 end | |
79 str = txtar | |
80 end | |
81 str | |
82 end | |
83 | |
84 def self.to_utf8_by_setting(str) | |
85 return str if str.nil? | |
86 str = self.to_utf8_by_setting_internal(str) | |
87 if str.respond_to?(:force_encoding) | |
88 str.force_encoding('UTF-8') | |
89 end | |
90 str | |
91 end | |
92 | |
93 def self.to_utf8_by_setting_internal(str) | |
94 return str if str.nil? | |
95 if str.respond_to?(:force_encoding) | |
96 str.force_encoding('ASCII-8BIT') | |
97 end | |
98 return str if str.empty? | |
99 return str if /\A[\r\n\t\x20-\x7e]*\Z/n.match(str) # for us-ascii | |
100 if str.respond_to?(:force_encoding) | |
101 str.force_encoding('UTF-8') | |
102 end | |
103 encodings = Setting.repositories_encodings.split(',').collect(&:strip) | |
104 encodings.each do |encoding| | |
105 if str.respond_to?(:force_encoding) | |
106 begin | |
107 str.force_encoding(encoding) | |
108 utf8 = str.encode('UTF-8') | |
109 return utf8 if utf8.valid_encoding? | |
110 rescue | |
111 # do nothing here and try the next encoding | |
112 end | |
113 else | |
114 begin | |
115 return Iconv.conv('UTF-8', encoding, str) | |
116 rescue Iconv::Failure | |
117 # do nothing here and try the next encoding | |
118 end | |
119 end | |
120 end | |
121 str = self.replace_invalid_utf8(str) | |
122 if str.respond_to?(:force_encoding) | |
123 str.force_encoding('UTF-8') | |
124 end | |
125 str | |
126 end | |
127 | |
128 def self.from_utf8(str, encoding) | |
129 str ||= '' | |
130 if str.respond_to?(:force_encoding) | |
131 str.force_encoding('UTF-8') | |
132 if encoding.upcase != 'UTF-8' | |
133 str = str.encode(encoding, :invalid => :replace, | |
134 :undef => :replace, :replace => '?') | |
135 else | |
136 str = self.replace_invalid_utf8(str) | |
137 end | |
138 elsif RUBY_PLATFORM == 'java' | |
139 begin | |
140 ic = Iconv.new(encoding, 'UTF-8') | |
141 str = ic.iconv(str) | |
142 rescue | |
143 str = str.gsub(%r{[^\r\n\t\x20-\x7e]}, '?') | |
144 end | |
145 else | |
146 ic = Iconv.new(encoding, 'UTF-8') | |
147 txtar = "" | |
148 begin | |
149 txtar += ic.iconv(str) | |
150 rescue Iconv::IllegalSequence | |
151 txtar += $!.success | |
152 str = '?' + $!.failed[1, $!.failed.length] | |
153 retry | |
154 rescue | |
155 txtar += $!.success | |
156 end | |
157 str = txtar | |
158 end | |
159 end | |
160 end | |
161 end |