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 Feb 10, 2023
1 parent 9646c4c commit 52e1406
Showing 1 changed file with 3 additions and 0 deletions.
3 changes: 3 additions & 0 deletions src/black/strings.py
Original file line number Diff line number Diff line change
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 52e1406

Please sign in to comment.