Skip to main content

object_rainbow/impls/
char.rs

1use crate::*;
2
3impl ToOutput for char {
4    fn to_output(&self, output: &mut impl Output) {
5        if output.is_real() {
6            let mut buf = [0; 4];
7            self.encode_utf8(&mut buf).to_output(output);
8        }
9    }
10}
11
12impl<I: ParseInput> Parse<I> for char {
13    fn parse(input: I) -> crate::Result<Self> {
14        Self::parse_as_inline(input)
15    }
16}
17
18impl<I: ParseInput> ParseInline<I> for char {
19    fn parse_inline(input: &mut I) -> crate::Result<Self> {
20        let c0 = input.parse_inline::<u8>()?;
21        if c0 & 0b11000000 != 0b11000000 {
22            let c = str::from_utf8(&[c0])
23                .map_err(crate::Error::parse)?
24                .chars()
25                .next()
26                .unwrap();
27            return Ok(c);
28        }
29        let c1 = input.parse_inline::<u8>()?;
30        if c0 & 0b00100000 == 0 {
31            let c = str::from_utf8(&[c0, c1])
32                .map_err(crate::Error::parse)?
33                .chars()
34                .next()
35                .unwrap();
36            return Ok(c);
37        }
38        let c2 = input.parse_inline::<u8>()?;
39        if c0 & 0b00010000 == 0 {
40            let c = str::from_utf8(&[c0, c1, c2])
41                .map_err(crate::Error::parse)?
42                .chars()
43                .next()
44                .unwrap();
45            return Ok(c);
46        }
47        let c3 = input.parse_inline::<u8>()?;
48        let c = str::from_utf8(&[c0, c1, c2, c3])
49            .map_err(crate::Error::parse)?
50            .chars()
51            .next()
52            .unwrap();
53        Ok(c)
54    }
55}
56
57impl InlineOutput for char {}
58impl Tagged for char {}
59impl ListHashes for char {}
60impl Topological for char {}
61
62#[test]
63fn reparse() -> crate::Result<()> {
64    assert_eq!('x'.reparse()?, 'x');
65    assert_eq!('ч'.reparse()?, 'ч');
66    Ok(())
67}