aboutsummaryrefslogtreecommitdiffstats
path: root/activesupport/lib/active_support/inflector/transliterate.rb
diff options
context:
space:
mode:
authorEileen M. Uchitelle <eileencodes@users.noreply.github.com>2019-07-17 09:55:12 -0400
committerGitHub <noreply@github.com>2019-07-17 09:55:12 -0400
commite4747a4965ad7f081f6c66cadcadffe0545e4025 (patch)
tree909515a96d7f1a6982b99d6ded86505a06ae98b0 /activesupport/lib/active_support/inflector/transliterate.rb
parent2a28b7c292b3d6c0a01722f50beedb8ec1bf074c (diff)
parent05f9e3ef92114c2df978009073d0b47fe1323eb7 (diff)
downloadrails-e4747a4965ad7f081f6c66cadcadffe0545e4025.tar.gz
rails-e4747a4965ad7f081f6c66cadcadffe0545e4025.tar.bz2
rails-e4747a4965ad7f081f6c66cadcadffe0545e4025.zip
Merge pull request #36690 from cpruitt/make-parameterize-requires-utf-8-explicit
Make UTF-8 string requirement explicit for `ActiveSupport::Inflector.transliterate`
Diffstat (limited to 'activesupport/lib/active_support/inflector/transliterate.rb')
-rw-r--r--activesupport/lib/active_support/inflector/transliterate.rb9
1 files changed, 7 insertions, 2 deletions
diff --git a/activesupport/lib/active_support/inflector/transliterate.rb b/activesupport/lib/active_support/inflector/transliterate.rb
index ec6e9ccb59..ea7161a6ba 100644
--- a/activesupport/lib/active_support/inflector/transliterate.rb
+++ b/activesupport/lib/active_support/inflector/transliterate.rb
@@ -5,8 +5,9 @@ require "active_support/i18n"
module ActiveSupport
module Inflector
- # Replaces non-ASCII characters with an ASCII approximation, or if none
- # exists, a replacement character which defaults to "?".
+ # Replaces non-ASCII characters in a UTF-8 encoded string with an ASCII
+ # approximation, or if none exists, a replacement character which
+ # defaults to "?".
#
# transliterate('Ærøskøbing')
# # => "AEroskobing"
@@ -56,8 +57,12 @@ module ActiveSupport
#
# 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),