diff lib/redmine/codeset_util.rb @ 441:cbce1fd3b1b7 redmine-1.2

Update to Redmine 1.2-stable branch (Redmine SVN rev 6000)
author Chris Cannam
date Mon, 06 Jun 2011 14:24:13 +0100
parents
children cbb26bc654de
line wrap: on
line diff
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/lib/redmine/codeset_util.rb	Mon Jun 06 14:24:13 2011 +0100
@@ -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