1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
use std::ops::{Range, RangeInclusive};

use ucd_trie::Error;

use crate::CharacterSet;

pub const UNICODE_MAX: u32 = 0x110000;

#[derive(Debug, Clone)]
pub struct InsertAction {
    pub insert: Vec<Range<u32>>,
    pub error: Option<Error>,
}

impl CharacterSet {
    pub fn all() -> Self {
        Self { all: Box::new([true; UNICODE_MAX as usize]) }
    }

    pub fn nil() -> Self {
        Self { all: Box::new([false; UNICODE_MAX as usize]) }
    }
    pub fn include(&mut self, set: impl Into<InsertAction>) -> Result<(), Error> {
        self.try_insert(set, true)
    }
    pub fn exclude(&mut self, set: impl Into<InsertAction>) -> Result<(), Error> {
        self.try_insert(set, false)
    }
    fn try_insert(&mut self, set: impl Into<InsertAction>, result: bool) -> Result<(), Error> {
        let InsertAction { insert, error } = set.into();
        if let Some(e) = error {
            return Err(e);
        }
        self.insert(insert, result);
        Ok(())
    }
    fn insert(&mut self, set: Vec<Range<u32>>, result: bool) {
        for range in set {
            for cp in range {
                debug_assert!(cp <= UNICODE_MAX);
                self.all[cp as usize] = result;
            }
        }
    }
}

impl From<char> for InsertAction {
    fn from(char: char) -> Self {
        let end = char as u32 + 1;
        InsertAction { insert: vec![Range { start: char as u32, end }], error: None }
    }
}

impl From<Range<char>> for InsertAction {
    fn from(range: Range<char>) -> Self {
        let start = range.start as u32;
        let end = range.end as u32;
        Self::from((start, end))
    }
}

impl From<RangeInclusive<char>> for InsertAction {
    fn from(range: RangeInclusive<char>) -> Self {
        let start = (*range.start()) as u32;
        let end = (*range.end()) as u32 + 1;
        Self::from((start, end))
    }
}

impl From<u32> for InsertAction {
    fn from(char: u32) -> Self {
        InsertAction { insert: vec![Range { start: char, end: char + 1 }], error: None }
    }
}

impl From<(u32, u32)> for InsertAction {
    fn from(range: (u32, u32)) -> Self {
        let mut error = None;
        if range.1 > UNICODE_MAX {
            error = Some(Error::InvalidCodepoint(range.0))
        }
        if range.0 > UNICODE_MAX {
            error = Some(Error::InvalidCodepoint(range.1))
        }
        InsertAction { insert: vec![Range { start: range.0, end: range.1 }], error }
    }
}

impl From<Range<u32>> for InsertAction {
    fn from(range: Range<u32>) -> Self {
        let start = range.start;
        let end = range.end;
        Self::from((start, end))
    }
}

impl From<RangeInclusive<u32>> for InsertAction {
    fn from(range: RangeInclusive<u32>) -> Self {
        let start = *range.start();
        let end = range.end() + 1;
        Self::from((start, end))
    }
}