-
Notifications
You must be signed in to change notification settings - Fork 6
/
test_input.txt
1692 lines (1601 loc) · 57.9 KB
/
test_input.txt
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
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
public StrBuilder appendSeparator(String separator) {
return appendSeparator(separator, null);
}
/**
* Appends one of both separators to the StrBuilder.
* If the builder is currently empty it will append the defaultIfEmpty-separator
* Otherwise it will append the standard-separator
*
* Appending a null separator will have no effect.
* The separator is appended using {@link #append(String)}.
* <p>
* This method is for example useful for constructing queries
* <pre>
* StrBuilder whereClause = new StrBuilder();
* if(searchCommand.getPriority() != null) {
* whereClause.appendSeparator(" and", " where");
* whereClause.append(" priority = ?")
* }
* if(searchCommand.getComponent() != null) {
* whereClause.appendSeparator(" and", " where");
* whereClause.append(" component = ?")
* }
* selectClause.append(whereClause)
* </pre>
*
* @param standard the separator if builder is not empty, null means no separator
* @param defaultIfEmpty the separator if builder is empty, null means no separator
* @return this, to enable chaining
* @since 2.5
*/
public StrBuilder appendSeparator(String standard, String defaultIfEmpty) {
String str = isEmpty() ? defaultIfEmpty : standard;
if (str != null) {
append(str);
}
return this;
}
/**
* Appends a separator if the builder is currently non-empty.
* The separator is appended using {@link #append(char)}.
* <p>
* This method is useful for adding a separator each time around the
* loop except the first.
* <pre>
* for (Iterator it = list.iterator(); it.hasNext(); ) {
* appendSeparator(',');
* append(it.next());
* }
* </pre>
* Note that for this simple example, you should use
* {@link #appendWithSeparators(Iterable, String)}.
*
* @param separator the separator to use
* @return this, to enable chaining
* @since 2.3
*/
public StrBuilder appendSeparator(char separator) {
if (size() > 0) {
append(separator);
}
return this;
}
/**
* Append one of both separators to the builder
* If the builder is currently empty it will append the defaultIfEmpty-separator
* Otherwise it will append the standard-separator
*
* The separator is appended using {@link #append(char)}.
* @param standard the separator if builder is not empty
* @param defaultIfEmpty the separator if builder is empty
* @return this, to enable chaining
* @since 2.5
*/
public StrBuilder appendSeparator(char standard, char defaultIfEmpty) {
if (size() > 0) {
append(standard);
}
else {
append(defaultIfEmpty);
}
return this;
}
/**
* Appends a separator to the builder if the loop index is greater than zero.
* Appending a null separator will have no effect.
* The separator is appended using {@link #append(String)}.
* <p>
* This method is useful for adding a separator each time around the
* loop except the first.
* <pre>
* for (int i = 0; i < list.size(); i++) {
* appendSeparator(",", i);
* append(list.get(i));
* }
* </pre>
* Note that for this simple example, you should use
* {@link #appendWithSeparators(Iterable, String)}.
*
* @param separator the separator to use, null means no separator
* @param loopIndex the loop index
* @return this, to enable chaining
* @since 2.3
*/
public StrBuilder appendSeparator(String separator, int loopIndex) {
if (separator != null && loopIndex > 0) {
append(separator);
}
return this;
}
/**
* Appends a separator to the builder if the loop index is greater than zero.
* The separator is appended using {@link #append(char)}.
* <p>
* This method is useful for adding a separator each time around the
* loop except the first.
* <pre>
* for (int i = 0; i < list.size(); i++) {
* appendSeparator(",", i);
* append(list.get(i));
* }
* </pre>
* Note that for this simple example, you should use
* {@link #appendWithSeparators(Iterable, String)}.
*
* @param separator the separator to use
* @param loopIndex the loop index
* @return this, to enable chaining
* @since 2.3
*/
public StrBuilder appendSeparator(char separator, int loopIndex) {
if (loopIndex > 0) {
append(separator);
}
return this;
}
//-----------------------------------------------------------------------
/**
* Appends the pad character to the builder the specified number of times.
*
* @param length the length to append, negative means no append
* @param padChar the character to append
* @return this, to enable chaining
*/
public StrBuilder appendPadding(int length, char padChar) {
if (length >= 0) {
ensureCapacity(size + length);
for (int i = 0; i < length; i++) {
buffer[size++] = padChar;
}
}
return this;
}
//-----------------------------------------------------------------------
/**
* Appends an object to the builder padding on the left to a fixed width.
* The <code>toString</code> of the object is used.
* If the object is larger than the length, the left hand side is lost.
* If the object is null, the null text value is used.
*
* @param obj the object to append, null uses null text
* @param width the fixed field width, zero or negative has no effect
* @param padChar the pad character to use
* @return this, to enable chaining
*/
public StrBuilder appendFixedWidthPadLeft(Object obj, int width, char padChar) {
if (width > 0) {
ensureCapacity(size + width);
String str = (obj == null ? getNullText() : obj.toString());
if (str == null) {
str = "";
}
int strLen = str.length();
if (strLen >= width) {
str.getChars(strLen - width, strLen, buffer, size);
} else {
int padLen = width - strLen;
for (int i = 0; i < padLen; i++) {
buffer[size + i] = padChar;
}
str.getChars(0, strLen, buffer, size + padLen);
}
size += width;
}
return this;
}
/**
* Appends an object to the builder padding on the left to a fixed width.
* The <code>String.valueOf</code> of the <code>int</code> value is used.
* If the formatted value is larger than the length, the left hand side is lost.
*
* @param value the value to append
* @param width the fixed field width, zero or negative has no effect
* @param padChar the pad character to use
* @return this, to enable chaining
*/
public StrBuilder appendFixedWidthPadLeft(int value, int width, char padChar) {
return appendFixedWidthPadLeft(String.valueOf(value), width, padChar);
}
/**
* Appends an object to the builder padding on the right to a fixed length.
* The <code>toString</code> of the object is used.
* If the object is larger than the length, the right hand side is lost.
* If the object is null, null text value is used.
*
* @param obj the object to append, null uses null text
* @param width the fixed field width, zero or negative has no effect
* @param padChar the pad character to use
* @return this, to enable chaining
*/
public StrBuilder appendFixedWidthPadRight(Object obj, int width, char padChar) {
if (width > 0) {
ensureCapacity(size + width);
String str = (obj == null ? getNullText() : obj.toString());
if (str == null) {
str = "";
}
int strLen = str.length();
if (strLen >= width) {
str.getChars(0, width, buffer, size);
} else {
int padLen = width - strLen;
str.getChars(0, strLen, buffer, size);
for (int i = 0; i < padLen; i++) {
buffer[size + strLen + i] = padChar;
}
}
size += width;
}
return this;
}
/**
* Appends an object to the builder padding on the right to a fixed length.
* The <code>String.valueOf</code> of the <code>int</code> value is used.
* If the object is larger than the length, the right hand side is lost.
*
* @param value the value to append
* @param width the fixed field width, zero or negative has no effect
* @param padChar the pad character to use
* @return this, to enable chaining
*/
public StrBuilder appendFixedWidthPadRight(int value, int width, char padChar) {
return appendFixedWidthPadRight(String.valueOf(value), width, padChar);
}
//-----------------------------------------------------------------------
/**
* Inserts the string representation of an object into this builder.
* Inserting null will use the stored null text value.
*
* @param index the index to add at, must be valid
* @param obj the object to insert
* @return this, to enable chaining
* @throws IndexOutOfBoundsException if the index is invalid
*/
public StrBuilder insert(int index, Object obj) {
if (obj == null) {
return insert(index, nullText);
}
return insert(index, obj.toString());
}
/**
* Inserts the string into this builder.
* Inserting null will use the stored null text value.
*
* @param index the index to add at, must be valid
* @param str the string to insert
* @return this, to enable chaining
* @throws IndexOutOfBoundsException if the index is invalid
*/
@SuppressWarnings("null") // str cannot be null
public StrBuilder insert(int index, String str) {
validateIndex(index);
if (str == null) {
str = nullText;
}
int strLen = (str == null ? 0 : str.length());
if (strLen > 0) {
int newSize = size + strLen;
ensureCapacity(newSize);
System.arraycopy(buffer, index, buffer, index + strLen, size - index);
size = newSize;
str.getChars(0, strLen, buffer, index); // str cannot be null here
}
return this;
}
/**
* Inserts the character array into this builder.
* Inserting null will use the stored null text value.
*
* @param index the index to add at, must be valid
* @param chars the char array to insert
* @return this, to enable chaining
* @throws IndexOutOfBoundsException if the index is invalid
*/
public StrBuilder insert(int index, char chars[]) {
validateIndex(index);
if (chars == null) {
return insert(index, nullText);
}
int len = chars.length;
if (len > 0) {
ensureCapacity(size + len);
System.arraycopy(buffer, index, buffer, index + len, size - index);
System.arraycopy(chars, 0, buffer, index, len);
size += len;
}
return this;
}
/**
* Inserts part of the character array into this builder.
* Inserting null will use the stored null text value.
*
* @param index the index to add at, must be valid
* @param chars the char array to insert
* @param offset the offset into the character array to start at, must be valid
* @param length the length of the character array part to copy, must be positive
* @return this, to enable chaining
* @throws IndexOutOfBoundsException if any index is invalid
*/
public StrBuilder insert(int index, char chars[], int offset, int length) {
validateIndex(index);
if (chars == null) {
return insert(index, nullText);
}
if (offset < 0 || offset > chars.length) {
throw new StringIndexOutOfBoundsException("Invalid offset: " + offset);
}
if (length < 0 || offset + length > chars.length) {
throw new StringIndexOutOfBoundsException("Invalid length: " + length);
}
if (length > 0) {
ensureCapacity(size + length);
System.arraycopy(buffer, index, buffer, index + length, size - index);
System.arraycopy(chars, offset, buffer, index, length);
size += length;
}
return this;
}
/**
* Inserts the value into this builder.
*
* @param index the index to add at, must be valid
* @param value the value to insert
* @return this, to enable chaining
* @throws IndexOutOfBoundsException if the index is invalid
*/
public StrBuilder insert(int index, boolean value) {
validateIndex(index);
if (value) {
ensureCapacity(size + 4);
System.arraycopy(buffer, index, buffer, index + 4, size - index);
buffer[index++] = 't';
buffer[index++] = 'r';
buffer[index++] = 'u';
buffer[index] = 'e';
size += 4;
} else {
ensureCapacity(size + 5);
System.arraycopy(buffer, index, buffer, index + 5, size - index);
buffer[index++] = 'f';
buffer[index++] = 'a';
buffer[index++] = 'l';
buffer[index++] = 's';
buffer[index] = 'e';
size += 5;
}
return this;
}
/**
* Inserts the value into this builder.
*
* @param index the index to add at, must be valid
* @param value the value to insert
* @return this, to enable chaining
* @throws IndexOutOfBoundsException if the index is invalid
*/
public StrBuilder insert(int index, char value) {
validateIndex(index);
ensureCapacity(size + 1);
System.arraycopy(buffer, index, buffer, index + 1, size - index);
buffer[index] = value;
size++;
return this;
}
/**
* Inserts the value into this builder.
*
* @param index the index to add at, must be valid
* @param value the value to insert
* @return this, to enable chaining
* @throws IndexOutOfBoundsException if the index is invalid
*/
public StrBuilder insert(int index, int value) {
return insert(index, String.valueOf(value));
}
/**
* Inserts the value into this builder.
*
* @param index the index to add at, must be valid
* @param value the value to insert
* @return this, to enable chaining
* @throws IndexOutOfBoundsException if the index is invalid
*/
public StrBuilder insert(int index, long value) {
return insert(index, String.valueOf(value));
}
/**
* Inserts the value into this builder.
*
* @param index the index to add at, must be valid
* @param value the value to insert
* @return this, to enable chaining
* @throws IndexOutOfBoundsException if the index is invalid
*/
public StrBuilder insert(int index, float value) {
return insert(index, String.valueOf(value));
}
/**
* Inserts the value into this builder.
*
* @param index the index to add at, must be valid
* @param value the value to insert
* @return this, to enable chaining
* @throws IndexOutOfBoundsException if the index is invalid
*/
public StrBuilder insert(int index, double value) {
return insert(index, String.valueOf(value));
}
//-----------------------------------------------------------------------
/**
* Internal method to delete a range without validation.
*
* @param startIndex the start index, must be valid
* @param endIndex the end index (exclusive), must be valid
* @param len the length, must be valid
* @throws IndexOutOfBoundsException if any index is invalid
*/
private void deleteImpl(int startIndex, int endIndex, int len) {
System.arraycopy(buffer, endIndex, buffer, startIndex, size - endIndex);
size -= len;
}
/**
* Deletes the characters between the two specified indices.
*
* @param startIndex the start index, inclusive, must be valid
* @param endIndex the end index, exclusive, must be valid except
* that if too large it is treated as end of string
* @return this, to enable chaining
* @throws IndexOutOfBoundsException if the index is invalid
*/
public StrBuilder delete(int startIndex, int endIndex) {
endIndex = validateRange(startIndex, endIndex);
int len = endIndex - startIndex;
if (len > 0) {
deleteImpl(startIndex, endIndex, len);
}
return this;
}
//-----------------------------------------------------------------------
/**
* Deletes the character wherever it occurs in the builder.
*
* @param ch the character to delete
* @return this, to enable chaining
*/
public StrBuilder deleteAll(char ch) {
for (int i = 0; i < size; i++) {
if (buffer[i] == ch) {
int start = i;
while (++i < size) {
if (buffer[i] != ch) {
break;
}
}
int len = i - start;
deleteImpl(start, i, len);
i -= len;
}
}
return this;
}
/**
* Deletes the character wherever it occurs in the builder.
*
* @param ch the character to delete
* @return this, to enable chaining
*/
public StrBuilder deleteFirst(char ch) {
for (int i = 0; i < size; i++) {
if (buffer[i] == ch) {
deleteImpl(i, i + 1, 1);
break;
}
}
return this;
}
//-----------------------------------------------------------------------
/**
* Deletes the string wherever it occurs in the builder.
*
* @param str the string to delete, null causes no action
* @return this, to enable chaining
*/
public StrBuilder deleteAll(String str) {
int len = (str == null ? 0 : str.length());
if (len > 0) {
int index = indexOf(str, 0);
while (index >= 0) {
deleteImpl(index, index + len, len);
index = indexOf(str, index);
}
}
return this;
}
/**
* Deletes the string wherever it occurs in the builder.
*
* @param str the string to delete, null causes no action
* @return this, to enable chaining
*/
public StrBuilder deleteFirst(String str) {
int len = (str == null ? 0 : str.length());
if (len > 0) {
int index = indexOf(str, 0);
if (index >= 0) {
deleteImpl(index, index + len, len);
}
}
return this;
}
//-----------------------------------------------------------------------
/**
* Deletes all parts of the builder that the matcher matches.
* <p>
* Matchers can be used to perform advanced deletion behaviour.
* For example you could write a matcher to delete all occurances
* where the character 'a' is followed by a number.
*
* @param matcher the matcher to use to find the deletion, null causes no action
* @return this, to enable chaining
*/
public StrBuilder deleteAll(StrMatcher matcher) {
return replace(matcher, null, 0, size, -1);
}
/**
* Deletes the first match within the builder using the specified matcher.
* <p>
* Matchers can be used to perform advanced deletion behaviour.
* For example you could write a matcher to delete
* where the character 'a' is followed by a number.
*
* @param matcher the matcher to use to find the deletion, null causes no action
* @return this, to enable chaining
*/
public StrBuilder deleteFirst(StrMatcher matcher) {
return replace(matcher, null, 0, size, 1);
}
//-----------------------------------------------------------------------
/**
* Internal method to delete a range without validation.
*
* @param startIndex the start index, must be valid
* @param endIndex the end index (exclusive), must be valid
* @param removeLen the length to remove (endIndex - startIndex), must be valid
* @param insertStr the string to replace with, null means delete range
* @param insertLen the length of the insert string, must be valid
* @throws IndexOutOfBoundsException if any index is invalid
*/
private void replaceImpl(int startIndex, int endIndex, int removeLen, String insertStr, int insertLen) {
int newSize = size - removeLen + insertLen;
if (insertLen != removeLen) {
ensureCapacity(newSize);
System.arraycopy(buffer, endIndex, buffer, startIndex + insertLen, size - endIndex);
size = newSize;
}
if (insertLen > 0) {
insertStr.getChars(0, insertLen, buffer, startIndex);
}
}
/**
* Replaces a portion of the string builder with another string.
* The length of the inserted string does not have to match the removed length.
*
* @param startIndex the start index, inclusive, must be valid
* @param endIndex the end index, exclusive, must be valid except
* that if too large it is treated as end of string
* @param replaceStr the string to replace with, null means delete range
* @return this, to enable chaining
* @throws IndexOutOfBoundsException if the index is invalid
*/
public StrBuilder replace(int startIndex, int endIndex, String replaceStr) {
endIndex = validateRange(startIndex, endIndex);
int insertLen = (replaceStr == null ? 0 : replaceStr.length());
replaceImpl(startIndex, endIndex, endIndex - startIndex, replaceStr, insertLen);
return this;
}
//-----------------------------------------------------------------------
/**
* Replaces the search character with the replace character
* throughout the builder.
*
* @param search the search character
* @param replace the replace character
* @return this, to enable chaining
*/
public StrBuilder replaceAll(char search, char replace) {
if (search != replace) {
for (int i = 0; i < size; i++) {
if (buffer[i] == search) {
buffer[i] = replace;
}
}
}
return this;
}
/**
* Replaces the first instance of the search character with the
* replace character in the builder.
*
* @param search the search character
* @param replace the replace character
* @return this, to enable chaining
*/
public StrBuilder replaceFirst(char search, char replace) {
if (search != replace) {
for (int i = 0; i < size; i++) {
if (buffer[i] == search) {
buffer[i] = replace;
break;
}
}
}
return this;
}
//-----------------------------------------------------------------------
/**
* Replaces the search string with the replace string throughout the builder.
*
* @param searchStr the search string, null causes no action to occur
* @param replaceStr the replace string, null is equivalent to an empty string
* @return this, to enable chaining
*/
public StrBuilder replaceAll(String searchStr, String replaceStr) {
int searchLen = (searchStr == null ? 0 : searchStr.length());
if (searchLen > 0) {
int replaceLen = (replaceStr == null ? 0 : replaceStr.length());
int index = indexOf(searchStr, 0);
while (index >= 0) {
replaceImpl(index, index + searchLen, searchLen, replaceStr, replaceLen);
index = indexOf(searchStr, index + replaceLen);
}
}
return this;
}
/**
* Replaces the first instance of the search string with the replace string.
*
* @param searchStr the search string, null causes no action to occur
* @param replaceStr the replace string, null is equivalent to an empty string
* @return this, to enable chaining
*/
public StrBuilder replaceFirst(String searchStr, String replaceStr) {
int searchLen = (searchStr == null ? 0 : searchStr.length());
if (searchLen > 0) {
int index = indexOf(searchStr, 0);
if (index >= 0) {
int replaceLen = (replaceStr == null ? 0 : replaceStr.length());
replaceImpl(index, index + searchLen, searchLen, replaceStr, replaceLen);
}
}
return this;
}
//-----------------------------------------------------------------------
/**
* Replaces all matches within the builder with the replace string.
* <p>
* Matchers can be used to perform advanced replace behaviour.
* For example you could write a matcher to replace all occurances
* where the character 'a' is followed by a number.
*
* @param matcher the matcher to use to find the deletion, null causes no action
* @param replaceStr the replace string, null is equivalent to an empty string
* @return this, to enable chaining
*/
public StrBuilder replaceAll(StrMatcher matcher, String replaceStr) {
return replace(matcher, replaceStr, 0, size, -1);
}
/**
* Replaces the first match within the builder with the replace string.
* <p>
* Matchers can be used to perform advanced replace behaviour.
* For example you could write a matcher to replace
* where the character 'a' is followed by a number.
*
* @param matcher the matcher to use to find the deletion, null causes no action
* @param replaceStr the replace string, null is equivalent to an empty string
* @return this, to enable chaining
*/
public StrBuilder replaceFirst(StrMatcher matcher, String replaceStr) {
return replace(matcher, replaceStr, 0, size, 1);
}
// -----------------------------------------------------------------------
/**
* Advanced search and replaces within the builder using a matcher.
* <p>
* Matchers can be used to perform advanced behaviour.
* For example you could write a matcher to delete all occurances
* where the character 'a' is followed by a number.
*
* @param matcher the matcher to use to find the deletion, null causes no action
* @param replaceStr the string to replace the match with, null is a delete
* @param startIndex the start index, inclusive, must be valid
* @param endIndex the end index, exclusive, must be valid except
* that if too large it is treated as end of string
* @param replaceCount the number of times to replace, -1 for replace all
* @return this, to enable chaining
* @throws IndexOutOfBoundsException if start index is invalid
*/
public StrBuilder replace(
StrMatcher matcher, String replaceStr,
int startIndex, int endIndex, int replaceCount) {
endIndex = validateRange(startIndex, endIndex);
return replaceImpl(matcher, replaceStr, startIndex, endIndex, replaceCount);
}
/**
* Replaces within the builder using a matcher.
* <p>
* Matchers can be used to perform advanced behaviour.
* For example you could write a matcher to delete all occurances
* where the character 'a' is followed by a number.
*
* @param matcher the matcher to use to find the deletion, null causes no action
* @param replaceStr the string to replace the match with, null is a delete
* @param from the start index, must be valid
* @param to the end index (exclusive), must be valid
* @param replaceCount the number of times to replace, -1 for replace all
* @return this, to enable chaining
* @throws IndexOutOfBoundsException if any index is invalid
*/
private StrBuilder replaceImpl(
StrMatcher matcher, String replaceStr,
int from, int to, int replaceCount) {
if (matcher == null || size == 0) {
return this;
}
int replaceLen = (replaceStr == null ? 0 : replaceStr.length());
char[] buf = buffer;
for (int i = from; i < to && replaceCount != 0; i++) {
int removeLen = matcher.isMatch(buf, i, from, to);
if (removeLen > 0) {
replaceImpl(i, i + removeLen, removeLen, replaceStr, replaceLen);
to = to - removeLen + replaceLen;
i = i + replaceLen - 1;
if (replaceCount > 0) {
replaceCount--;
}
}
}
return this;
}
//-----------------------------------------------------------------------
/**
* Reverses the string builder placing each character in the opposite index.
*
* @return this, to enable chaining
*/
public StrBuilder reverse() {
if (size == 0) {
return this;
}
int half = size / 2;
char[] buf = buffer;
for (int leftIdx = 0, rightIdx = size - 1; leftIdx < half; leftIdx++,rightIdx--) {
char swap = buf[leftIdx];
buf[leftIdx] = buf[rightIdx];
buf[rightIdx] = swap;
}
return this;
}
//-----------------------------------------------------------------------
/**
* Trims the builder by removing characters less than or equal to a space
* from the beginning and end.
*
* @return this, to enable chaining
*/
public StrBuilder trim() {
if (size == 0) {
return this;
}
int len = size;
char[] buf = buffer;
int pos = 0;
while (pos < len && buf[pos] <= ' ') {
pos++;
}
while (pos < len && buf[len - 1] <= ' ') {
len--;
}
if (len < size) {
delete(len, size);
}
if (pos > 0) {
delete(0, pos);
}
return this;
}
//-----------------------------------------------------------------------
/**
* Checks whether this builder starts with the specified string.
* <p>
* Note that this method handles null input quietly, unlike String.
*
* @param str the string to search for, null returns false
* @return true if the builder starts with the string
*/
public boolean startsWith(String str) {
if (str == null) {
return false;
}
int len = str.length();
if (len == 0) {
return true;
}
if (len > size) {
return false;
}
for (int i = 0; i < len; i++) {
if (buffer[i] != str.charAt(i)) {
return false;
}
}
return true;
}
/**
* Checks whether this builder ends with the specified string.
* <p>
* Note that this method handles null input quietly, unlike String.
*
* @param str the string to search for, null returns false
* @return true if the builder ends with the string
*/
public boolean endsWith(String str) {
if (str == null) {
return false;
}
int len = str.length();
if (len == 0) {
return true;
}
if (len > size) {
return false;
}
int pos = size - len;
for (int i = 0; i < len; i++,pos++) {
if (buffer[pos] != str.charAt(i)) {
return false;
}
}
return true;
}
//-----------------------------------------------------------------------
/**
* {@inheritDoc}
*/
public CharSequence subSequence(int startIndex, int endIndex) {
if (startIndex < 0) {
throw new StringIndexOutOfBoundsException(startIndex);
}
if (endIndex > size) {
throw new StringIndexOutOfBoundsException(endIndex);
}
if (startIndex > endIndex) {
throw new StringIndexOutOfBoundsException(endIndex - startIndex);
}
return substring(startIndex, endIndex);
}
/**
* Extracts a portion of this string builder as a string.
*
* @param start the start index, inclusive, must be valid
* @return the new string
* @throws IndexOutOfBoundsException if the index is invalid
*/
public String substring(int start) {
return substring(start, size);
}
/**
* Extracts a portion of this string builder as a string.
* <p>
* Note: This method treats an endIndex greater than the length of the
* builder as equal to the length of the builder, and continues
* without error, unlike StringBuffer or String.
*
* @param startIndex the start index, inclusive, must be valid
* @param endIndex the end index, exclusive, must be valid except
* that if too large it is treated as end of string
* @return the new string
* @throws IndexOutOfBoundsException if the index is invalid
*/
public String substring(int startIndex, int endIndex) {
endIndex = validateRange(startIndex, endIndex);
return new String(buffer, startIndex, endIndex - startIndex);
}
/**
* Extracts the leftmost characters from the string builder without
* throwing an exception.
* <p>
* This method extracts the left <code>length</code> characters from
* the builder. If this many characters are not available, the whole
* builder is returned. Thus the returned string may be shorter than the
* length requested.
*
* @param length the number of characters to extract, negative returns empty string
* @return the new string
*/
public String leftString(int length) {
if (length <= 0) {
return "";
} else if (length >= size) {
return new String(buffer, 0, size);
} else {
return new String(buffer, 0, length);
}
}
/**
* Extracts the rightmost characters from the string builder without
* throwing an exception.
* <p>
* This method extracts the right <code>length</code> characters from
* the builder. If this many characters are not available, the whole
* builder is returned. Thus the returned string may be shorter than the
* length requested.
*
* @param length the number of characters to extract, negative returns empty string
* @return the new string
*/
public String rightString(int length) {
if (length <= 0) {
return "";
} else if (length >= size) {
return new String(buffer, 0, size);
} else {
return new String(buffer, size - length, length);
}
}
/**
* Extracts some characters from the middle of the string builder without
* throwing an exception.
* <p>
* This method extracts <code>length</code> characters from the builder
* at the specified index.
* If the index is negative it is treated as zero.