Skip to content

Commit

Permalink
Performance optimization: Be more specific about forbidden control ch…
Browse files Browse the repository at this point in the history
…aracters
  • Loading branch information
janlelis committed Jan 3, 2023
1 parent f0881e3 commit daa9c56
Showing 1 changed file with 4 additions and 1 deletion.
5 changes: 4 additions & 1 deletion lib/unicode/display_width.rb
Original file line number Diff line number Diff line change
Expand Up @@ -6,10 +6,13 @@
module Unicode
class DisplayWidth
DEPTHS = [0x10000, 0x1000, 0x100, 0x10].freeze
ASCII_NON_ZERO_REGEX = /[\0\x05\a\b\n\v\f\r\x0E\x0F]/

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:]]/)
if overwrite.empty? && string.ascii_only? && !string.match?(ASCII_NON_ZERO_REGEX)
return string.size
end

# Add width of each char
res = string.codepoints.inject(0){ |total_width, codepoint|
Expand Down

0 comments on commit daa9c56

Please sign in to comment.