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
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
use std::borrow::Borrow;
use std::cell::RefCell;
use std::cmp::Ordering;
use std::collections::HashMap;
use std::fmt;
use std::hash::Hash;
use std::ops::Deref;
use std::rc::Rc;
#[derive(Clone, Copy, PartialEq, Eq, PartialOrd, Ord, Hash)]
pub struct Name(pub u32);
impl Name {
pub fn is_case_sensitive(&self) -> bool {
self.0 & 1 == 1
}
pub fn as_str(self) -> RcStr {
get_name_table().get(self)
}
}
impl fmt::Debug for Name {
fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
write!(f, "{}({})", self, self.0)
}
}
impl fmt::Display for Name {
fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
fmt::Display::fmt(&self.as_str(), f)
}
}
impl Into<String> for Name {
fn into(self) -> String {
self.as_str().into()
}
}
impl<'a> From<&'a str> for Name {
fn from(s: &'a str) -> Name {
get_name_table().intern(s, true)
}
}
#[derive(Clone, PartialEq, Hash, PartialOrd)]
pub struct RcStr(Rc<String>);
impl RcStr {
pub fn new(value: &str) -> RcStr {
RcStr(Rc::new(value.to_string()))
}
pub fn from(value: String) -> RcStr {
RcStr(Rc::new(value))
}
}
impl Eq for RcStr {}
impl Ord for RcStr {
fn cmp(&self, other: &RcStr) -> Ordering {
self[..].cmp(&other[..])
}
}
impl fmt::Debug for RcStr {
fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
self[..].fmt(f)
}
}
impl fmt::Display for RcStr {
fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
self[..].fmt(f)
}
}
impl Borrow<str> for RcStr {
fn borrow(&self) -> &str {
&self.0[..]
}
}
impl Deref for RcStr {
type Target = str;
fn deref(&self) -> &str {
&self.0[..]
}
}
impl Into<String> for RcStr {
fn into(self) -> String {
(*self.0).clone()
}
}
pub struct NameTable {
map: RefCell<HashMap<RcStr, Name>>,
vect: RefCell<Vec<RcStr>>,
}
impl NameTable {
pub fn new() -> NameTable {
NameTable {
map: RefCell::new(HashMap::new()),
vect: RefCell::new(Vec::new()),
}
}
pub fn intern(&self, value: &str, case_sensitive: bool) -> Name {
let mut map = self.map.borrow_mut();
if let Some(&idx) = map.get(value) {
return idx;
}
let mut vect = self.vect.borrow_mut();
if case_sensitive {
let new_idx = Name((vect.len() as u32) << 1 | 1);
let v = RcStr::new(value);
map.insert(v.clone(), new_idx);
vect.push(v);
new_idx
} else {
let new_idx = Name((vect.len() as u32) << 1 | 0);
let lower = value.to_lowercase();
if let Some(&idx) = map.get(lower.as_str()) {
return idx;
}
let v = RcStr::new(value);
map.insert(RcStr::from(lower), new_idx);
map.insert(v.clone(), new_idx);
vect.push(v);
new_idx
}
}
pub fn get(&self, idx: Name) -> RcStr {
(*self.vect.borrow())[(idx.0 >> 1) as usize].clone()
}
pub fn find<Q: ?Sized>(&self, value: &Q) -> Option<Name>
where
RcStr: Borrow<Q>,
Q: Eq + Hash,
{
(*self.map.borrow()).get(value).map(|v| *v)
}
}
pub fn get_name_table() -> Rc<NameTable> {
thread_local!(static TBL: Rc<NameTable> = {
let nt = NameTable::new();
Rc::new(nt)
});
TBL.with(|x| x.clone())
}