Skip to content

Commit 3b05813

Browse files
committed
Use an array instead of match for hexadecimal digits.
1 parent 7a1266c commit 3b05813

File tree

1 file changed

+6
-12
lines changed

1 file changed

+6
-12
lines changed

src/serializer.rs

Lines changed: 6 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -132,23 +132,17 @@ impl<'a> ToCss for Token<'a> {
132132
}
133133
}
134134

135-
fn to_hex_byte(value: u8) -> u8 {
136-
match value {
137-
0...9 => value + b'0',
138-
_ => value - 10 + b'a',
139-
}
140-
}
141-
142135
fn hex_escape<W>(ascii_byte: u8, dest: &mut W) -> fmt::Result where W:fmt::Write {
143-
let high = ascii_byte >> 4;
136+
static HEX_DIGITS: &'static [u8; 16] = b"0123456789abcdef";
144137
let b3;
145138
let b4;
146-
let bytes = if high > 0 {
147-
let low = ascii_byte & 0x0F;
148-
b4 = [b'\\', to_hex_byte(high), to_hex_byte(low), b' '];
139+
let bytes = if ascii_byte > 0x0F {
140+
let high = (ascii_byte >> 4) as usize;
141+
let low = (ascii_byte & 0x0F) as usize;
142+
b4 = [b'\\', HEX_DIGITS[high], HEX_DIGITS[low], b' '];
149143
&b4[..]
150144
} else {
151-
b3 = [b'\\', to_hex_byte(ascii_byte), b' '];
145+
b3 = [b'\\', HEX_DIGITS[ascii_byte as usize], b' '];
152146
&b3[..]
153147
};
154148
dest.write_str(unsafe { str::from_utf8_unchecked(&bytes) })

0 commit comments

Comments
 (0)