Skip to content

Commit 7f4f4fc

Browse files
committed
Use indexing instead of .get_unchecked() for LUT lookup
Based on @paolobarbolini's tip that the unsafe block was unnecessary in this case. Not much left of `hexify()` after this, so seemed clearer to just inline it.
1 parent 8761424 commit 7f4f4fc

File tree

1 file changed

+2
-14
lines changed

1 file changed

+2
-14
lines changed

library/core/src/ascii.rs

+2-14
Original file line numberDiff line numberDiff line change
@@ -99,24 +99,12 @@ pub fn escape_default(c: u8) -> EscapeDefault {
9999
b'"' => ([b'\\', b'"', 0, 0], 2),
100100
b'\x20'..=b'\x7e' => ([c, 0, 0, 0], 1),
101101
_ => {
102-
let (b1, b2) = hexify(c);
103-
([b'\\', b'x', b1, b2], 4)
102+
let hex_digits: &[u8; 16] = b"0123456789abcdef";
103+
([b'\\', b'x', hex_digits[(c >> 4) as usize], hex_digits[(c & 0xf) as usize]], 4)
104104
}
105105
};
106106

107107
return EscapeDefault { range: 0..len, data };
108-
109-
#[inline]
110-
fn hexify(b: u8) -> (u8, u8) {
111-
let hex_digits: &[u8; 16] = b"0123456789abcdef";
112-
// SAFETY: For all n: u8, n >> 4 < 16 and n & 0xf < 16
113-
unsafe {
114-
(
115-
*hex_digits.get_unchecked((b >> 4) as usize),
116-
*hex_digits.get_unchecked((b & 0xf) as usize),
117-
)
118-
}
119-
}
120108
}
121109

122110
#[stable(feature = "rust1", since = "1.0.0")]

0 commit comments

Comments
 (0)