@@ -1,31 +1,38 | |||||
1 | require 'iconv' |
|
1 | require 'iconv' | |
2 |
|
2 | |||
3 | module Redmine |
|
3 | module Redmine | |
4 | module CodesetUtil |
|
4 | module CodesetUtil | |
5 |
|
5 | |||
6 | def self.replace_invalid_utf8(str) |
|
6 | def self.replace_invalid_utf8(str) | |
7 | return str if str.nil? |
|
7 | return str if str.nil? | |
8 | if str.respond_to?(:force_encoding) |
|
8 | if str.respond_to?(:force_encoding) | |
9 | str.force_encoding('UTF-8') |
|
9 | str.force_encoding('UTF-8') | |
10 | if ! str.valid_encoding? |
|
10 | if ! str.valid_encoding? | |
11 | str = str.encode("US-ASCII", :invalid => :replace, |
|
11 | str = str.encode("US-ASCII", :invalid => :replace, | |
12 | :undef => :replace, :replace => '?').encode("UTF-8") |
|
12 | :undef => :replace, :replace => '?').encode("UTF-8") | |
13 | end |
|
13 | end | |
|
14 | elsif RUBY_PLATFORM == 'java' | |||
|
15 | begin | |||
|
16 | ic = Iconv.new('UTF-8', 'UTF-8') | |||
|
17 | str = ic.iconv(str) | |||
|
18 | rescue | |||
|
19 | str = str.gsub(%r{[^\r\n\t\x20-\x7e]}, '?') | |||
|
20 | end | |||
14 | else |
|
21 | else | |
15 | ic = Iconv.new('UTF-8', 'UTF-8') |
|
22 | ic = Iconv.new('UTF-8', 'UTF-8') | |
16 | txtar = "" |
|
23 | txtar = "" | |
17 | begin |
|
24 | begin | |
18 | txtar += ic.iconv(str) |
|
25 | txtar += ic.iconv(str) | |
19 | rescue Iconv::IllegalSequence |
|
26 | rescue Iconv::IllegalSequence | |
20 | txtar += $!.success |
|
27 | txtar += $!.success | |
21 | str = '?' + $!.failed[1,$!.failed.length] |
|
28 | str = '?' + $!.failed[1,$!.failed.length] | |
22 | retry |
|
29 | retry | |
23 | rescue |
|
30 | rescue | |
24 | txtar += $!.success |
|
31 | txtar += $!.success | |
25 | end |
|
32 | end | |
26 | str = txtar |
|
33 | str = txtar | |
27 | end |
|
34 | end | |
28 | str |
|
35 | str | |
29 | end |
|
36 | end | |
30 | end |
|
37 | end | |
31 | end |
|
38 | end |
General Comments 0
You need to be logged in to leave comments.
Login now