|
| 1 | +use crate::formatting::hex_as_ascii; |
| 2 | + |
| 3 | +#[cfg(any(test, feature = "fmt"))] |
| 4 | +pub(crate) const fn char_display_len(c: char) -> usize { |
| 5 | + match c as u32 { |
| 6 | + 0..=127 => 1, |
| 7 | + 0x80..=0x7FF => 2, |
| 8 | + 0x800..=0xFFFF => 3, |
| 9 | + 0x10000..=u32::MAX => 4, |
| 10 | + } |
| 11 | +} |
| 12 | + |
| 13 | +#[cfg(any(test, feature = "fmt"))] |
| 14 | +pub(crate) const fn char_debug_len(c: char) -> usize { |
| 15 | + let inner = match c { |
| 16 | + '\t' | '\r' | '\n' | '\\' | '\'' | '\"' => 2, |
| 17 | + '\x00'..='\x1F' => 4, |
| 18 | + _ => char_display_len(c), |
| 19 | + }; |
| 20 | + inner + 2 |
| 21 | +} |
| 22 | + |
| 23 | +const fn char_to_utf8(char: char) -> ([u8; 4], usize) { |
| 24 | + let u32 = char as u32; |
| 25 | + match u32 { |
| 26 | + 0..=127 => ([u32 as u8, 0, 0, 0], 1), |
| 27 | + 0x80..=0x7FF => { |
| 28 | + let b0 = 0b1100_0000 | (u32 >> 6) as u8; |
| 29 | + let b1 = 0b1000_0000 | (u32 & 0b0011_1111) as u8; |
| 30 | + ([b0, b1, 0, 0], 2) |
| 31 | + } |
| 32 | + 0x800..=0xFFFF => { |
| 33 | + let b0 = 0b1110_0000 | (u32 >> 12) as u8; |
| 34 | + let b1 = 0b1000_0000 | ((u32 >> 6) & 0b0011_1111) as u8; |
| 35 | + let b2 = 0b1000_0000 | (u32 & 0b0011_1111) as u8; |
| 36 | + ([b0, b1, b2, 0], 3) |
| 37 | + } |
| 38 | + 0x10000..=u32::MAX => { |
| 39 | + let b0 = 0b1111_0000 | (u32 >> 18) as u8; |
| 40 | + let b1 = 0b1000_0000 | ((u32 >> 12) & 0b0011_1111) as u8; |
| 41 | + let b2 = 0b1000_0000 | ((u32 >> 6) & 0b0011_1111) as u8; |
| 42 | + let b3 = 0b1000_0000 | (u32 & 0b0011_1111) as u8; |
| 43 | + ([b0, b1, b2, b3], 4) |
| 44 | + } |
| 45 | + } |
| 46 | +} |
| 47 | + |
| 48 | +pub(crate) const fn char_to_display(char: char) -> FmtChar { |
| 49 | + let ([b0, b1, b2, b3], len) = char_to_utf8(char); |
| 50 | + FmtChar { |
| 51 | + encoded: [b0, b1, b2, b3, 0, 0], |
| 52 | + len: len as u8, |
| 53 | + } |
| 54 | +} |
| 55 | + |
| 56 | +pub(crate) const fn char_to_debug(c: char) -> FmtChar { |
| 57 | + let ([b0, b1, b2, b3], len) = match c { |
| 58 | + '\t' => (*br#"\t "#, 2), |
| 59 | + '\r' => (*br#"\r "#, 2), |
| 60 | + '\n' => (*br#"\n "#, 2), |
| 61 | + '\\' => (*br#"\\ "#, 2), |
| 62 | + '\'' => (*br#"\' "#, 2), |
| 63 | + '\"' => (*br#"\" "#, 2), |
| 64 | + '\x00'..='\x1F' => { |
| 65 | + let n = c as u8; |
| 66 | + ( |
| 67 | + [b'\\', b'x', hex_as_ascii(n >> 4), hex_as_ascii(n & 0b1111)], |
| 68 | + 4, |
| 69 | + ) |
| 70 | + } |
| 71 | + _ => char_to_utf8(c), |
| 72 | + }; |
| 73 | + |
| 74 | + let mut encoded = [b'\'', b0, b1, b2, b3, 0]; |
| 75 | + encoded[len + 1] = b'\''; |
| 76 | + |
| 77 | + FmtChar { |
| 78 | + encoded, |
| 79 | + len: (len as u8) + 2, |
| 80 | + } |
| 81 | +} |
| 82 | + |
| 83 | +#[derive(Copy, Clone)] |
| 84 | +pub struct FmtChar { |
| 85 | + encoded: [u8; 6], |
| 86 | + len: u8, |
| 87 | +} |
| 88 | + |
| 89 | +impl FmtChar { |
| 90 | + /// Array which contains the pre-len display/debug-formatted `char`, |
| 91 | + /// only `&self.encoded[][..self.len()]` should be copied. |
| 92 | + pub const fn encoded(&self) -> &[u8; 6] { |
| 93 | + &self.encoded |
| 94 | + } |
| 95 | + |
| 96 | + pub const fn len(&self) -> usize { |
| 97 | + self.len as usize |
| 98 | + } |
| 99 | + |
| 100 | + #[cfg(test)] |
| 101 | + fn as_bytes(&self) -> &[u8] { |
| 102 | + &self.encoded[..self.len()] |
| 103 | + } |
| 104 | +} |
| 105 | + |
| 106 | +#[cfg(all(test, not(miri)))] |
| 107 | +mod tests; |
0 commit comments