Add a fast-path to Debug ASCII &str

Instead of going through the `EscapeDebug` machinery, we can just skip over ASCII chars that don’t need any escaping.
This commit is contained in:
Arpad Borsos 2024-02-15 17:36:21 +01:00 committed by Arpad Borsos
parent 0334c45bb5
commit 3fda931afe
No known key found for this signature in database
GPG key ID: FC7BCA77824B3298

View file

@ -2401,6 +2401,11 @@ impl Debug for str {
f.write_char('"')?;
let mut from = 0;
for (i, c) in self.char_indices() {
// a fast path for ASCII chars that do not need escapes:
if matches!(c, ' '..='~') && !matches!(c, '\\' | '\"') {
continue;
}
let esc = c.escape_debug_ext(EscapeDebugExtArgs {
escape_grapheme_extended: true,
escape_single_quote: false,