Write char::DebugEscape
sequences using write_str
Instead of writing each `char` of an escape sequence one by one, this delegates to `Display`, which uses `write_str` internally in order to write the whole escape sequence at once.
This commit is contained in:
parent
f092f73c11
commit
0334c45bb5
2 changed files with 6 additions and 9 deletions
|
@ -44,7 +44,7 @@ fn ascii_escapes(b: &mut Bencher) {
|
||||||
assert_fmt(
|
assert_fmt(
|
||||||
s,
|
s,
|
||||||
r#""some\tmore\tascii\ttext\nthis time with some \"escapes\", also 64 byte""#,
|
r#""some\tmore\tascii\ttext\nthis time with some \"escapes\", also 64 byte""#,
|
||||||
21,
|
15,
|
||||||
);
|
);
|
||||||
b.iter(|| {
|
b.iter(|| {
|
||||||
black_box(format!("{:?}", black_box(s)));
|
black_box(format!("{:?}", black_box(s)));
|
||||||
|
@ -72,7 +72,7 @@ fn mostly_unicode(b: &mut Bencher) {
|
||||||
#[bench]
|
#[bench]
|
||||||
fn mixed(b: &mut Bencher) {
|
fn mixed(b: &mut Bencher) {
|
||||||
let s = "\"❤️\"\n\"hűha ez betű\"\n\"кириллических букв\".";
|
let s = "\"❤️\"\n\"hűha ez betű\"\n\"кириллических букв\".";
|
||||||
assert_fmt(s, r#""\"❤\u{fe0f}\"\n\"hűha ez betű\"\n\"кириллических букв\".""#, 36);
|
assert_fmt(s, r#""\"❤\u{fe0f}\"\n\"hűha ez betű\"\n\"кириллических букв\".""#, 21);
|
||||||
b.iter(|| {
|
b.iter(|| {
|
||||||
black_box(format!("{:?}", black_box(s)));
|
black_box(format!("{:?}", black_box(s)));
|
||||||
});
|
});
|
||||||
|
|
|
@ -2409,9 +2409,7 @@ impl Debug for str {
|
||||||
// If char needs escaping, flush backlog so far and write, else skip
|
// If char needs escaping, flush backlog so far and write, else skip
|
||||||
if esc.len() != 1 {
|
if esc.len() != 1 {
|
||||||
f.write_str(&self[from..i])?;
|
f.write_str(&self[from..i])?;
|
||||||
for c in esc {
|
Display::fmt(&esc, f)?;
|
||||||
f.write_char(c)?;
|
|
||||||
}
|
|
||||||
from = i + c.len_utf8();
|
from = i + c.len_utf8();
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
@ -2431,13 +2429,12 @@ impl Display for str {
|
||||||
impl Debug for char {
|
impl Debug for char {
|
||||||
fn fmt(&self, f: &mut Formatter<'_>) -> Result {
|
fn fmt(&self, f: &mut Formatter<'_>) -> Result {
|
||||||
f.write_char('\'')?;
|
f.write_char('\'')?;
|
||||||
for c in self.escape_debug_ext(EscapeDebugExtArgs {
|
let esc = self.escape_debug_ext(EscapeDebugExtArgs {
|
||||||
escape_grapheme_extended: true,
|
escape_grapheme_extended: true,
|
||||||
escape_single_quote: true,
|
escape_single_quote: true,
|
||||||
escape_double_quote: false,
|
escape_double_quote: false,
|
||||||
}) {
|
});
|
||||||
f.write_char(c)?
|
Display::fmt(&esc, f)?;
|
||||||
}
|
|
||||||
f.write_char('\'')
|
f.write_char('\'')
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
Loading…
Add table
Reference in a new issue