Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Optimize escape_ascii. #125340

Closed
Closed
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
58 changes: 32 additions & 26 deletions library/core/src/escape.rs
Original file line number Diff line number Diff line change
Expand Up @@ -24,34 +24,40 @@ const fn backslash<const N: usize>(a: ascii::Char) -> ([ascii::Char; N], Range<u
const fn escape_ascii<const N: usize>(byte: u8) -> ([ascii::Char; N], Range<u8>) {
const { assert!(N >= 4) };

match byte {
b'\t' => backslash(ascii::Char::SmallT),
b'\r' => backslash(ascii::Char::SmallR),
b'\n' => backslash(ascii::Char::SmallN),
b'\\' => backslash(ascii::Char::ReverseSolidus),
b'\'' => backslash(ascii::Char::Apostrophe),
b'\"' => backslash(ascii::Char::QuotationMark),
byte => {
let mut output = [ascii::Char::Null; N];

if let Some(c) = byte.as_ascii()
&& !byte.is_ascii_control()
{
output[0] = c;
(output, 0..1)
} else {
let hi = HEX_DIGITS[(byte >> 4) as usize];
let lo = HEX_DIGITS[(byte & 0xf) as usize];

output[0] = ascii::Char::ReverseSolidus;
output[1] = ascii::Char::SmallX;
output[2] = hi;
output[3] = lo;

(output, 0..4)
let mut output = [ascii::Char::ReverseSolidus; N];
output[1] = ascii::Char::SmallX;
output[2] = HEX_DIGITS[(byte >> 4) as usize];
output[3] = HEX_DIGITS[(byte & 0b1111) as usize];

let len = if byte < 127 {
match byte {
c @ b'\"' | c @ b'\'' | c @ b'\\' => {
output[1] = c.as_ascii().unwrap();
2
}
c @ 0x20..=0x7e => {
output[0] = c.as_ascii().unwrap();
1
}
b'\n' => {
output[1] = ascii::Char::SmallN;
2
}
b'\r' => {
output[1] = ascii::Char::SmallR;
2
}
b'\t' => {
output[1] = ascii::Char::SmallT;
2
}
_ => 4,
}
}
} else {
4
};

(output, 0..len)
}

/// Escapes a character `\u{NNNN}` representation.
Expand Down
Loading