/rust/registry/src/index.crates.io-6f17d22bba15001f/serde-1.0.140/src/de/utf8.rs
Line | Count | Source (jump to first uncovered line) |
1 | | use lib::*; |
2 | | |
3 | | const TAG_CONT: u8 = 0b1000_0000; |
4 | | const TAG_TWO_B: u8 = 0b1100_0000; |
5 | | const TAG_THREE_B: u8 = 0b1110_0000; |
6 | | const TAG_FOUR_B: u8 = 0b1111_0000; |
7 | | const MAX_ONE_B: u32 = 0x80; |
8 | | const MAX_TWO_B: u32 = 0x800; |
9 | | const MAX_THREE_B: u32 = 0x10000; |
10 | | |
11 | | #[inline] |
12 | 0 | pub fn encode(c: char) -> Encode { |
13 | 0 | let code = c as u32; |
14 | 0 | let mut buf = [0; 4]; |
15 | 0 | let pos = if code < MAX_ONE_B { |
16 | 0 | buf[3] = code as u8; |
17 | 0 | 3 |
18 | 0 | } else if code < MAX_TWO_B { |
19 | 0 | buf[2] = (code >> 6 & 0x1F) as u8 | TAG_TWO_B; |
20 | 0 | buf[3] = (code & 0x3F) as u8 | TAG_CONT; |
21 | 0 | 2 |
22 | 0 | } else if code < MAX_THREE_B { |
23 | 0 | buf[1] = (code >> 12 & 0x0F) as u8 | TAG_THREE_B; |
24 | 0 | buf[2] = (code >> 6 & 0x3F) as u8 | TAG_CONT; |
25 | 0 | buf[3] = (code & 0x3F) as u8 | TAG_CONT; |
26 | 0 | 1 |
27 | | } else { |
28 | 0 | buf[0] = (code >> 18 & 0x07) as u8 | TAG_FOUR_B; |
29 | 0 | buf[1] = (code >> 12 & 0x3F) as u8 | TAG_CONT; |
30 | 0 | buf[2] = (code >> 6 & 0x3F) as u8 | TAG_CONT; |
31 | 0 | buf[3] = (code & 0x3F) as u8 | TAG_CONT; |
32 | 0 | 0 |
33 | | }; |
34 | 0 | Encode { buf: buf, pos: pos } |
35 | 0 | } |
36 | | |
37 | | pub struct Encode { |
38 | | buf: [u8; 4], |
39 | | pos: usize, |
40 | | } |
41 | | |
42 | | impl Encode { |
43 | 0 | pub fn as_str(&self) -> &str { |
44 | 0 | str::from_utf8(&self.buf[self.pos..]).unwrap() |
45 | 0 | } |
46 | | } |