Skip to content

Commit 4d0f7e2

Browse files
committed
review
1 parent 4510439 commit 4d0f7e2

File tree

3 files changed

+15
-15
lines changed

3 files changed

+15
-15
lines changed

library/core/src/char/methods.rs

+3-3
Original file line numberDiff line numberDiff line change
@@ -395,9 +395,9 @@ impl char {
395395
'\t' => EscapeDebug::backslash(b't'),
396396
'\r' => EscapeDebug::backslash(b'r'),
397397
'\n' => EscapeDebug::backslash(b'n'),
398-
'\\' => EscapeDebug::backslash(self as u8),
399-
'"' if args.escape_double_quote => EscapeDebug::backslash(self as u8),
400-
'\'' if args.escape_single_quote => EscapeDebug::backslash(self as u8),
398+
'\\' => EscapeDebug::backslash(b'\\'),
399+
'"' if args.escape_double_quote => EscapeDebug::backslash(b'"'),
400+
'\'' if args.escape_single_quote => EscapeDebug::backslash(b'\''),
401401
_ if args.escape_grapheme_extended && self.is_grapheme_extended() => {
402402
EscapeDebug::from_unicode(self.escape_unicode())
403403
}

library/core/src/char/mod.rs

+4-4
Original file line numberDiff line numberDiff line change
@@ -293,17 +293,17 @@ impl fmt::Display for EscapeDefault {
293293
pub struct EscapeDebug(EscapeDebugInner);
294294

295295
#[derive(Clone, Debug)]
296+
// Note: It’s possible to manually encode the EscapeDebugInner inside of
297+
// EscapeIterInner (e.g. with alive=254..255 indicating that data[0..4] holds
298+
// a char) which would likely result in a more optimised code. For now we use
299+
// the option easier to implement.
296300
enum EscapeDebugInner {
297301
Bytes(escape::EscapeIterInner<10>),
298302
Char(char),
299303
}
300304

301305
impl EscapeDebug {
302306
fn printable(chr: char) -> Self {
303-
// Note: It’s possible to manually encode the EscapeDebugInner inside of
304-
// EscapeIterInner (e.g. with alive=254..255 indicating that data[0..4]
305-
// holds a char) which would likely result in a more optimised code.
306-
// For now we use the option easier to implement.
307307
Self(EscapeDebugInner::Char(chr))
308308
}
309309

library/core/src/escape.rs

+8-8
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,7 @@ const HEX_DIGITS: [u8; 16] = *b"0123456789abcdef";
77

88
/// Escapes a byte into provided buffer; returns length of escaped
99
/// representation.
10-
pub(super) fn escape_ascii_into(output: &mut [u8; 4], byte: u8) -> Range<u8> {
10+
pub(crate) fn escape_ascii_into(output: &mut [u8; 4], byte: u8) -> Range<u8> {
1111
let (data, len) = match byte {
1212
b'\t' => ([b'\\', b't', 0, 0], 2),
1313
b'\r' => ([b'\\', b'r', 0, 0], 2),
@@ -25,11 +25,10 @@ pub(super) fn escape_ascii_into(output: &mut [u8; 4], byte: u8) -> Range<u8> {
2525
}
2626

2727
/// Escapes a character into provided buffer using `\u{NNNN}` representation.
28-
pub(super) fn escape_unicode_into(output: &mut [u8; 10], ch: char) -> Range<u8> {
29-
let ch = (ch as u32) & 0x1f_ffff;
30-
28+
pub(crate) fn escape_unicode_into(output: &mut [u8; 10], ch: char) -> Range<u8> {
3129
output[9] = b'}';
3230

31+
let ch = ch as u32;
3332
output[3] = HEX_DIGITS[((ch >> 20) & 15) as usize];
3433
output[4] = HEX_DIGITS[((ch >> 16) & 15) as usize];
3534
output[5] = HEX_DIGITS[((ch >> 12) & 15) as usize];
@@ -50,24 +49,25 @@ pub(super) fn escape_unicode_into(output: &mut [u8; 10], ch: char) -> Range<u8>
5049
/// This is essentially equivalent to array’s IntoIter except that indexes are
5150
/// limited to u8 to reduce size of the structure.
5251
#[derive(Clone, Debug)]
53-
pub(super) struct EscapeIterInner<const N: usize> {
52+
pub(crate) struct EscapeIterInner<const N: usize> {
5453
// Invariant: data[alive] is all ASCII.
55-
pub(super) data: [u8; N],
54+
pub(crate) data: [u8; N],
5655

5756
// Invariant: alive.start <= alive.end <= N.
58-
pub(super) alive: Range<u8>,
57+
pub(crate) alive: Range<u8>,
5958
}
6059

6160
impl<const N: usize> EscapeIterInner<N> {
6261
pub fn new(data: [u8; N], alive: Range<u8>) -> Self {
62+
const { assert!(N < 256) };
6363
debug_assert!(alive.start <= alive.end && usize::from(alive.end) <= N, "{alive:?}");
6464
let this = Self { data, alive };
6565
debug_assert!(this.as_bytes().is_ascii(), "Expected ASCII, got {:?}", this.as_bytes());
6666
this
6767
}
6868

6969
fn as_bytes(&self) -> &[u8] {
70-
&self.data[(self.alive.start as usize)..(self.alive.end as usize)]
70+
&self.data[usize::from(self.alive.start)..usize::from(self.alive.end)]
7171
}
7272

7373
pub fn as_str(&self) -> &str {

0 commit comments

Comments
 (0)