Perf: Skip string normalization when possible (#10116)

This commit is contained in:
Micha Reiser 2024-02-26 18:35:29 +01:00 committed by GitHub
parent 15b87ea8be
commit 8dc22d5793
No known key found for this signature in database
GPG key ID: B5690EEEBB952194
3 changed files with 170 additions and 88 deletions

View file

@ -59,16 +59,16 @@ impl Format<PyFormatContext<'_>> for FormatFString<'_> {
return result;
}
let quotes = normalizer.choose_quotes(&string, &locator);
let quote_selection = normalizer.choose_quotes(&string, &locator);
let context = FStringContext::new(
string.prefix(),
quotes,
quote_selection.quotes(),
FStringLayout::from_f_string(self.value, &locator),
);
// Starting prefix and quote
write!(f, [string.prefix(), quotes])?;
write!(f, [string.prefix(), quote_selection.quotes()])?;
f.join()
.entries(
@ -80,7 +80,7 @@ impl Format<PyFormatContext<'_>> for FormatFString<'_> {
.finish()?;
// Ending quote
quotes.fmt(f)
quote_selection.quotes().fmt(f)
}
}

View file

@ -59,6 +59,7 @@ impl Format<PyFormatContext<'_>> for FormatFStringLiteralElement<'_> {
let literal_content = f.context().locator().slice(self.element.range());
let normalized = normalize_string(
literal_content,
0,
self.context.quotes(),
self.context.prefix(),
is_hex_codes_in_unicode_sequences_enabled(f.context()),