-
Notifications
You must be signed in to change notification settings - Fork 1.2k
/
Copy pathCharTrie.cpp
234 lines (220 loc) · 6.48 KB
/
CharTrie.cpp
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
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
//-------------------------------------------------------------------------------------------------------
// Copyright (C) Microsoft. All rights reserved.
// Copyright (c) ChakraCore Project Contributors. All rights reserved.
// Licensed under the MIT license. See LICENSE.txt file in the project root for full license information.
//-------------------------------------------------------------------------------------------------------
#include "ParserPch.h"
namespace UnifiedRegex
{
// The VS2013 linker treats this as a redefinition of an already
// defined constant and complains. So skip the declaration if we're compiling
// with VS2013 or below.
#if !defined(_MSC_VER) || _MSC_VER >= 1900
const int CharTrie::initCapacity;
#endif
// ----------------------------------------------------------------------
// CharTrie
// ----------------------------------------------------------------------
inline bool CharTrie::Find(Char c, int& outi)
{
if (count == 0)
{
outi = 0;
return false;
}
int l = 0;
int h = count - 1;
while (true)
{
int m = (l + h) / 2;
if (children[m].c == c)
{
outi = m;
return true;
}
else if (CTU(children[m].c) < CTU(c))
{
l = m + 1;
if (l > h)
{
outi = l;
return false;
}
}
else
{
h = m - 1;
if (h < l)
{
outi = m;
return false;
}
}
}
return false;
}
void CharTrie::FreeBody(ArenaAllocator* allocator)
{
for (int i = 0; i < count; i++)
children[i].node.FreeBody(allocator);
if (capacity > 0)
AdeleteArray(allocator, capacity, children);
#if DBG
count = 0;
capacity = 0;
children = 0;
#endif
}
CharTrie* CharTrie::Add(ArenaAllocator* allocator, Char c)
{
int i;
if (!Find(c, i))
{
if (capacity <= count)
{
int newCapacity = max(capacity * 2, initCapacity);
children = (CharTrieEntry*)allocator->Realloc(children, capacity * sizeof(CharTrieEntry), newCapacity * sizeof(CharTrieEntry));
capacity = newCapacity;
}
for (int j = count; j > i; j--)
{
children[j].c = children[j - 1].c;
children[j].node = children[j - 1].node;
}
children[i].c = c;
children[i].node.Reset();
count++;
}
return &children[i].node;
}
bool CharTrie::IsDepthZero() const
{
return isAccepting && count == 0;
}
bool CharTrie::IsDepthOne() const
{
if (isAccepting)
return 0;
for (int i = 0; i < count; i++)
{
if (!children[i].node.IsDepthZero())
return false;
}
return true;
}
#if ENABLE_REGEX_CONFIG_OPTIONS
void CharTrie::Print(DebugWriter* w) const
{
w->Indent();
if (isAccepting)
w->PrintEOL(_u("<accept>"));
for (int i = 0; i < count; i++)
{
w->PrintQuotedChar(children[i].c);
w->EOL();
children[i].node.Print(w);
}
w->Unindent();
}
#endif
// ----------------------------------------------------------------------
// RuntimeCharTrie
// ----------------------------------------------------------------------
bool RuntimeCharTrie::Match
(const Char* const input
, const CharCount inputLength
, CharCount& inputOffset
#if ENABLE_REGEX_CONFIG_OPTIONS
, RegexStats* stats
#endif
) const
{
const RuntimeCharTrie* curr = this;
while (true)
{
if (curr->count == 0)
return true;
if (inputOffset >= inputLength)
return false;
#if ENABLE_REGEX_CONFIG_OPTIONS
if (stats != 0)
stats->numCompares++;
#endif
#if 0
int l = 0;
int h = curr->count - 1;
while (true)
{
if (l > h)
return false;
int m = (l + h) / 2;
if (curr->children[m].c == input[inputOffset])
{
inputOffset++;
curr = &curr->children[m].node;
break;
}
else if (CTU(curr->children[m].c) < CTU(input[inputOffset]))
l = m + 1;
else
h = m - 1;
}
#else
int i = 0;
while (true)
{
if (curr->children[i].c == input[inputOffset])
{
inputOffset++;
curr = &curr->children[i].node;
break;
}
else if (curr->children[i].c > input[inputOffset])
return false;
else if (++i >= curr->count)
return false;
}
#endif
}
}
void RuntimeCharTrie::FreeBody(ArenaAllocator* allocator)
{
for (int i = 0; i < count; i++)
children[i].node.FreeBody(allocator);
if (count > 0)
AdeleteArray(allocator, count, children);
#if DBG
count = 0;
children = 0;
#endif
}
void RuntimeCharTrie::CloneFrom(Js::ScriptContext* scriptContext, ArenaAllocator* allocator, const CharTrie& other)
{
PROBE_STACK_NO_DISPOSE(scriptContext, Js::Constants::MinStackRegex);
count = other.count;
if (count > 0)
{
children = AnewArray(allocator, RuntimeCharTrieEntry, count);
for (int i = 0; i < count; i++)
{
children[i].c = other.children[i].c;
children[i].node.CloneFrom(scriptContext, allocator, other.children[i].node);
}
}
else
children = 0;
}
#if ENABLE_REGEX_CONFIG_OPTIONS
void RuntimeCharTrie::Print(DebugWriter* w) const
{
w->Indent();
for (int i = 0; i < count; i++)
{
w->PrintQuotedChar(children[i].c);
w->EOL();
children[i].node.Print(w);
}
w->Unindent();
}
#endif
}