From 7c70980026318a5b1b5f6edba0d3986ee301f015 Mon Sep 17 00:00:00 2001 From: Hong Minhee Date: Mon, 26 Dec 2022 04:48:38 +0900 Subject: [PATCH] Fast path to measure width of ASCII strings See also: https://github.com/psf/black/pull/3445#issuecomment-1364459424 --- src/black/strings.py | 3 +++ 1 file changed, 3 insertions(+) diff --git a/src/black/strings.py b/src/black/strings.py index bf41baf9fa3..b86145ee48e 100644 --- a/src/black/strings.py +++ b/src/black/strings.py @@ -256,6 +256,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))