To check out this repository please hg clone the following URL, or open the URL using EasyMercurial or your preferred Mercurial client.
root / lib / redmine / codeset_util.rb @ 441:cbce1fd3b1b7
History | View | Annotate | Download (744 Bytes)
| 1 | 441:cbce1fd3b1b7 | Chris | require 'iconv'
|
|---|---|---|---|
| 2 | |||
| 3 | module Redmine |
||
| 4 | module CodesetUtil |
||
| 5 | |||
| 6 | def self.replace_invalid_utf8(str) |
||
| 7 | return str if str.nil? |
||
| 8 | if str.respond_to?(:force_encoding) |
||
| 9 | str.force_encoding('UTF-8')
|
||
| 10 | if ! str.valid_encoding?
|
||
| 11 | str = str.encode("US-ASCII", :invalid => :replace, |
||
| 12 | :undef => :replace, :replace => '?').encode("UTF-8") |
||
| 13 | end
|
||
| 14 | else
|
||
| 15 | ic = Iconv.new('UTF-8', 'UTF-8') |
||
| 16 | txtar = ""
|
||
| 17 | begin
|
||
| 18 | txtar += ic.iconv(str) |
||
| 19 | rescue Iconv::IllegalSequence |
||
| 20 | txtar += $!.success
|
||
| 21 | str = '?' + $!.failed[1,$!.failed.length] |
||
| 22 | retry
|
||
| 23 | rescue
|
||
| 24 | txtar += $!.success
|
||
| 25 | end
|
||
| 26 | str = txtar |
||
| 27 | end
|
||
| 28 | str |
||
| 29 | end
|
||
| 30 | end
|
||
| 31 | end |