Skip to content

Commit

Permalink
Add a fast-path to Debug ASCII &str
Browse files Browse the repository at this point in the history
Instead of going through the `EscapeDebug` machinery, we can just skip over ASCII chars that don’t need any escaping.
  • Loading branch information
Swatinem committed Feb 15, 2024
1 parent bd6b336 commit 04602a2
Showing 1 changed file with 5 additions and 0 deletions.
5 changes: 5 additions & 0 deletions library/core/src/fmt/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2340,6 +2340,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,
Expand Down

0 comments on commit 04602a2

Please sign in to comment.