Use #b shortcut instead of #force_encoding (#31059).

Patch by Pavel Rosický.


git-svn-id: http://svn.redmine.org/redmine/trunk@17992 e93f8b46-1217-0410-a6f0-8f06a7374b81
This commit is contained in:
Go MAEDA
2019-03-20 01:36:44 +00:00
parent 26509e7f4c
commit 606e75d12d
14 changed files with 32 additions and 40 deletions

View File

@@ -43,10 +43,8 @@ class MailHandler < ActionMailer::Base
options[:no_notification] = (options[:no_notification].to_s == '1') options[:no_notification] = (options[:no_notification].to_s == '1')
options[:no_permission_check] = (options[:no_permission_check].to_s == '1') options[:no_permission_check] = (options[:no_permission_check].to_s == '1')
raw_mail.force_encoding('ASCII-8BIT')
ActiveSupport::Notifications.instrument("receive.action_mailer") do |payload| ActiveSupport::Notifications.instrument("receive.action_mailer") do |payload|
mail = Mail.new(raw_mail) mail = Mail.new(raw_mail.b)
set_payload_for_mail(payload, mail) set_payload_for_mail(payload, mail)
new.receive(mail, options) new.receive(mail, options)
end end

View File

@@ -16,8 +16,7 @@ module Redmine
def self.to_utf8(str, encoding) def self.to_utf8(str, encoding)
return if str.nil? return if str.nil?
str = str.dup str = str.b
str.force_encoding("ASCII-8BIT")
if str.empty? if str.empty?
str.force_encoding("UTF-8") str.force_encoding("UTF-8")
return str return str
@@ -41,8 +40,7 @@ module Redmine
def self.to_utf8_by_setting_internal(str) def self.to_utf8_by_setting_internal(str)
return if str.nil? return if str.nil?
str = str.dup str = str.b
str.force_encoding('ASCII-8BIT')
return str if str.empty? return str if str.empty?
return str if /\A[\r\n\t\x20-\x7e]*\Z/n.match(str) # for us-ascii return str if /\A[\r\n\t\x20-\x7e]*\Z/n.match(str) # for us-ascii
str.force_encoding('UTF-8') str.force_encoding('UTF-8')

View File

@@ -134,9 +134,7 @@ module Redmine
class RDMPdfEncoding class RDMPdfEncoding
def self.rdm_from_utf8(txt, encoding) def self.rdm_from_utf8(txt, encoding)
txt ||= '' txt ||= ''
txt = Redmine::CodesetUtil.from_utf8(txt, encoding) Redmine::CodesetUtil.from_utf8(txt, encoding).b
txt.force_encoding('ASCII-8BIT')
txt
end end
def self.attach(attachments, filename, encoding) def self.attach(attachments, filename, encoding)

View File

