Skip to content

Commit

Permalink
Fast path to measure width of ASCII strings
Browse files Browse the repository at this point in the history
  • Loading branch information
dahlia committed Jan 25, 2023
1 parent 098742d commit 4565670
Showing 1 changed file with 3 additions and 0 deletions.
3 changes: 3 additions & 0 deletions src/black/strings.py
Expand Up @@ -298,6 +298,9 @@ def str_width(line_str: str) -> int:
You could utilize this function to determine, for example, if a string
is too wide to display in a terminal or editor.
"""
if line_str.isascii():
# Fast path for most of strings which contains only characters in ASCII:
return len(line_str)
return sum(map(char_width, line_str))


Expand Down

0 comments on commit 4565670

Please sign in to comment.