Skip to content

Commit

Permalink
Merge pull request #41347 from ricardotk002/use-string-grapheme-clusters
Browse files Browse the repository at this point in the history
Use String#grapheme_clusters instead of String#scan
  • Loading branch information
rafaelfranca committed Feb 5, 2021
2 parents c8a86d3 + 95f8bfe commit 7f2d44f
Show file tree
Hide file tree
Showing 2 changed files with 8 additions and 8 deletions.
Expand Up @@ -106,7 +106,7 @@ def truncate_bytes(truncate_at, omission: "…")
self.class.new.tap do |cut|
cut_at = truncate_at - omission.bytesize

scan(/\X/) do |grapheme|
each_grapheme_cluster do |grapheme|
if cut.bytesize + grapheme.bytesize <= cut_at
cut << grapheme
else
Expand Down
14 changes: 7 additions & 7 deletions activesupport/lib/active_support/multibyte/chars.rb
Expand Up @@ -103,7 +103,7 @@ def slice!(*args)
#
# 'Café'.mb_chars.reverse.to_s # => 'éfaC'
def reverse
chars(@wrapped_string.scan(/\X/).reverse.join)
chars(@wrapped_string.grapheme_clusters.reverse.join)
end

# Limits the byte size of the string to a number of bytes without breaking
Expand All @@ -126,26 +126,26 @@ def titleize

# Performs canonical decomposition on all the characters.
#
# 'é'.length # => 2
# 'é'.mb_chars.decompose.to_s.length # => 3
# 'é'.length # => 1
# 'é'.mb_chars.decompose.to_s.length # => 2
def decompose
chars(Unicode.decompose(:canonical, @wrapped_string.codepoints.to_a).pack("U*"))
end

# Performs composition on all the characters.
#
# 'é'.length # => 3
# 'é'.mb_chars.compose.to_s.length # => 2
# 'é'.length # => 1
# 'é'.mb_chars.compose.to_s.length # => 1
def compose
chars(Unicode.compose(@wrapped_string.codepoints.to_a).pack("U*"))
end

# Returns the number of grapheme clusters in the string.
#
# 'क्षि'.mb_chars.length # => 4
# 'क्षि'.mb_chars.grapheme_length # => 3
# 'क्षि'.mb_chars.grapheme_length # => 2
def grapheme_length
@wrapped_string.scan(/\X/).length
@wrapped_string.grapheme_clusters.length
end

# Replaces all ISO-8859-1 or CP1252 characters by their UTF-8 equivalent
Expand Down

0 comments on commit 7f2d44f

Please sign in to comment.