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