From a78b12706a46670e3c07f37e379cb67f725038ac Mon Sep 17 00:00:00 2001 From: Toshi MARUYAMA Date: Sat, 16 Apr 2011 06:43:49 +0000 Subject: 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 --- lib/redmine/codeset_util.rb | 31 +++++++++++++++++++++++++++++++ 1 file changed, 31 insertions(+) create mode 100644 lib/redmine/codeset_util.rb (limited to 'lib') 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 -- cgit v1.2.3