aboutsummaryrefslogtreecommitdiffstats
path: root/activesupport
diff options
context:
space:
mode:
authorFrancesco Rodríguez <frodsan@me.com>2018-10-16 22:36:30 +0200
committerJeremy Daer <jeremydaer@gmail.com>2018-10-16 16:32:56 -0700
commitd234dd677a54d2ec787b8a2ee552b620fe6c3bb5 (patch)
treeb8367eb803a9dd99a52c17832afb362a85e74c4c /activesupport
parent6647341aead6b90c57dd0e9c42b0350f0747de45 (diff)
downloadrails-d234dd677a54d2ec787b8a2ee552b620fe6c3bb5.tar.gz
rails-d234dd677a54d2ec787b8a2ee552b620fe6c3bb5.tar.bz2
rails-d234dd677a54d2ec787b8a2ee552b620fe6c3bb5.zip
Refactor Chars#reverse and Chars#grapheme_length
Use \X meta character directly to get grapheme clusters. Thanks to @mtsmfm for the tip: https://github.com/rails/rails/pull/34123#issuecomment-429028878 r? @jeremy
Diffstat (limited to 'activesupport')
-rw-r--r--activesupport/lib/active_support/multibyte/chars.rb4
1 files changed, 2 insertions, 2 deletions
diff --git a/activesupport/lib/active_support/multibyte/chars.rb b/activesupport/lib/active_support/multibyte/chars.rb
index 424ffa993c..bd8cab3460 100644
--- a/activesupport/lib/active_support/multibyte/chars.rb
+++ b/activesupport/lib/active_support/multibyte/chars.rb
@@ -113,7 +113,7 @@ module ActiveSupport #:nodoc:
#
# 'Café'.mb_chars.reverse.to_s # => 'éfaC'
def reverse
- chars(Unicode.unpack_graphemes(@wrapped_string).reverse.flatten.pack("U*"))
+ chars(@wrapped_string.scan(/\X/).reverse.join)
end
# Limits the byte size of the string to a number of bytes without breaking
@@ -183,7 +183,7 @@ module ActiveSupport #:nodoc:
# 'क्षि'.mb_chars.length # => 4
# 'क्षि'.mb_chars.grapheme_length # => 3
def grapheme_length
- Unicode.unpack_graphemes(@wrapped_string).length
+ @wrapped_string.scan(/\X/).length
end
# Replaces all ISO-8859-1 or CP1252 characters by their UTF-8 equivalent