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
mod tables;
#[cfg(test)]
mod tests;
use unicode_general_category::{get_general_category, GeneralCategory};
pub const UNICODE_VERSION: (u64, u64, u64) = (13, 0, 0);
pub trait UnicodeProperties: Sized + Copy {
fn is_id_start(self) -> bool;
fn is_id_continue(self) -> bool;
fn is_other_id_start(self) -> bool;
fn is_other_id_continue(self) -> bool;
fn is_pattern_syntax(self) -> bool;
fn is_pattern_whitespace(self) -> bool;
}
fn table_binary_search(target: char, table: &'static [char]) -> bool {
table.binary_search(&target).is_ok()
}
impl UnicodeProperties for char {
#[inline]
fn is_id_start(self) -> bool {
!self.is_pattern_syntax()
&& !self.is_pattern_whitespace()
&& (self.is_other_id_start()
|| matches!(
get_general_category(self),
GeneralCategory::LowercaseLetter
| GeneralCategory::ModifierLetter
| GeneralCategory::OtherLetter
| GeneralCategory::TitlecaseLetter
| GeneralCategory::UppercaseLetter
| GeneralCategory::LetterNumber
))
}
#[inline]
fn is_id_continue(self) -> bool {
!self.is_pattern_syntax()
&& !self.is_pattern_whitespace()
&& (self.is_id_start()
|| self.is_other_id_continue()
|| matches!(
get_general_category(self),
GeneralCategory::NonspacingMark
| GeneralCategory::SpacingMark
| GeneralCategory::DecimalNumber
| GeneralCategory::ConnectorPunctuation
))
}
#[inline]
fn is_other_id_start(self) -> bool {
table_binary_search(self, tables::OTHER_ID_START)
}
#[inline]
fn is_other_id_continue(self) -> bool {
table_binary_search(self, tables::OTHER_ID_CONTINUE)
}
#[inline]
fn is_pattern_syntax(self) -> bool {
table_binary_search(self, tables::PATTERN_SYNTAX)
}
#[inline]
fn is_pattern_whitespace(self) -> bool {
table_binary_search(self, tables::PATTERN_WHITE_SPACE)
}
}