Skip to content

Commit 04602a2

Browse files
committed
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.
1 parent bd6b336 commit 04602a2

File tree

1 file changed

+5
-0
lines changed

1 file changed

+5
-0
lines changed

library/core/src/fmt/mod.rs

+5
Original file line numberDiff line numberDiff line change
@@ -2340,6 +2340,11 @@ impl Debug for str {
23402340
f.write_char('"')?;
23412341
let mut from = 0;
23422342
for (i, c) in self.char_indices() {
2343+
// a fast path for ASCII chars that do not need escapes:
2344+
if matches!(c, ' '..='~') && !matches!(c, '\\' | '\"') {
2345+
continue;
2346+
}
2347+
23432348
let esc = c.escape_debug_ext(EscapeDebugExtArgs {
23442349
escape_grapheme_extended: true,
23452350
escape_single_quote: false,

0 commit comments

Comments
 (0)