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
104
105
106
107
108
109
110
111
112
113
114
115
116
117
#![deny(missing_docs)]
#![cfg_attr(not(feature = "std"), no_std)]
use core::fmt;
#[cfg(feature = "std")]
pub use crate::owned::{Error, Result, TrieSetOwned};
#[cfg(test)]
#[allow(dead_code)]
mod general_category;
#[cfg(feature = "std")]
mod owned;
const CHUNK_SIZE: usize = 64;
pub type TrieSet = TrieSetSlice<'static>;
#[derive(Clone, Copy)]
pub struct TrieSetSlice<'a> {
#[doc(hidden)]
pub tree1_level1: &'a [u64],
#[doc(hidden)]
pub tree2_level1: &'a [u8],
#[doc(hidden)]
pub tree2_level2: &'a [u64],
#[doc(hidden)]
pub tree3_level1: &'a [u8],
#[doc(hidden)]
pub tree3_level2: &'a [u8],
#[doc(hidden)]
pub tree3_level3: &'a [u64],
}
impl<'a> fmt::Debug for TrieSetSlice<'a> {
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
write!(f, "TrieSetSlice(...)")
}
}
impl<'a> TrieSetSlice<'a> {
pub fn contains_char(&self, c: char) -> bool {
self.contains(c as usize)
}
pub fn contains_u32(&self, cp: u32) -> bool {
if cp > 0x10FFFF {
return false;
}
self.contains(cp as usize)
}
#[inline(always)]
fn contains(&self, cp: usize) -> bool {
if cp < 0x800 {
self.chunk_contains(cp, self.tree1_level1[cp >> 6])
} else if cp < 0x10000 {
let leaf = match self.tree2_level1.get((cp >> 6) - 0x20) {
None => return false,
Some(&leaf) => leaf,
};
self.chunk_contains(cp, self.tree2_level2[leaf as usize])
} else {
let child = match self.tree3_level1.get((cp >> 12) - 0x10) {
None => return false,
Some(&child) => child,
};
let i = ((child as usize) * CHUNK_SIZE) + ((cp >> 6) & 0b111111);
let leaf = self.tree3_level2[i];
self.chunk_contains(cp, self.tree3_level3[leaf as usize])
}
}
#[inline(always)]
fn chunk_contains(&self, cp: usize, chunk: u64) -> bool {
((chunk >> (cp & 0b111111)) & 1) == 1
}
}