-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathFastStringBuffer.java
More file actions
464 lines (405 loc) · 12.1 KB
/
Copy pathFastStringBuffer.java
File metadata and controls
464 lines (405 loc) · 12.1 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
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
package com.jetbrains.python.console.pydev;
import java.util.Iterator;
/**
* This is a custom string buffer optimized for append(), clear() and deleteLast().
*
* Basically it aims at being created once, being used for something, having clear() called and then reused
* (ultimately providing minimum allocation/garbage collection overhead for that use-case).
*
* append() is optimizing by doing less checks (so, exceptions thrown may be uglier on invalid operations
* and null is not checked for in the common case -- use appendObject if it may be null).
*
* clear() and deleteLast() only change the internal count and have almost zero overhead.
*
* Note that it's also not synchronized.
*
* @author Fabio
*/
public final class FastStringBuffer {
/**
* Holds the actual chars
*/
private char[] value;
/**
* Count for which chars are actually used
*/
private int count;
/**
* Initializes with a default initial size (128 chars)
*/
public FastStringBuffer() {
this(128);
}
/**
* An initial size can be specified (if available and given for no allocations it can be more efficient)
*/
public FastStringBuffer(int initialSize) {
this.value = new char[initialSize];
this.count = 0;
}
/**
* initializes from a string and the additional size for the buffer
*
* @param s string with the initial contents
* @param additionalSize the additional size for the buffer
*/
public FastStringBuffer(String s, int additionalSize) {
this.count = s.length();
value = new char[this.count + additionalSize];
s.getChars(0, this.count, value, 0);
}
/**
* Appends a string to the buffer. Passing a null string will throw an exception.
*/
public FastStringBuffer append(String string) {
int strLen = string.length();
int newCount = count + strLen;
if (newCount > this.value.length) {
resizeForMinimum(newCount);
}
string.getChars(0, strLen, value, this.count);
this.count = newCount;
return this;
}
/**
* Resizes the internal buffer to have at least the minimum capacity passed (but may be more)
*/
private void resizeForMinimum(int minimumCapacity) {
int newCapacity = (value.length + 1) * 2;
if (minimumCapacity > newCapacity) {
newCapacity = minimumCapacity;
}
char newValue[] = new char[newCapacity];
System.arraycopy(value, 0, newValue, 0, count);
value = newValue;
}
/**
* Appends an int to the buffer.
*/
public FastStringBuffer append(int n) {
append(String.valueOf(n));
return this;
}
/**
* Appends a char to the buffer.
*/
public FastStringBuffer append(char n) {
if (count + 1 > value.length) {
resizeForMinimum(count + 1);
}
value[count] = n;
count++;
return this;
}
/**
* Appends a long to the buffer.
*/
public FastStringBuffer append(long n) {
append(String.valueOf(n));
return this;
}
/**
* Appends a boolean to the buffer.
*/
public FastStringBuffer append(boolean b) {
append(String.valueOf(b));
return this;
}
/**
* Appends an array of chars to the buffer.
*/
public FastStringBuffer append(char[] chars) {
int newCount = count + chars.length;
if (newCount > value.length) {
resizeForMinimum(newCount);
}
System.arraycopy(chars, 0, value, count, chars.length);
count = newCount;
return this;
}
/**
* Appends another buffer to this buffer.
*/
public FastStringBuffer append(FastStringBuffer other) {
append(other.value, 0, other.count);
return this;
}
/**
* Appends an array of chars to this buffer, starting at the offset passed with the length determined.
*/
public FastStringBuffer append(char[] chars, int offset, int len) {
int newCount = count + len;
if (newCount > value.length) {
resizeForMinimum(newCount);
}
System.arraycopy(chars, offset, value, count, len);
count = newCount;
return this;
}
/**
* Reverses the contents on this buffer
*/
public FastStringBuffer reverse() {
int limit = count / 2;
for (int i = 0; i < limit; ++i) {
char c = value[i];
value[i] = value[count - i - 1];
value[count - i - 1] = c;
}
return this;
}
/**
* Clears this buffer.
*/
public FastStringBuffer clear() {
this.count = 0;
return this;
}
/**
* @return the length of this buffer
*/
public int length() {
return this.count;
}
/**
* @return a new string with the contents of this buffer.
*/
@Override
public String toString() {
return new String(value, 0, count);
}
/**
* @return a new char array with the contents of this buffer.
*/
public char[] toCharArray() {
char[] v = new char[count];
System.arraycopy(value, 0, v, 0, count);
return v;
}
/**
* Erases the last char in this buffer
*/
public void deleteLast() {
if (this.count > 0) {
this.count--;
}
}
/**
* @return the char given at a specific position of the buffer (no bounds check)
*/
public char charAt(int i) {
return this.value[i];
}
/**
* Inserts a string at a given position in the buffer.
*/
public FastStringBuffer insert(int offset, String str) {
int len = str.length();
int newCount = count + len;
if (newCount > value.length) {
resizeForMinimum(newCount);
}
System.arraycopy(value, offset, value, offset + len, count - offset);
str.getChars(0, len, value, offset);
count = newCount;
return this;
}
/**
* Inserts a char at a given position in the buffer.
*/
public FastStringBuffer insert(int offset, char c) {
int newCount = count + 1;
if (newCount > value.length) {
resizeForMinimum(newCount);
}
System.arraycopy(value, offset, value, offset + 1, count - offset);
value[offset] = c;
count = newCount;
return this;
}
/**
* Appends object.toString(). If null, "null" is appended.
*/
public FastStringBuffer appendObject(Object object) {
return append(object != null ? object.toString() : "null");
}
/**
* Sets the new size of this buffer (warning: use with care: no validation is done of the len passed)
*/
public void setCount(int newLen) {
this.count = newLen;
}
public FastStringBuffer delete(int start, int end) {
if (start < 0)
throw new StringIndexOutOfBoundsException(start);
if (end > count)
end = count;
if (start > end)
throw new StringIndexOutOfBoundsException();
int len = end - start;
if (len > 0) {
System.arraycopy(value, start + len, value, start, count - end);
count -= len;
}
return this;
}
public FastStringBuffer replace(int start, int end, String str) {
if (start < 0)
throw new StringIndexOutOfBoundsException(start);
if (start > count)
throw new StringIndexOutOfBoundsException("start > length()");
if (start > end)
throw new StringIndexOutOfBoundsException("start > end");
if (end > count)
end = count;
if (end > count)
end = count;
int len = str.length();
int newCount = count + len - (end - start);
if (newCount > value.length) {
resizeForMinimum(newCount);
}
System.arraycopy(value, end, value, start + len, count - end);
str.getChars(0, len, value, start);
count = newCount;
return this;
}
/**
* Replaces all the occurrences of a string in this buffer for another string and returns the
* altered version.
*/
public FastStringBuffer replaceAll(String replace, String with) {
int replaceLen = replace.length();
int withLen = with.length();
int matchPos = 0;
for (int i = 0; i < this.count; i++) {
if(this.value[i] == replace.charAt(matchPos)){
matchPos ++;
if(matchPos == replaceLen){
this.replace(i-(replaceLen-1), i+1, with);
matchPos = 0;
i = i-(replaceLen-withLen);
}
continue;
}else{
matchPos = 0;
}
}
return this;
}
public FastStringBuffer deleteCharAt(int index) {
if ((index < 0) || (index >= count)) {
throw new StringIndexOutOfBoundsException(index);
}
System.arraycopy(value, index + 1, value, index, count - index - 1);
count--;
return this;
}
public int indexOf(char c) {
for(int i=0;i<this.count;i++){
if(c == this.value[i]){
return i;
}
}
return -1;
}
public char firstChar() {
return this.value[0];
}
public char lastChar() {
return this.value[this.count-1];
}
public final static class BackwardCharIterator implements Iterable<Character>{
private int i;
private FastStringBuffer fastStringBuffer;
public BackwardCharIterator(FastStringBuffer fastStringBuffer) {
this.fastStringBuffer = fastStringBuffer;
i = fastStringBuffer.length();
}
@Override
public Iterator<Character> iterator() {
return new Iterator<>(){
@Override
public boolean hasNext() {
return i > 0;
}
@Override
public Character next() {
return fastStringBuffer.value[--i];
}
@Override
public void remove() {
throw new RuntimeException("Not implemented");
}
};
}
}
public BackwardCharIterator reverseIterator() {
return new BackwardCharIterator(this);
}
public void rightTrim() {
char c;
while(((c=this.lastChar()) == ' ' || c == '\t' )){
this.deleteLast();
}
}
public char deleteFirst(){
char ret = this.value[0];
this.deleteCharAt(0);
return ret;
}
public FastStringBuffer appendN(String val, int n){
int min = count + (n*val.length());
if (min > value.length) {
resizeForMinimum(min);
}
int strLen = val.length();
while (n-- > 0){
val.getChars(0, strLen, value, this.count);
this.count += strLen;
}
return this;
}
public FastStringBuffer appendN(char val, int n){
if (count + n > value.length) {
resizeForMinimum(count + n);
}
while (n-- > 0){
value[count] = val;
count++;
}
return this;
}
public boolean endsWith(String string) {
return startsWith(string, count - string.length());
}
public boolean startsWith(String prefix) {
return startsWith(prefix, 0);
}
public boolean startsWith(String prefix, int offset) {
char ta[] = value;
int to = offset;
char pa[] = prefix.toCharArray();
int po = 0;
int pc = pa.length;
// Note: toffset might be near -1>>>1.
if ((offset < 0) || (offset > count - pc)) {
return false;
}
while (--pc >= 0) {
if (ta[to++] != pa[po++]) {
return false;
}
}
return true;
}
public void setCharAt(int i, char c) {
this.value[i] = c;
}
/**
* Careful: it doesn't check anything. Just sets the internal length.
*/
public void setLength(int i) {
this.count = i;
}
}