Skip to main content

object_rainbow/impls/
char.rs

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