forked from google/re2j
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathMachineInput.java
More file actions
304 lines (264 loc) · 8.13 KB
/
Copy pathMachineInput.java
File metadata and controls
304 lines (264 loc) · 8.13 KB
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
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
/*
* Copyright (c) 2020 The Go Authors. All rights reserved.
*
* Use of this source code is governed by a BSD-style
* license that can be found in the LICENSE file.
*/
// Original Go source here:
// http://code.google.com/p/go/source/browse/src/pkg/regexp/regexp.go
package com.google.re2j;
/**
* MachineInput abstracts different representations of the input text supplied to the Machine. It
* provides one-character lookahead.
*/
abstract class MachineInput {
static final int EOF = (-1 << 3);
static MachineInput fromUTF8(byte[] b) {
return new UTF8Input(b);
}
static MachineInput fromUTF8(byte[] b, int start, int end) {
return new UTF8Input(b, start, end);
}
static MachineInput fromUTF16(CharSequence s) {
return new UTF16Input(s, 0, s.length());
}
static MachineInput fromUTF16(CharSequence s, int start, int end) {
return new UTF16Input(s, start, end);
}
//// Interface
// Returns the rune at the specified index; the units are
// unspecified, but could be UTF-8 byte, UTF-16 char, or rune
// indices. Returns the width (in the same units) of the rune in
// the lower 3 bits, and the rune (Unicode code point) in the high
// bits. Never negative, except for EOF which is represented as -1
// << 3 | 0.
abstract int step(int pos);
// can we look ahead without losing info?
abstract boolean canCheckPrefix();
// Returns the index relative to |pos| at which |re2.prefix| is found
// in this input stream, or a negative value if not found.
abstract int index(RE2 re2, int pos);
// Returns a bitmask of EMPTY_* flags.
abstract int context(int pos);
// Returns a bitmask of EMPTY_* flags.
abstract int context(int pos, boolean boundaryUnicode);
// Returns the end position in the same units as step().
abstract int endPos();
//// Implementations
// An implementation of MachineInput for UTF-8 byte arrays.
// |pos| and |width| are byte indices.
private static class UTF8Input extends MachineInput {
final byte[] b;
final int start;
final int end;
UTF8Input(byte[] b) {
this.b = b;
start = 0;
end = b.length;
}
UTF8Input(byte[] b, int start, int end) {
if (end > b.length) {
throw new ArrayIndexOutOfBoundsException(
"end is greater than length: " + end + " > " + b.length);
}
this.b = b;
this.start = start;
this.end = end;
}
@Override
int step(int i) {
i += start;
if (i >= end) {
return EOF;
}
// UTF-8. RFC 3629 in five lines:
//
// Unicode code points UTF-8 encoding (binary)
// 00-7F (7 bits) 0tuvwxyz
// 0080-07FF (11 bits) 110pqrst 10uvwxyz
// 0800-FFFF (16 bits) 1110jklm 10npqrst 10uvwxyz
// 010000-10FFFF (21 bits) 11110efg 10hijklm 10npqrst 10uvwxyz
int x = b[i++] & 0xff; // zero extend
if ((x & 0x80) == 0) {
return x << 3 | 1;
} else if ((x & 0xE0) == 0xC0) { // 110xxxxx
x = x & 0x1F;
if (i >= end) {
return EOF;
}
x = x << 6 | (b[i++] & 0x3F);
return x << 3 | 2;
} else if ((x & 0xF0) == 0xE0) { // 1110xxxx
x = x & 0x0F;
if (i + 1 >= end) {
return EOF;
}
x = x << 6 | (b[i++] & 0x3F);
x = x << 6 | (b[i++] & 0x3F);
return x << 3 | 3;
} else { // 11110xxx
x = x & 0x07;
if (i + 2 >= end) {
return EOF;
}
x = x << 6 | (b[i++] & 0x3F);
x = x << 6 | (b[i++] & 0x3F);
x = x << 6 | (b[i++] & 0x3F);
return x << 3 | 4;
}
}
@Override
boolean canCheckPrefix() {
return true;
}
@Override
int index(RE2 re2, int pos) {
pos += start;
int i = Utils.indexOf(b, re2.prefixUTF8, pos);
return i < 0 ? i : i - pos;
}
@Override
int context(int pos) {
pos += this.start;
int r1 = -1;
if (pos > this.start && pos <= this.end) {
int start = pos - 1;
r1 = b[start--];
if (r1 >= 0x80) { // decode UTF-8
// Find start, up to 4 bytes earlier.
int lim = pos - 4;
if (lim < this.start) {
lim = this.start;
}
while (start >= lim && (b[start] & 0xC0) == 0x80) { // 10xxxxxx
start--;
}
if (start < this.start) {
start = this.start;
}
r1 = step(start) >> 3;
}
}
int r2 = pos < this.end ? (step(pos) >> 3) : -1;
return Utils.emptyOpContext(r1, r2);
}
@Override
int context(int pos, boolean boundaryUnicode) {
pos += this.start;
int r1 = -1;
if (pos > this.start && pos <= this.end) {
int start = pos - 1;
r1 = b[start--];
if (r1 >= 0x80) { // decode UTF-8
// Find start, up to 4 bytes earlier.
int lim = pos - 4;
if (lim < this.start) {
lim = this.start;
}
while (start >= lim && (b[start] & 0xC0) == 0x80) { // 10xxxxxx
start--;
}
if (start < this.start) {
start = this.start;
}
r1 = step(start) >> 3;
}
}
int r2 = pos < this.end ? (step(pos) >> 3) : -1;
return Utils.emptyOpContext(r1, r2, boundaryUnicode);
}
@Override
int endPos() {
return end;
}
}
// |pos| and |width| are in Java "char" units.
private static class UTF16Input extends MachineInput {
final CharSequence str;
final int start;
final int end;
public UTF16Input(CharSequence str, int start, int end) {
this.str = str;
this.start = start;
this.end = end;
}
@Override
int step(int pos) {
pos += start;
if (pos < end) {
int rune = Character.codePointAt(str, pos);
return rune << 3 | Character.charCount(rune);
} else {
return EOF;
}
}
@Override
boolean canCheckPrefix() {
return true;
}
@Override
int index(RE2 re2, int pos) {
pos += start;
int i = indexOf(str, re2.prefix, pos);
return i < 0 ? i : i - pos;
}
@Override
int context(int pos) {
pos += start;
int r1 = pos > 0 && pos <= str.length() ? Character.codePointBefore(str, pos) : -1;
int r2 = pos < str.length() ? Character.codePointAt(str, pos) : -1;
return Utils.emptyOpContext(r1, r2);
}
@Override
int context(int pos, boolean boundaryUnicode) {
pos += start;
int r1 = pos > 0 && pos <= str.length() ? Character.codePointBefore(str, pos) : -1;
int r2 = pos < str.length() ? Character.codePointAt(str, pos) : -1;
return Utils.emptyOpContext(r1, r2, boundaryUnicode);
}
@Override
int endPos() {
return end;
}
private int indexOf(CharSequence hayStack, String needle, int pos) {
if (hayStack instanceof String) {
return ((String) hayStack).indexOf(needle, pos);
}
if (hayStack instanceof StringBuilder) {
return ((StringBuilder) hayStack).indexOf(needle, pos);
}
return indexOfFallback(hayStack, needle, pos);
}
// Modified version of {@link String#indexOf(String) that allows a CharSequence.
private int indexOfFallback(CharSequence hayStack, String needle, int fromIndex) {
if (fromIndex >= hayStack.length()) {
return needle.isEmpty() ? 0 : -1;
}
if (fromIndex < 0) {
fromIndex = 0;
}
if (needle.isEmpty()) {
return fromIndex;
}
char first = needle.charAt(0);
int max = hayStack.length() - needle.length();
for (int i = fromIndex; i <= max; i++) {
/* Look for first character. */
if (hayStack.charAt(i) != first) {
while (++i <= max && hayStack.charAt(i) != first) {}
}
/* Found first character, now look at the rest of v2 */
if (i <= max) {
int j = i + 1;
int end = j + needle.length() - 1;
for (int k = 1; j < end && hayStack.charAt(j) == needle.charAt(k); j++, k++) {}
if (j == end) {
/* Found whole string. */
return i;
}
}
}
return -1;
}
}
}