@@ -45,7 +45,7 @@ module Redmine
end end
def scm_command_version def scm_command_version
scm_version = scm_version_from_command_line.dup.force_encoding('ASCII-8BIT') scm_version = scm_version_from_command_line.b
if m = scm_version.match(%r{\A(.*?)((\d+\.)+\d+)}) if m = scm_version.match(%r{\A(.*?)((\d+\.)+\d+)})
m[2].scan(%r{\d+}).collect(&:to_i) m[2].scan(%r{\d+}).collect(&:to_i)
end end
@@ -98,8 +98,7 @@ module Redmine
scm_cmd(*cmd_args) do |io| scm_cmd(*cmd_args) do |io|
prefix_utf8 = "#{url}/#{path}".tr('\\', '/') prefix_utf8 = "#{url}/#{path}".tr('\\', '/')
logger.debug "PREFIX: #{prefix_utf8}" logger.debug "PREFIX: #{prefix_utf8}"
prefix = scm_iconv(@path_encoding, 'UTF-8', prefix_utf8) prefix = scm_iconv(@path_encoding, 'UTF-8', prefix_utf8).b
prefix.force_encoding('ASCII-8BIT')
re = %r{^V\s+(#{Regexp.escape(prefix)})?(\/?)([^\/]+)(\/?)\s+(\S+)\r?$} re = %r{^V\s+(#{Regexp.escape(prefix)})?(\/?)([^\/]+)(\/?)\s+(\S+)\r?$}
io.each_line do |line| io.each_line do |line|
next unless line =~ re next unless line =~ re

View File

@@ -45,7 +45,7 @@ module Redmine
end end
def scm_command_version def scm_command_version
scm_version = scm_version_from_command_line.dup.force_encoding('ASCII-8BIT') scm_version = scm_version_from_command_line.b
if m = scm_version.match(%r{\A(.*?)((\d+\.)+\d+)}m) if m = scm_version.match(%r{\A(.*?)((\d+\.)+\d+)}m)
m[2].scan(%r{\d+}).collect(&:to_i) m[2].scan(%r{\d+}).collect(&:to_i)
end end
@@ -93,7 +93,7 @@ module Redmine
def entries(path=nil, identifier=nil, options={}) def entries(path=nil, identifier=nil, options={})
logger.debug "<cvs> entries '#{path}' with identifier '#{identifier}'" logger.debug "<cvs> entries '#{path}' with identifier '#{identifier}'"
path_locale = scm_iconv(@path_encoding, 'UTF-8', path) path_locale = scm_iconv(@path_encoding, 'UTF-8', path)
path_locale = path_locale.dup.force_encoding("ASCII-8BIT") path_locale = path_locale.b
entries = Entries.new entries = Entries.new
cmd_args = %w|-q rls -e| cmd_args = %w|-q rls -e|
cmd_args << "-D" << time_to_cvstime_rlog(identifier) if identifier cmd_args << "-D" << time_to_cvstime_rlog(identifier) if identifier

View File

@@ -49,7 +49,7 @@ module Redmine
end end
def scm_command_version def scm_command_version
scm_version = scm_version_from_command_line.dup.force_encoding('ASCII-8BIT') scm_version = scm_version_from_command_line.b
if m = scm_version.match(%r{\A(.*?)((\d+\.)+\d+)}) if m = scm_version.match(%r{\A(.*?)((\d+\.)+\d+)})
m[2].scan(%r{\d+}).collect(&:to_i) m[2].scan(%r{\d+}).collect(&:to_i)
end end

View File

@@ -58,7 +58,7 @@ module Redmine
# The hg version is expressed either as a # The hg version is expressed either as a
# release number (eg 0.9.5 or 1.0) or as a revision # release number (eg 0.9.5 or 1.0) or as a revision
# id composed of 12 hexa characters. # id composed of 12 hexa characters.
theversion = hgversion_from_command_line.dup.force_encoding('ASCII-8BIT') theversion = hgversion_from_command_line.b
if m = theversion.match(%r{\A(.*?)((\d+\.)+\d+)}) if m = theversion.match(%r{\A(.*?)((\d+\.)+\d+)})
m[2].scan(%r{\d+}).collect(&:to_i) m[2].scan(%r{\d+}).collect(&:to_i)
end end
@@ -270,8 +270,7 @@ module Redmine
blame = Annotate.new blame = Annotate.new
hg 'rhannotate', '-ncu', "-r#{CGI.escape(hgrev(identifier))}", '--', hgtarget(p) do |io| hg 'rhannotate', '-ncu', "-r#{CGI.escape(hgrev(identifier))}", '--', hgtarget(p) do |io|
io.each_line do |line| io.each_line do |line|
line.force_encoding('ASCII-8BIT') next unless line.b =~ %r{^([^:]+)\s(\d+)\s([0-9a-f]+):\s(.*)$}
next unless line =~ %r{^([^:]+)\s(\d+)\s([0-9a-f]+):\s(.*)$}
r = Revision.new(:author => $1.strip, :revision => $2, :scmid => $3, r = Revision.new(:author => $1.strip, :revision => $2, :scmid => $3,
:identifier => $3) :identifier => $3)
blame.add_line($4.rstrip, r) blame.add_line($4.rstrip, r)

View File

@@ -48,7 +48,7 @@ module Redmine
end end
def svn_binary_version def svn_binary_version
scm_version = scm_version_from_command_line.dup.force_encoding('ASCII-8BIT') scm_version = scm_version_from_command_line.b
if m = scm_version.match(%r{\A(.*?)((\d+\.)+\d+)}) if m = scm_version.match(%r{\A(.*?)((\d+\.)+\d+)})
m[2].scan(%r{\d+}).collect(&:to_i) m[2].scan(%r{\d+}).collect(&:to_i)
end end

View File

@@ -448,7 +448,7 @@ class ChangesetTest < ActiveSupport::TestCase
def test_comments_should_be_converted_to_utf8 def test_comments_should_be_converted_to_utf8
proj = Project.find(3) proj = Project.find(3)
str = (+"Texte encod\xe9 en ISO-8859-1.").force_encoding("ASCII-8BIT") str = "Texte encod\xe9 en ISO-8859-1.".b
r = Repository::Bazaar.create!( r = Repository::Bazaar.create!(
:project => proj, :project => proj,
:url => '/tmp/test/bazaar', :url => '/tmp/test/bazaar',
@@ -465,7 +465,7 @@ class ChangesetTest < ActiveSupport::TestCase
def test_invalid_utf8_sequences_in_comments_should_be_replaced_latin1 def test_invalid_utf8_sequences_in_comments_should_be_replaced_latin1
proj = Project.find(3) proj = Project.find(3)
str2 = (+"\xe9a\xe9b\xe9c\xe9d\xe9e test").force_encoding("ASCII-8BIT") str2 = "\xe9a\xe9b\xe9c\xe9d\xe9e test".b
r = Repository::Bazaar.create!( r = Repository::Bazaar.create!(
:project => proj, :project => proj,
:url => '/tmp/test/bazaar', :url => '/tmp/test/bazaar',
@@ -484,7 +484,7 @@ class ChangesetTest < ActiveSupport::TestCase
def test_invalid_utf8_sequences_in_comments_should_be_replaced_ja_jis def test_invalid_utf8_sequences_in_comments_should_be_replaced_ja_jis
proj = Project.find(3) proj = Project.find(3)
str = (+"test\xb5\xfetest\xb5\xfe").force_encoding('ASCII-8BIT') str = "test\xb5\xfetest\xb5\xfe".b
r = Repository::Bazaar.create!( r = Repository::Bazaar.create!(
:project => proj, :project => proj,
:url => '/tmp/test/bazaar', :url => '/tmp/test/bazaar',
@@ -504,8 +504,8 @@ class ChangesetTest < ActiveSupport::TestCase
s2 = +"\xc3\x82\xc2\x80" s2 = +"\xc3\x82\xc2\x80"
s4 = s2.dup s4 = s2.dup
s3 = s1.dup s3 = s1.dup
s1.force_encoding('ASCII-8BIT') s1 = s1.b
s2.force_encoding('ASCII-8BIT') s2 = s2.b
s3.force_encoding('ISO-8859-1') s3.force_encoding('ISO-8859-1')
s4.force_encoding('UTF-8') s4.force_encoding('UTF-8')
assert_equal s3.encode('UTF-8'), s4 assert_equal s3.encode('UTF-8'), s4
@@ -526,7 +526,7 @@ class ChangesetTest < ActiveSupport::TestCase
def test_invalid_utf8_sequences_in_paths_should_be_replaced def test_invalid_utf8_sequences_in_paths_should_be_replaced
proj = Project.find(3) proj = Project.find(3)
str2 = (+"\xe9a\xe9b\xe9c\xe9d\xe9e test").force_encoding("ASCII-8BIT") str2 = "\xe9a\xe9b\xe9c\xe9d\xe9e test".b
r = Repository::Bazaar.create!( r = Repository::Bazaar.create!(
:project => proj, :project => proj,
:url => '/tmp/test/bazaar', :url => '/tmp/test/bazaar',

View File

@@ -107,7 +107,7 @@ class CustomFieldTest < ActiveSupport::TestCase
def test_possible_values_should_return_utf8_encoded_strings def test_possible_values_should_return_utf8_encoded_strings
field = CustomField.new field = CustomField.new
s = (+"Value").force_encoding('BINARY') s = "Value".b
field.possible_values = s field.possible_values = s
assert_equal [s], field.possible_values assert_equal [s], field.possible_values
assert_equal 'UTF-8', field.possible_values.first.encoding.name assert_equal 'UTF-8', field.possible_values.first.encoding.name

View File

@@ -24,7 +24,7 @@ class Redmine::CodesetUtilTest < ActiveSupport::TestCase
def test_to_utf8_by_setting_from_latin1 def test_to_utf8_by_setting_from_latin1
with_settings :repositories_encodings => 'UTF-8,ISO-8859-1' do with_settings :repositories_encodings => 'UTF-8,ISO-8859-1' do
s1 = 'Texte encodé' s1 = 'Texte encodé'
s2 = (+"Texte encod\xe9").force_encoding("ASCII-8BIT") s2 = "Texte encod\xe9".b
s3 = s2.dup.force_encoding("UTF-8") s3 = s2.dup.force_encoding("UTF-8")
assert_equal s1, Redmine::CodesetUtil.to_utf8_by_setting(s2) assert_equal s1, Redmine::CodesetUtil.to_utf8_by_setting(s2)
assert_equal s1, Redmine::CodesetUtil.to_utf8_by_setting(s3) assert_equal s1, Redmine::CodesetUtil.to_utf8_by_setting(s3)
@@ -34,7 +34,7 @@ class Redmine::CodesetUtilTest < ActiveSupport::TestCase
def test_to_utf8_by_setting_from_euc_jp def test_to_utf8_by_setting_from_euc_jp
with_settings :repositories_encodings => 'UTF-8,EUC-JP' do with_settings :repositories_encodings => 'UTF-8,EUC-JP' do
s1 = 'レッドマイン' s1 = 'レッドマイン'
s2 = (+"\xa5\xec\xa5\xc3\xa5\xc9\xa5\xde\xa5\xa4\xa5\xf3").force_encoding("ASCII-8BIT") s2 = "\xa5\xec\xa5\xc3\xa5\xc9\xa5\xde\xa5\xa4\xa5\xf3".b
s3 = s2.dup.force_encoding("UTF-8") s3 = s2.dup.force_encoding("UTF-8")
assert_equal s1, Redmine::CodesetUtil.to_utf8_by_setting(s2) assert_equal s1, Redmine::CodesetUtil.to_utf8_by_setting(s2)
assert_equal s1, Redmine::CodesetUtil.to_utf8_by_setting(s3) assert_equal s1, Redmine::CodesetUtil.to_utf8_by_setting(s3)
@@ -44,7 +44,7 @@ class Redmine::CodesetUtilTest < ActiveSupport::TestCase
def test_to_utf8_by_setting_should_be_converted_all_latin1 def test_to_utf8_by_setting_should_be_converted_all_latin1
with_settings :repositories_encodings => 'ISO-8859-1' do with_settings :repositories_encodings => 'ISO-8859-1' do
s1 = "Â\u0080" s1 = "Â\u0080"
s2 = (+"\xC2\x80").force_encoding("ASCII-8BIT") s2 = "\xC2\x80".b
s3 = s2.dup.force_encoding("UTF-8") s3 = s2.dup.force_encoding("UTF-8")
assert_equal s1, Redmine::CodesetUtil.to_utf8_by_setting(s2) assert_equal s1, Redmine::CodesetUtil.to_utf8_by_setting(s2)
assert_equal s1, Redmine::CodesetUtil.to_utf8_by_setting(s3) assert_equal s1, Redmine::CodesetUtil.to_utf8_by_setting(s3)
@@ -69,7 +69,7 @@ class Redmine::CodesetUtilTest < ActiveSupport::TestCase
def test_to_utf8_by_setting_invalid_utf8_sequences_should_be_stripped def test_to_utf8_by_setting_invalid_utf8_sequences_should_be_stripped
with_settings :repositories_encodings => '' do with_settings :repositories_encodings => '' do
s1 = (+"Texte encod\xe9 en ISO-8859-1.").force_encoding("ASCII-8BIT") s1 = "Texte encod\xe9 en ISO-8859-1.".b
str = Redmine::CodesetUtil.to_utf8_by_setting(s1) str = Redmine::CodesetUtil.to_utf8_by_setting(s1)
assert str.valid_encoding? assert str.valid_encoding?
assert_equal "UTF-8", str.encoding.to_s assert_equal "UTF-8", str.encoding.to_s
@@ -79,7 +79,7 @@ class Redmine::CodesetUtilTest < ActiveSupport::TestCase
def test_to_utf8_by_setting_invalid_utf8_sequences_should_be_stripped_ja_jis def test_to_utf8_by_setting_invalid_utf8_sequences_should_be_stripped_ja_jis
with_settings :repositories_encodings => 'ISO-2022-JP' do with_settings :repositories_encodings => 'ISO-2022-JP' do
s1 = (+"test\xb5\xfetest\xb5\xfe").force_encoding("ASCII-8BIT") s1 = "test\xb5\xfetest\xb5\xfe".b
str = Redmine::CodesetUtil.to_utf8_by_setting(s1) str = Redmine::CodesetUtil.to_utf8_by_setting(s1)
assert str.valid_encoding? assert str.valid_encoding?
assert_equal "UTF-8", str.encoding.to_s assert_equal "UTF-8", str.encoding.to_s

View File

@@ -36,9 +36,9 @@ class PdfTest < ActiveSupport::TestCase
txt_1 = Redmine::Export::PDF::RDMPdfEncoding::rdm_from_utf8(utf8_txt_1, encoding) txt_1 = Redmine::Export::PDF::RDMPdfEncoding::rdm_from_utf8(utf8_txt_1, encoding)
txt_2 = Redmine::Export::PDF::RDMPdfEncoding::rdm_from_utf8(utf8_txt_2, encoding) txt_2 = Redmine::Export::PDF::RDMPdfEncoding::rdm_from_utf8(utf8_txt_2, encoding)
txt_3 = Redmine::Export::PDF::RDMPdfEncoding::rdm_from_utf8(utf8_txt_3, encoding) txt_3 = Redmine::Export::PDF::RDMPdfEncoding::rdm_from_utf8(utf8_txt_3, encoding)
assert_equal (+"?\x91\xd4").force_encoding("ASCII-8BIT"), txt_1 assert_equal "?\x91\xd4".b, txt_1
assert_equal (+"?\x91\xd4?").force_encoding("ASCII-8BIT"), txt_2 assert_equal "?\x91\xd4?".b, txt_2
assert_equal (+"??\x91\xd4?").force_encoding("ASCII-8BIT"), txt_3 assert_equal "??\x91\xd4?".b, txt_3
assert_equal "ASCII-8BIT", txt_1.encoding.to_s assert_equal "ASCII-8BIT", txt_1.encoding.to_s
assert_equal "ASCII-8BIT", txt_2.encoding.to_s assert_equal "ASCII-8BIT", txt_2.encoding.to_s
assert_equal "ASCII-8BIT", txt_3.encoding.to_s assert_equal "ASCII-8BIT", txt_3.encoding.to_s
@@ -47,7 +47,7 @@ class PdfTest < ActiveSupport::TestCase
def test_rdm_pdf_iconv_invalid_utf8_should_be_replaced_en def test_rdm_pdf_iconv_invalid_utf8_should_be_replaced_en
str1 = "Texte encod\xE9 en ISO-8859-1" str1 = "Texte encod\xE9 en ISO-8859-1"
str2 = (+"\xe9a\xe9b\xe9c\xe9d\xe9e test").force_encoding("ASCII-8BIT") str2 = "\xe9a\xe9b\xe9c\xe9d\xe9e test".b
txt_1 = Redmine::Export::PDF::RDMPdfEncoding::rdm_from_utf8(str1, 'UTF-8') txt_1 = Redmine::Export::PDF::RDMPdfEncoding::rdm_from_utf8(str1, 'UTF-8')
txt_2 = Redmine::Export::PDF::RDMPdfEncoding::rdm_from_utf8(str2, 'UTF-8') txt_2 = Redmine::Export::PDF::RDMPdfEncoding::rdm_from_utf8(str2, 'UTF-8')
assert_equal "ASCII-8BIT", txt_1.encoding.to_s assert_equal "ASCII-8BIT", txt_1.encoding.to_s
@@ -58,7 +58,7 @@ class PdfTest < ActiveSupport::TestCase
def test_rdm_pdf_iconv_invalid_utf8_should_be_replaced_ja def test_rdm_pdf_iconv_invalid_utf8_should_be_replaced_ja
str1 = "Texte encod\xE9 en ISO-8859-1" str1 = "Texte encod\xE9 en ISO-8859-1"
str2 = (+"\xe9a\xe9b\xe9c\xe9d\xe9e test").force_encoding("ASCII-8BIT") str2 = "\xe9a\xe9b\xe9c\xe9d\xe9e test".b
encoding = ( RUBY_PLATFORM == 'java' ? "SJIS" : "CP932" ) encoding = ( RUBY_PLATFORM == 'java' ? "SJIS" : "CP932" )
txt_1 = Redmine::Export::PDF::RDMPdfEncoding::rdm_from_utf8(str1, encoding) txt_1 = Redmine::Export::PDF::RDMPdfEncoding::rdm_from_utf8(str1, encoding)
txt_2 = Redmine::Export::PDF::RDMPdfEncoding::rdm_from_utf8(str2, encoding) txt_2 = Redmine::Export::PDF::RDMPdfEncoding::rdm_from_utf8(str2, encoding)
@@ -72,7 +72,7 @@ class PdfTest < ActiveSupport::TestCase
["CP932", "SJIS"].each do |encoding| ["CP932", "SJIS"].each do |encoding|
set_fixtures_attachments_directory set_fixtures_attachments_directory
str2 = (+"\x83e\x83X\x83g").force_encoding("ASCII-8BIT") str2 = "\x83e\x83X\x83g".b
a1 = Attachment.find(17) a1 = Attachment.find(17)
a2 = Attachment.find(19) a2 = Attachment.find(19)

View File

@@ -58,7 +58,7 @@ class GitAdapterTest < ActiveSupport::TestCase
) )
assert @adapter assert @adapter
@char_1 = 'Ü' @char_1 = 'Ü'
@str_felix_hex = (+"Felix Sch\xC3\xA4fer").force_encoding('ASCII-8BIT') @str_felix_hex = "Felix Sch\xC3\xA4fer".b
end end
def test_scm_version def test_scm_version

View File

@@ -47,7 +47,7 @@ class RepositoryBazaarTest < ActiveSupport::TestCase
Encoding.locale_charmap == "ISO-8859-1") Encoding.locale_charmap == "ISO-8859-1")
CHAR_1_UTF8_HEX = 'Ü' CHAR_1_UTF8_HEX = 'Ü'
CHAR_1_LATIN1_HEX = (+"\xdc").force_encoding('ASCII-8BIT') CHAR_1_LATIN1_HEX = "\xdc".b
def setup def setup
User.current = nil User.current = nil