From ad727d378100bba88ee16df38f534f1d6fd495db Mon Sep 17 00:00:00 2001 From: Jean-Philippe Lang Date: Sat, 1 Jan 2011 19:12:35 +0000 Subject: [PATCH] Fixes utf8 conversions with ruby1.9. git-svn-id: svn+ssh://rubyforge.org/var/svn/redmine/trunk@4605 e93f8b46-1217-0410-a6f0-8f06a7374b81 --- app/helpers/attachments_helper.rb | 8 +++++++- app/helpers/repositories_helper.rb | 8 +++++++- app/models/changeset.rb | 8 +++++++- 3 files changed, 21 insertions(+), 3 deletions(-) diff --git a/app/helpers/attachments_helper.rb b/app/helpers/attachments_helper.rb index 50b44890f..367976515 100644 --- a/app/helpers/attachments_helper.rb +++ b/app/helpers/attachments_helper.rb @@ -29,7 +29,13 @@ module AttachmentsHelper end def to_utf8(str) - 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') + return str if str.valid_encoding? + else + return str if /\A[\r\n\t\x20-\x7e]*\Z/n.match(str) # for us-ascii + end + begin Iconv.conv('UTF-8//IGNORE', 'UTF-8', str + ' ')[0..-3] rescue Iconv::InvalidEncoding diff --git a/app/helpers/repositories_helper.rb b/app/helpers/repositories_helper.rb index 959628735..19a86e00c 100644 --- a/app/helpers/repositories_helper.rb +++ b/app/helpers/repositories_helper.rb @@ -113,7 +113,13 @@ module RepositoriesHelper end def to_utf8(str) - 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') + return str if str.valid_encoding? + else + return str if /\A[\r\n\t\x20-\x7e]*\Z/n.match(str) # for us-ascii + end + @encodings ||= Setting.repositories_encodings.split(',').collect(&:strip) @encodings.each do |encoding| begin diff --git a/app/models/changeset.rb b/app/models/changeset.rb index 804719481..ca4daace8 100644 --- a/app/models/changeset.rb +++ b/app/models/changeset.rb @@ -227,7 +227,13 @@ class Changeset < ActiveRecord::Base end def self.to_utf8(str) - 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') + return str if str.valid_encoding? + else + return str if /\A[\r\n\t\x20-\x7e]*\Z/n.match(str) # for us-ascii + end + encoding = Setting.commit_logs_encoding.to_s.strip unless encoding.blank? || encoding == 'UTF-8' begin