Skip to content

Commit

Permalink
Optimize for ASCII-only strings without control symbols
Browse files Browse the repository at this point in the history
  • Loading branch information
fatkodima committed Jan 2, 2023
1 parent db0a5aa commit bfef8da
Showing 1 changed file with 3 additions and 0 deletions.
3 changes: 3 additions & 0 deletions lib/unicode/display_width.rb
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,9 @@ class DisplayWidth
DEPTHS = [0x10000, 0x1000, 0x100, 0x10].freeze

def self.of(string, ambiguous = 1, overwrite = {}, options = {})
# Optimization for ASCII-only strings without control symbols.
return string.size if overwrite.empty? && string.ascii_only? && !string.match?(/[[:cntrl:]]/)

res = string.codepoints.inject(0){ |total_width, codepoint|
index_or_value = INDEX
codepoint_depth_offset = codepoint
Expand Down

0 comments on commit bfef8da

Please sign in to comment.