Skip to content

Commit

Permalink
Auto merge of #121150 - Swatinem:debug-ascii-str, r=<try>
Browse files Browse the repository at this point in the history
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 is an alternative / a companion to #121138.

The other PR is adding the fast path deep within `EscapeDebug`, whereas this skips as early as possible.
  • Loading branch information
bors committed Feb 18, 2024
2 parents 6f72620 + 04602a2 commit d3c44b1
Showing 1 changed file with 5 additions and 0 deletions.
5 changes: 5 additions & 0 deletions library/core/src/fmt/mod.rs
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 d3c44b1

Please sign in to comment.