diff options
| author | Michal Nazarewicz <mina86@mina86.com> | 2023-04-30 03:59:11 +0200 |
|---|---|---|
| committer | Michal Nazarewicz <mina86@mina86.com> | 2023-04-30 03:59:11 +0200 |
| commit | 4d0f7e2f393937afac76c97d33e0d96c50160510 (patch) | |
| tree | cae40a47335c2458f27eec288593e3be2cd38895 | |
| parent | 45104397e5541b76e281ed9b72cb0a89a4c850eb (diff) | |
| download | rust-4d0f7e2f393937afac76c97d33e0d96c50160510.tar.gz rust-4d0f7e2f393937afac76c97d33e0d96c50160510.zip | |
review
| -rw-r--r-- | library/core/src/char/methods.rs | 6 | ||||
| -rw-r--r-- | library/core/src/char/mod.rs | 8 | ||||
| -rw-r--r-- | library/core/src/escape.rs | 16 |
3 files changed, 15 insertions, 15 deletions
diff --git a/library/core/src/char/methods.rs b/library/core/src/char/methods.rs index 8f149a9ece2..2408f178075 100644 --- a/library/core/src/char/methods.rs +++ b/library/core/src/char/methods.rs @@ -395,9 +395,9 @@ impl char { '\t' => EscapeDebug::backslash(b't'), '\r' => EscapeDebug::backslash(b'r'), '\n' => EscapeDebug::backslash(b'n'), - '\\' => EscapeDebug::backslash(self as u8), - '"' if args.escape_double_quote => EscapeDebug::backslash(self as u8), - '\'' if args.escape_single_quote => EscapeDebug::backslash(self as u8), + '\\' => EscapeDebug::backslash(b'\\'), + '"' if args.escape_double_quote => EscapeDebug::backslash(b'"'), + '\'' if args.escape_single_quote => EscapeDebug::backslash(b'\''), _ if args.escape_grapheme_extended && self.is_grapheme_extended() => { EscapeDebug::from_unicode(self.escape_unicode()) } diff --git a/library/core/src/char/mod.rs b/library/core/src/char/mod.rs index 6383a5b5ca9..e186db7052c 100644 --- a/library/core/src/char/mod.rs +++ b/library/core/src/char/mod.rs @@ -293,6 +293,10 @@ impl fmt::Display for EscapeDefault { pub struct EscapeDebug(EscapeDebugInner); #[derive(Clone, Debug)] +// Note: It’s possible to manually encode the EscapeDebugInner inside of +// EscapeIterInner (e.g. with alive=254..255 indicating that data[0..4] holds +// a char) which would likely result in a more optimised code. For now we use +// the option easier to implement. enum EscapeDebugInner { Bytes(escape::EscapeIterInner<10>), Char(char), @@ -300,10 +304,6 @@ enum EscapeDebugInner { impl EscapeDebug { fn printable(chr: char) -> Self { - // Note: It’s possible to manually encode the EscapeDebugInner inside of - // EscapeIterInner (e.g. with alive=254..255 indicating that data[0..4] - // holds a char) which would likely result in a more optimised code. - // For now we use the option easier to implement. Self(EscapeDebugInner::Char(chr)) } diff --git a/library/core/src/escape.rs b/library/core/src/escape.rs index 66faa0316d1..c52c1fa870e 100644 --- a/library/core/src/escape.rs +++ b/library/core/src/escape.rs @@ -7,7 +7,7 @@ const HEX_DIGITS: [u8; 16] = *b"0123456789abcdef"; /// Escapes a byte into provided buffer; returns length of escaped /// representation. -pub(super) fn escape_ascii_into(output: &mut [u8; 4], byte: u8) -> Range<u8> { +pub(crate) fn escape_ascii_into(output: &mut [u8; 4], byte: u8) -> Range<u8> { let (data, len) = match byte { b'\t' => ([b'\\', b't', 0, 0], 2), 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> { } /// Escapes a character into provided buffer using `\u{NNNN}` representation. -pub(super) fn escape_unicode_into(output: &mut [u8; 10], ch: char) -> Range<u8> { - let ch = (ch as u32) & 0x1f_ffff; - +pub(crate) fn escape_unicode_into(output: &mut [u8; 10], ch: char) -> Range<u8> { output[9] = b'}'; + let ch = ch as u32; output[3] = HEX_DIGITS[((ch >> 20) & 15) as usize]; output[4] = HEX_DIGITS[((ch >> 16) & 15) as usize]; output[5] = HEX_DIGITS[((ch >> 12) & 15) as usize]; @@ -50,16 +49,17 @@ pub(super) fn escape_unicode_into(output: &mut [u8; 10], ch: char) -> Range<u8> /// This is essentially equivalent to array’s IntoIter except that indexes are /// limited to u8 to reduce size of the structure. #[derive(Clone, Debug)] -pub(super) struct EscapeIterInner<const N: usize> { +pub(crate) struct EscapeIterInner<const N: usize> { // Invariant: data[alive] is all ASCII. - pub(super) data: [u8; N], + pub(crate) data: [u8; N], // Invariant: alive.start <= alive.end <= N. - pub(super) alive: Range<u8>, + pub(crate) alive: Range<u8>, } impl<const N: usize> EscapeIterInner<N> { pub fn new(data: [u8; N], alive: Range<u8>) -> Self { + const { assert!(N < 256) }; debug_assert!(alive.start <= alive.end && usize::from(alive.end) <= N, "{alive:?}"); let this = Self { data, alive }; debug_assert!(this.as_bytes().is_ascii(), "Expected ASCII, got {:?}", this.as_bytes()); @@ -67,7 +67,7 @@ impl<const N: usize> EscapeIterInner<N> { } fn as_bytes(&self) -> &[u8] { - &self.data[(self.alive.start as usize)..(self.alive.end as usize)] + &self.data[usize::from(self.alive.start)..usize::from(self.alive.end)] } pub fn as_str(&self) -> &str { |
