make format

This commit is contained in:
Bastien Montagne 2026-08-04 17:57:34 +02:00
parent 7d906159e5
commit 94f0d0930d
2 changed files with 8 additions and 8 deletions

View file

@ -57,13 +57,13 @@ class FormatToken:
return match.start()
@classmethod
def parse_string_lookup_first_token(cls, string, start_idx, token_idx=0, only_at_start_idx = False):
def parse_string_lookup_first_token(cls, string, start_idx, token_idx=0, only_at_start_idx=False):
"""
Search for the first instance of a formatting sequences (like %s, {:.4f}, etc.) in the given string,
and return a FormatToken for it (or None is none is found).
NOTE: This is not covering all exotic syntax cases of 'printf' or 'format'!
If `only_at_start_idx` is True, this function will only return a valid token if it starts at given `start_idx`.
Useful e.g. for code already calling `next_potential_formatting_index` itself.
"""
@ -148,7 +148,7 @@ class FormatToken:
# `%x12|` - What is this for actually?
# It also 'steals' the standard printf format for hexadecimal prints...
if (idx_fmt < (ln - 2) and string[idx_fmt + 1] == 'x' and
string[idx_fmt + 2] in cls.printf_format_widthprec
string[idx_fmt + 2] in cls.printf_format_widthprec
):
tk_len = 2
while (idx_fmt + tk_len) < ln and string[idx_fmt + tk_len] in cls.printf_format_widthprec:

View file

@ -154,7 +154,7 @@ def protect_format_seq(msg):
# Formatting tokens (%s, {:.4f}, etc.).
# Find if potential next token is actually a valid one.
token = FormatToken.parse_string_lookup_first_token(
msg, start_idx=idx_fmt, token_idx=token_idx, only_at_start_idx=True)
msg, start_idx=idx_fmt, token_idx=token_idx, only_at_start_idx=True)
if token is not None:
assert token.start_index == idx_fmt
tk_start_index = token.start_index
@ -165,10 +165,10 @@ def protect_format_seq(msg):
# processed escape group is enough to avoid wrongly including e.g. the '"' with the '%s' in
# unlikely cases like this: `'foo\"%s" bar'`
if (tk_start_index > idx and tk_start_index > last_idx_escape + 2 and
(tk_start_index + tk_len) < ln and
msg[tk_start_index - 1] in '\'"' and
msg[tk_start_index + tk_len] == msg[tk_start_index - 1]
):
(tk_start_index + tk_len) < ln and
msg[tk_start_index - 1] in '\'"' and
msg[tk_start_index + tk_len] == msg[tk_start_index - 1]
):
stride = token.start_index - idx - 1
ltr_len = len(token.token) + 2
else: