]> source.dussan.org Git - redmine.git/commitdiff
move Changeset#to_utf8 logic to lib/redmine/codeset_util.rb for common use (#3261)
authorToshi MARUYAMA <marutosijp2@yahoo.co.jp>
Mon, 14 Nov 2011 23:04:45 +0000 (23:04 +0000)
committerToshi MARUYAMA <marutosijp2@yahoo.co.jp>
Mon, 14 Nov 2011 23:04:45 +0000 (23:04 +0000)
git-svn-id: svn+ssh://rubyforge.org/var/svn/redmine/trunk@7810 e93f8b46-1217-0410-a6f0-8f06a7374b81

app/models/changeset.rb
lib/redmine/codeset_util.rb

index 4f08c2e95afe28dcbdb9f17b98152b15be10ae93..e8438f11f6503d991d128df3248ef71acd7280cd 100644 (file)
@@ -264,46 +264,6 @@ class Changeset < ActiveRecord::Base
   end
 
   def self.to_utf8(str, encoding)
-    return str if str.nil?
-    str.force_encoding("ASCII-8BIT") if str.respond_to?(:force_encoding)
-    if str.empty?
-      str.force_encoding("UTF-8") if str.respond_to?(:force_encoding)
-      return str
-    end
-    enc = encoding.blank? ? "UTF-8" : encoding
-    if str.respond_to?(:force_encoding)
-      if enc.upcase != "UTF-8"
-        str.force_encoding(enc)
-        str = str.encode("UTF-8", :invalid => :replace,
-              :undef => :replace, :replace => '?')
-      else
-        str.force_encoding("UTF-8")
-        if ! str.valid_encoding?
-          str = str.encode("US-ASCII", :invalid => :replace,
-                :undef => :replace, :replace => '?').encode("UTF-8")
-        end
-      end
-    elsif RUBY_PLATFORM == 'java'
-      begin
-        ic = Iconv.new('UTF-8', enc)
-        str = ic.iconv(str)
-      rescue
-        str = str.gsub(%r{[^\r\n\t\x20-\x7e]}, '?')
-      end
-    else
-      ic = Iconv.new('UTF-8', enc)
-      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
+    Redmine::CodesetUtil.to_utf8(str, encoding)
   end
 end
index 945b3ea7b1ef20b59aa6310cf75f4b2a3af0f3cf..5188a355ca1a71ebd75dce59947a37c44c492162 100644 (file)
@@ -34,5 +34,49 @@ module Redmine
       end
       str
     end
+
+    def self.to_utf8(str, encoding)
+      return str if str.nil?
+      str.force_encoding("ASCII-8BIT") if str.respond_to?(:force_encoding)
+      if str.empty?
+        str.force_encoding("UTF-8") if str.respond_to?(:force_encoding)
+        return str
+      end
+      enc = encoding.blank? ? "UTF-8" : encoding
+      if str.respond_to?(:force_encoding)
+        if enc.upcase != "UTF-8"
+          str.force_encoding(enc)
+          str = str.encode("UTF-8", :invalid => :replace,
+                :undef => :replace, :replace => '?')
+        else
+          str.force_encoding("UTF-8")
+          if ! str.valid_encoding?
+            str = str.encode("US-ASCII", :invalid => :replace,
+                  :undef => :replace, :replace => '?').encode("UTF-8")
+          end
+        end
+      elsif RUBY_PLATFORM == 'java'
+        begin
+          ic = Iconv.new('UTF-8', enc)
+          str = ic.iconv(str)
+        rescue
+          str = str.gsub(%r{[^\r\n\t\x20-\x7e]}, '?')
+        end
+      else
+        ic = Iconv.new('UTF-8', enc)
+        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