Skip to content

Commit

Permalink
Merge pull request #36700 from cpruitt/revert-36690-make-parameterize…
Browse files Browse the repository at this point in the history
…-requires-utf-8-explicit

Revert "Make UTF-8 string requirement explicit for `ActiveSupport::Inflector.transliterate`"
  • Loading branch information
eileencodes committed Jul 17, 2019
2 parents e4747a4 + 0deecc7 commit 6a50498
Show file tree
Hide file tree
Showing 2 changed files with 2 additions and 15 deletions.
9 changes: 2 additions & 7 deletions activesupport/lib/active_support/inflector/transliterate.rb
Expand Up @@ -5,9 +5,8 @@

module ActiveSupport
module Inflector
# Replaces non-ASCII characters in a UTF-8 encoded string with an ASCII
# approximation, or if none exists, a replacement character which
# defaults to "?".
# Replaces non-ASCII characters with an ASCII approximation, or if none
# exists, a replacement character which defaults to "?".
#
# transliterate('Ærøskøbing')
# # => "AEroskobing"
Expand Down Expand Up @@ -57,12 +56,8 @@ module Inflector
#
# transliterate('Jürgen', locale: :de)
# # => "Juergen"
#
# This method requires that `string` be UTF-8 encoded. Passing an argument
# with a different string encoding will raise an ArgumentError.
def transliterate(string, replacement = "?", locale: nil)
raise ArgumentError, "Can only transliterate strings. Received #{string.class.name}" unless string.is_a?(String)
raise ArgumentError, "Can only transliterate UTF-8 strings. Received string with encoding #{string.encoding}" unless string.encoding == ::Encoding::UTF_8

I18n.transliterate(
ActiveSupport::Multibyte::Unicode.tidy_bytes(string).unicode_normalize(:nfc),
Expand Down
8 changes: 0 additions & 8 deletions activesupport/test/transliterate_test.rb
Expand Up @@ -57,12 +57,4 @@ def test_transliterate_handles_unknown_object
end
assert_equal "Can only transliterate strings. Received Object", exception.message
end

def test_transliterate_handles_non_unicode_strings
ascii_8bit_string = "A".b
exception = assert_raises ArgumentError do
assert_equal "A", ActiveSupport::Inflector.transliterate(ascii_8bit_string)
end
assert_equal "Can only transliterate UTF-8 strings. Received string with encoding ASCII-8BIT", exception.message
end
end

0 comments on commit 6a50498

Please sign in to comment.