summaryrefslogtreecommitdiffstats
path: root/lib/redmine/codeset_util.rb
diff options
context:
space:
mode:
authorToshi MARUYAMA <marutosijp2@yahoo.co.jp>2011-11-17 11:53:15 +0000
committerToshi MARUYAMA <marutosijp2@yahoo.co.jp>2011-11-17 11:53:15 +0000
commit88f8daf8b70295957183080258f7829f8681edca (patch)
tree41ebdb863f90352e154bfa8ea959f443a0d0e90f /lib/redmine/codeset_util.rb
parentfdf6e8547b6b5cb8da4ac0ae6c29377813af80bb (diff)
downloadredmine-88f8daf8b70295957183080258f7829f8681edca.tar.gz
redmine-88f8daf8b70295957183080258f7829f8681edca.zip
move repositories helper to_utf8 logic to lib/redmine/codeset_util.rb for common use (#2371)
git-svn-id: svn+ssh://rubyforge.org/var/svn/redmine/trunk@7825 e93f8b46-1217-0410-a6f0-8f06a7374b81
Diffstat (limited to 'lib/redmine/codeset_util.rb')
-rw-r--r--lib/redmine/codeset_util.rb34
1 files changed, 34 insertions, 0 deletions
diff --git a/lib/redmine/codeset_util.rb b/lib/redmine/codeset_util.rb
index bf24080c6..b74a3a9e7 100644
--- a/lib/redmine/codeset_util.rb
+++ b/lib/redmine/codeset_util.rb
@@ -79,6 +79,40 @@ module Redmine
str
end
+ def self.to_utf8_by_setting(str)
+ return str if str.nil?
+ str = self.to_utf8_by_setting_internal(str)
+ if str.respond_to?(:force_encoding)
+ str.force_encoding('UTF-8')
+ end
+ str
+ end
+
+ def self.to_utf8_by_setting_internal(str)
+ return str if str.nil?
+ if str.respond_to?(:force_encoding)
+ str.force_encoding('ASCII-8BIT')
+ end
+ return str if str.empty?
+ return str if /\A[\r\n\t\x20-\x7e]*\Z/n.match(str) # for us-ascii
+ if str.respond_to?(:force_encoding)
+ str.force_encoding('UTF-8')
+ end
+ encodings = Setting.repositories_encodings.split(',').collect(&:strip)
+ encodings.each do |encoding|
+ begin
+ return Iconv.conv('UTF-8', encoding, str)
+ rescue Iconv::Failure
+ # do nothing here and try the next encoding
+ end
+ end
+ str = self.replace_invalid_utf8(str)
+ if str.respond_to?(:force_encoding)
+ str.force_encoding('UTF-8')
+ end
+ str
+ end
+
def self.from_utf8(str, encoding)
str ||= ''
if str.respond_to?(:force_encoding)