-
Notifications
You must be signed in to change notification settings - Fork 37
/
SearchableString.java
320 lines (263 loc) · 9.18 KB
/
SearchableString.java
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
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
package com.blueconic.browscap.impl;
import java.util.BitSet;
import java.util.concurrent.atomic.AtomicInteger;
/**
* This class represents a searchable useragent strings. It relies and simple char arrays for low memory use and fast
* operations. It provided methods for finding substrings and provides caches for better performance.
*/
class SearchableString {
private static final int[] EMPTY = new int[0];
private static final int[][] SINGLE_VALUES = getSingleValues();
private final char[] myChars;
private final int[][] myIndices;
private final Cache myPrefixCache = new Cache();
private final Cache myPostfixCache = new Cache();
// Reusable buffer for findIndices
private final int[] myBuffer;
/**
* Creates a new instance for the specified string value.
* @param stringValue The user agent string
* @param maxIndex The number of unique literals
*/
SearchableString(final String stringValue, final int maxIndex) {
myChars = stringValue.toCharArray();
myIndices = new int[maxIndex][];
myBuffer = new int[myChars.length];
}
/**
* Returns the size of this instance.
* @return The size
*/
int getSize() {
return myChars.length;
}
/**
* Indicates whether this instance starts with the specified prefix.
* @param literal The prefix that should be tested
* @return <code>true</code> if the argument represents the prefix of this instance, <code>false</code> otherwise.
*/
boolean startsWith(final Literal literal) {
// Check whether the answer is already in the cache
final int index = literal.getIndex();
final Boolean cached = myPrefixCache.get(index);
if (cached != null) {
return cached.booleanValue();
}
// Get the answer and cache the result
final boolean result = literal.matches(myChars, 0);
myPrefixCache.set(index, result);
return result;
}
/**
* Indicates whether this instance ends with the specified postfix.
* @param literal The postfix that should be tested
* @return <code>true</code> if the argument represents the postfix of this instance, <code>false</code> otherwise.
*/
boolean endsWith(final Literal literal) {
// Check whether the answer is already in the cache
final int index = literal.getIndex();
final Boolean cached = myPostfixCache.get(index);
if (cached != null) {
return cached.booleanValue();
}
// Get the answer and cache the result
final boolean result = literal.matches(myChars, myChars.length - literal.getLength());
myPostfixCache.set(index, result);
return result;
}
/**
* Returns all indices where the literal argument can be found in this String. Results are cached for better
* performance.
* @param literal The string that should be found
* @return all indices where the literal argument can be found in this String.
*/
int[] getIndices(final Literal literal) {
// Check whether the answer is already in the cache
final int index = literal.getIndex();
final int[] cached = myIndices[index];
if (cached != null) {
return cached;
}
// Find all indices
final int[] values = findIndices(literal);
myIndices[index] = values;
return values;
}
/**
* Returns all indices where the literal argument can be found in this String.
* @param literal The string that should be found
* @return all indices where the literal argument can be found in this String.
*/
private int[] findIndices(final Literal literal) {
int count = 0;
final char s = literal.getFirstChar();
for (int i = 0; i < myChars.length; i++) {
// Check the first char for better performance and check the complete string
if ((myChars[i] == s || s == '?') && literal.matches(myChars, i)) {
// This index matches
myBuffer[count] = i;
count++;
}
}
// Check whether any match has been found
if (count == 0) {
return EMPTY;
}
// Use an existing array
if (count == 1 && myBuffer[0] < SINGLE_VALUES.length) {
final int index = myBuffer[0];
return SINGLE_VALUES[index];
}
// Copy the values
final int[] values = new int[count];
for (int i = 0; i < count; i++) {
values[i] = myBuffer[i];
}
return values;
}
/**
* {@inheritDoc}
*/
@Override
public String toString() {
return new String(myChars);
}
private static final int[][] getSingleValues() {
final int[][] result = new int[1024][];
for (int i = 0; i < result.length; i++) {
result[i] = new int[]{i};
}
return result;
}
/** Compact cache for boolean values. */
static class Cache {
// The boolean values
private final BitSet myValues = new BitSet();
// Indicates whether a value has been stored for an index
private final BitSet myIsKnown = new BitSet();
/**
* Gets the cached value for the specified index.
* @param index The index of the requested value
* @return The cached boolean value, or <code>null</code> if no value is present in the cache.
*/
Boolean get(final int index) {
// Check whether a true value has been set
if (myValues.get(index)) {
return Boolean.TRUE;
}
// Check whether any value has been stored
if (myIsKnown.get(index)) {
return Boolean.FALSE;
}
// No value found
return null;
}
/**
* Set the value in the cache.
* @param index The index for the stored value
* @param flag The actual value
*/
void set(final int index, final boolean flag) {
// Store the value
myValues.set(index, flag);
// Store the fact the value has been stored
myIsKnown.set(index, true);
}
}
}
/**
* This combines a String value with a unique int value. The int value is used for caching of results.
*/
class Literal {
// The actual string data
private final char[] myCharacters;
// The unique index for this instance
private final int myIndex;
/**
* Creates a new instance with the specified non-empty value.
* @param value The String value
* @param index The unique index for this instance
*/
Literal(final String value, final int index) {
myCharacters = value.toCharArray();
myIndex = index;
}
/**
* Returns the first character for quick checks.
* @return the first character
*/
char getFirstChar() {
return myCharacters[0];
}
/**
* Returns the size of this instance.
* @return The size of this instance
*/
int getLength() {
return myCharacters.length;
}
/**
* Checks whether the value represents a complete substring from the from index.
* @param from The start index of the potential substring
* @return <code>true</code> If the arguments represent a valid substring, <code>false</code> otherwise.
*/
boolean matches(final char[] value, final int from) {
// Check the bounds
final int len = myCharacters.length;
if (len + from > value.length || from < 0) {
return false;
}
// Bounds are ok, check all characters.
// Allow question marks to match any character
for (int i = 0; i < len; i++) {
if (myCharacters[i] != value[i + from] && myCharacters[i] != '?') {
return false;
}
}
// All characters match
return true;
}
/**
* Returns the unique index of this instance.
* @return the unique index of this instance.
*/
final int getIndex() {
return myIndex;
}
private static boolean contains(final char[] characters, final char value) {
for (final char c : characters) {
if (c == value) {
return true;
}
}
return false;
}
boolean requires(final String value) {
final int len = value.length();
if (len == 1) {
return contains(myCharacters, value.charAt(0));
}
if (len > myCharacters.length) {
return false;
}
return toString().contains(value);
}
/**
* {@inheritDoc}
*/
@Override
public String toString() {
return new String(myCharacters);
}
}
class LiteralDomain {
// Keep track of the total number of instances
private final AtomicInteger myNrOfInstances = new AtomicInteger();
Literal createLiteral(final String contents) {
return new Literal(contents, myNrOfInstances.getAndAdd(1));
}
SearchableString getSearchableString(final String contents) {
final int maxIndex = myNrOfInstances.get() + 1;
return new SearchableString(contents, maxIndex);
}
}