summaryrefslogtreecommitdiffstats
path: root/lib
diff options
context:
space:
mode:
authorToshi MARUYAMA <marutosijp2@yahoo.co.jp>2011-11-14 23:04:45 +0000
committerToshi MARUYAMA <marutosijp2@yahoo.co.jp>2011-11-14 23:04:45 +0000
commit9a93bd1484db1f6bb671f12bf3827a1c504023ca (patch)
tree7fc84823ed049faef6216cad18a97ad5dd335e18 /lib
parent38089d9a56060227d5059c61d64e3a839e741263 (diff)
downloadredmine-9a93bd1484db1f6bb671f12bf3827a1c504023ca.tar.gz
redmine-9a93bd1484db1f6bb671f12bf3827a1c504023ca.zip
move Changeset#to_utf8 logic to lib/redmine/codeset_util.rb for common use (#3261)
git-svn-id: svn+ssh://rubyforge.org/var/svn/redmine/trunk@7810 e93f8b46-1217-0410-a6f0-8f06a7374b81
Diffstat (limited to 'lib')
-rw-r--r--lib/redmine/codeset_util.rb44
1 files changed, 44 insertions, 0 deletions
diff --git a/lib/redmine/codeset_util.rb b/lib/redmine/codeset_util.rb
index 945b3ea7b..5188a355c 100644
--- a/lib/redmine/codeset_util.rb
+++ b/lib/redmine/codeset_util.rb
@@ -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