Skip to content

Commit d3c44b1

Browse files
committed
Auto merge of #121150 - Swatinem:debug-ascii-str, r=<try>
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.
2 parents 6f72620 + 04602a2 commit d3c44b1

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)