1
0
Fork 0
mirror of https://github.com/rails/rails.git synced 2022-11-09 12:12:34 -05:00

Merge pull request #36825 from cpruitt/transliterate-frozen-us-ascii-strings

Prevent error on transliterate with frozen strings
This commit is contained in:
Eileen M. Uchitelle 2019-07-31 13:04:58 -04:00 committed by GitHub
commit f66bfdb284
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
2 changed files with 8 additions and 7 deletions

View file

@ -60,6 +60,7 @@ module ActiveSupport
# Transliteration is restricted to UTF-8, US-ASCII and GB18030 strings # Transliteration is restricted to UTF-8, US-ASCII and GB18030 strings
# Other encodings will raise an ArgumentError. # Other encodings will raise an ArgumentError.
def transliterate(string, replacement = "?", locale: nil) def transliterate(string, replacement = "?", locale: nil)
string = string.dup if string.frozen?
raise ArgumentError, "Can only transliterate strings. Received #{string.class.name}" unless string.is_a?(String) raise ArgumentError, "Can only transliterate strings. Received #{string.class.name}" unless string.is_a?(String)
allowed_encodings = [Encoding::UTF_8, Encoding::US_ASCII, Encoding::GB18030] allowed_encodings = [Encoding::UTF_8, Encoding::US_ASCII, Encoding::GB18030]

View file

@ -59,19 +59,19 @@ class TransliterateTest < ActiveSupport::TestCase
end end
def test_transliterate_handles_strings_with_valid_utf8_encodings def test_transliterate_handles_strings_with_valid_utf8_encodings
string = String.new("A", encoding: Encoding::UTF_8) string = String.new("A", encoding: Encoding::UTF_8).freeze
assert_equal "A", ActiveSupport::Inflector.transliterate(string) assert_equal "A", ActiveSupport::Inflector.transliterate(string)
end end
def test_transliterate_handles_strings_with_valid_us_ascii_encodings def test_transliterate_handles_strings_with_valid_us_ascii_encodings
string = String.new("A", encoding: Encoding::US_ASCII) string = String.new("A", encoding: Encoding::US_ASCII).freeze
transcoded = ActiveSupport::Inflector.transliterate(string) transcoded = ActiveSupport::Inflector.transliterate(string)
assert_equal "A", transcoded assert_equal "A", transcoded
assert_equal Encoding::US_ASCII, transcoded.encoding assert_equal Encoding::US_ASCII, transcoded.encoding
end end
def test_transliterate_handles_strings_with_valid_gb18030_encodings def test_transliterate_handles_strings_with_valid_gb18030_encodings
string = String.new("A", encoding: Encoding::GB18030) string = String.new("A", encoding: Encoding::GB18030).freeze
transcoded = ActiveSupport::Inflector.transliterate(string) transcoded = ActiveSupport::Inflector.transliterate(string)
assert_equal "A", transcoded assert_equal "A", transcoded
assert_equal Encoding::GB18030, transcoded.encoding assert_equal Encoding::GB18030, transcoded.encoding
@ -84,7 +84,7 @@ class TransliterateTest < ActiveSupport::TestCase
Encoding::GB18030 Encoding::GB18030
] ]
incompatible_encodings.each do |encoding| incompatible_encodings.each do |encoding|
string = String.new("", encoding: encoding) string = String.new("", encoding: encoding).freeze
exception = assert_raises ArgumentError do exception = assert_raises ArgumentError do
ActiveSupport::Inflector.transliterate(string) ActiveSupport::Inflector.transliterate(string)
end end
@ -93,17 +93,17 @@ class TransliterateTest < ActiveSupport::TestCase
end end
def test_transliterate_handles_strings_with_invalid_utf8_bytes def test_transliterate_handles_strings_with_invalid_utf8_bytes
string = String.new("\255", encoding: Encoding::UTF_8) string = String.new("\255", encoding: Encoding::UTF_8).freeze
assert_equal "?", ActiveSupport::Inflector.transliterate(string) assert_equal "?", ActiveSupport::Inflector.transliterate(string)
end end
def test_transliterate_handles_strings_with_invalid_us_ascii_bytes def test_transliterate_handles_strings_with_invalid_us_ascii_bytes
string = String.new("\255", encoding: Encoding::US_ASCII) string = String.new("\255", encoding: Encoding::US_ASCII).freeze
assert_equal "?", ActiveSupport::Inflector.transliterate(string) assert_equal "?", ActiveSupport::Inflector.transliterate(string)
end end
def test_transliterate_handles_strings_with_invalid_gb18030_bytes def test_transliterate_handles_strings_with_invalid_gb18030_bytes
string = String.new("\255", encoding: Encoding::GB18030) string = String.new("\255", encoding: Encoding::GB18030).freeze
assert_equal "?", ActiveSupport::Inflector.transliterate(string) assert_equal "?", ActiveSupport::Inflector.transliterate(string)
end end
end end