diff options
| author | Arpad Borsos <arpad.borsos@sentry.io> | 2024-02-15 17:36:21 +0100 |
|---|---|---|
| committer | Arpad Borsos <swatinem@swatinem.de> | 2024-05-20 10:04:45 +0200 |
| commit | 3fda931afe98ccfbc2da7307731b268a38b153e9 (patch) | |
| tree | da8e4a59a25109b43a47a0604e3942b35227abeb | |
| parent | 0334c45bb525f7eb9078f3c5b5074320a9ac7a3d (diff) | |
| download | rust-3fda931afe98ccfbc2da7307731b268a38b153e9.tar.gz rust-3fda931afe98ccfbc2da7307731b268a38b153e9.zip | |
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.
| -rw-r--r-- | library/core/src/fmt/mod.rs | 5 |
1 files changed, 5 insertions, 0 deletions
diff --git a/library/core/src/fmt/mod.rs b/library/core/src/fmt/mod.rs index 10e1d27c88a..4cc2a9cf96d 100644 --- a/library/core/src/fmt/mod.rs +++ b/library/core/src/fmt/mod.rs @@ -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, |
