diff options
author | Toshi MARUYAMA <marutosijp2@yahoo.co.jp> | 2011-04-16 06:43:49 +0000 |
---|---|---|
committer | Toshi MARUYAMA <marutosijp2@yahoo.co.jp> | 2011-04-16 06:43:49 +0000 |
commit | a78b12706a46670e3c07f37e379cb67f725038ac (patch) | |
tree | c6d0bf44b7ad58fcfcba6d0fe306ca9f367e2d41 /lib | |
parent | 2507bcd8e7f83de07c6360f41e2fdf9ea5c96db2 (diff) | |
download | redmine-a78b12706a46670e3c07f37e379cb67f725038ac.tar.gz redmine-a78b12706a46670e3c07f37e379cb67f725038ac.zip |
add Redmine::CodesetUtil and move replacing invalid utf8 logic to it.
git-svn-id: svn+ssh://rubyforge.org/var/svn/redmine/trunk@5474 e93f8b46-1217-0410-a6f0-8f06a7374b81
Diffstat (limited to 'lib')
-rw-r--r-- | lib/redmine/codeset_util.rb | 31 |
1 files changed, 31 insertions, 0 deletions
diff --git a/lib/redmine/codeset_util.rb b/lib/redmine/codeset_util.rb new file mode 100644 index 000000000..20156b827 --- /dev/null +++ b/lib/redmine/codeset_util.rb @@ -0,0 +1,31 @@ +require 'iconv' + +module Redmine + module CodesetUtil + + def self.replace_invalid_utf8(str) + return str if str.nil? + if str.respond_to?(:force_encoding) + str.force_encoding('UTF-8') + if ! str.valid_encoding? + str = str.encode("US-ASCII", :invalid => :replace, + :undef => :replace, :replace => '?').encode("UTF-8") + end + else + ic = Iconv.new('UTF-8', 'UTF-8') + txtar = "" + begin + txtar += ic.iconv(str) + rescue Iconv::IllegalSequence + txtar += $!.success + str = '?' + $!.failed[1,$!.failed.length] + retry + rescue + txtar += $!.success + end + str = txtar + end + str + end + end +end |