-
Notifications
You must be signed in to change notification settings - Fork 5
/
Copy pathindex.js
1057 lines (1008 loc) · 34.6 KB
/
index.js
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
'use strict';
const Buffer2 = require('buffer');
const fs = require('fs');
var blockSize = 256;
/**
* Build strings in memory.
* @param {(String|Buffer|Number|Boolean|StringBuilder|ReadStream)?} [content = ''] The text you want to initialize.
* @param {Number?} [initialCapacity = 128] The initial capacity.
* @constructor StringBuilder
*/
var StringBuilder = function (content = '', initialCapacity = blockSize / 2) {
var self = this;
// TODO -----Variables-----
var length;
var capacity;
var buffer;
// TODO -----Functions-----
/**
* Re-allocate space.
* @param {Number} newSize The new size you need to re-allocate. The final capacity will bigger than this value.
*/
var reAlloc = function (newSize) {
if (capacity < newSize) {
let count = Math.ceil((newSize - capacity) / blockSize);
let sizeToAdd = count * blockSize;
let emptyBuffer = Buffer.allocUnsafe(sizeToAdd);
buffer = Buffer.concat([buffer, emptyBuffer]);
capacity += sizeToAdd;
}
};
/**
* Get a buffer of UTF-16LE data from the outside source.
* <br/>
* <b>#Sync</b>
* @param {(String|Buffer|Number|Boolean|StringBuilder|ReadStream)!} source The source of data.
* @returns {Buffer|undefined}
*/
var getBufferFromOutside = function (source) {
var type = typeof source;
var dataBuffer;
switch (type) {
case 'object':
if (Buffer.isBuffer(source)) {
dataBuffer = Buffer2.transcode(source, 'utf8', 'utf16le');
break;
} else if (source instanceof StringBuilder) {
let sourceBuffer = source._getBuffer();
let sourceLength = source._getLength();
dataBuffer = sourceBuffer.slice(0, sourceLength);
break;
} else if (source instanceof fs.ReadStream) {
source.close();
let path = source.path;
let encoding = source._readableState.encoding;
if (typeof encoding !== 'string') {
encoding = 'utf8';
}
let content = fs.readFileSync(path);
if (encoding !== 'utf16le') {
content = Buffer2.transcode(content, encoding, 'utf16le');
}
dataBuffer = content;
break;
}
// no break here
case 'boolean':
case 'number':
dataBuffer = Buffer.from(source.toString(), 'utf16le');
break;
case 'string':
dataBuffer = Buffer.from(source, 'utf16le');
break;
default:
return undefined;
}
return dataBuffer;
};
/**
* Get the real index from outside.
* @param {Number} index
*/
var getRealIndex = function (index) {
var halfLength = length / 2;
if (index < 0) {
index += halfLength;
}
if (index < 0) {
index = 0;
} else if (index > halfLength) {
index = length;
} else {
index *= 2;
}
return index;
};
/**
* Whether the character can be trimmed or not.
* @param {Number} characterCode
*/
var isTrimmable = function (characterCode) {
return characterCode <= 32 || characterCode === 12288;
};
/**
* Compute Floor(log2(n)).
* @param {Number} n
* @returns {Number}
*/
var log2Floor = function (n) {
return Math.floor(Math.log(n) / Math.log(2));
};
// TODO -----Getters-----
/**
* Please don't use this function.
* @returns {Buffer}
* @private
*/
this._getBuffer = function () {
return buffer;
};
/**
* Please don't use this function.
* @returns {Number}
* @private
*/
this._getLength = function () {
return length;
};
/**
* Get the length.
* <br/>
* <b>#Sync</b>
* @returns {Number} The current length of this string.
*/
this.length = function () {
return length / 2;
};
/**
* Get the capacity.
* <br/>
* <b>#Sync</b>
* @returns {Number} The current capacity of this StringBuilder.
*/
this.capacity = function () {
return capacity / 2;
};
// TODO -----Changers-----
/**
* Replace the characters in the specific range in this StringBuilder.
* <br/>
* <b>#Sync</b>
* @param {Number?} [start = 0] The start index.
* @param {Number?} [end = length] The end index.
* @param {(String|Buffer|Number|Boolean|StringBuilder|ReadStream)!} content The text you want to replace.
* @returns {StringBuilder}
*/
this.replace = function (start = 0, end = length / 2, content) {
start = getRealIndex(start);
end = getRealIndex(end);
if (start > end) {
return this;
}
var contentBuffer = getBufferFromOutside(content);
if (contentBuffer === undefined) {
return this;
}
var contentBufferLength = contentBuffer.length;
var replaceLength = end - start;
var concatLength = length + contentBufferLength - replaceLength;
reAlloc(concatLength);
if (end === length || contentBufferLength === replaceLength) {
contentBuffer.copy(buffer, start);
} else {
buffer.copy(buffer, start + contentBufferLength, end, length);
contentBuffer.copy(buffer, start);
}
length = concatLength;
return this;
};
/**
* Insert text into this StringBuilder.
* <br/>
* <b>#Sync</b>
* @param {Number?} [offset = 0] The index you want to start to insert.
* @param {(String|Buffer|Number|Boolean|StringBuilder|ReadStream)!} content The text you want to insert.
* @returns {StringBuilder}
*/
this.insert = function (offset = 0, content) {
if (arguments.length < 2) {
content = offset;
offset = 0;
} else {
offset = getRealIndex(offset);
}
var contentBuffer = getBufferFromOutside(content);
if (contentBuffer === undefined) {
return this;
}
var contentBufferLength = contentBuffer.length;
var concatLength = length + contentBufferLength;
reAlloc(concatLength);
if (offset === length) {
contentBuffer.copy(buffer, length);
} else {
buffer.copy(buffer, offset + contentBufferLength, offset, length);
contentBuffer.copy(buffer, offset);
}
length = concatLength;
return this;
};
/**
* Clear this StringBuilder but preserve the capacity.
* <br/>
* <b>#Sync</b>
* @returns {StringBuilder}
*/
this.clear = function () {
length = 0;
return this;
};
/**
* Remove the characters in a substring of this StringBuilder.
* <br/>
* <b>#Sync</b>
* @param {Number?} [start = 0] The start index.
* @param {Number?} [end = length] The end index.
* @returns {StringBuilder}
*/
this.delete = function (start = 0, end = length / 2) {
start = getRealIndex(start);
end = getRealIndex(end);
if (start >= end) {
return this;
}
if (end === length) {
length = start;
} else {
buffer.copy(buffer, start, end, length);
length -= (end - start);
}
return this;
};
/**
* Remove the character at the specified position in this StringBuilder.
* <br/>
* <b>#Sync</b>
* @param {Number!} index Index of char to remove.
* @returns {StringBuilder}
*/
this.deleteCharAt = function (index) {
this.delete(index, index + 1);
return this;
};
/**
* Reserve the substring at the specified range in this StringBuilder.
* <br/>
* <b>#Sync</b>
* @param {Number?} [start = 0] The start index.
* @param {Number?} [end = length] The end index.
* @returns {StringBuilder}
*/
this.substring = function (start = 0, end = length / 2) {
start = getRealIndex(start);
end = getRealIndex(end);
if (start >= end) {
length = 0;
return this;
}
if (start === 0) {
length = end;
} else {
length = end - start;
buffer.copy(buffer, 0, start, end);
}
return this;
};
this.slice = this.substring;
/**
* Reserve the substring at the specified position and length in this StringBuilder.
* <br/>
* <b>#Sync</b>
* @param {Number?} [start = 0] The start index.
* @param {Number?} [length = length] The length of the substring.
* @returns {StringBuilder}
*/
this.substr = function (start = 0, length = length / 2) {
start = getRealIndex(start) / 2;
if (length < 0) {
length = 0;
}
this.substring(start, start + length);
return this;
};
/**
* Append data from ReadStream into this StringBuilder.
* <br/>
* <b>#Async</b>
* @param {ReadStream!} readStream The read stream you want to append.
* @returns {Promise<StringBuilder>}
*/
this.appendReadStream = async function (readStream) {
readStream.setEncoding('utf8');
var promise = new Promise(function (resolve, reject) {
readStream.on('error', function (err) {
reject(err);
});
readStream.on('data', function (text) {
self.append(text);
});
readStream.on('close', function () {
resolve();
});
});
await promise.then();
return this;
};
/**
* Append text into this StringBuilder and add a new line.
* <br/>
* <b>#Sync</b>
* @param {(String|Buffer|Number|Boolean|StringBuilder|ReadStream)!} content The text you want to append.
* @returns {StringBuilder}
*/
this.appendLine = function (content) {
this.append(content);
if (length === capacity) {
reAlloc(capacity + 2);
}
buffer.writeUInt16LE(10, length);
length += 2;
return this;
};
/**
* Append text into this StringBuilder repeatedly.
* <br/>
* <b>#Sync</b>
* @param {(String|Buffer|Number|Boolean|StringBuilder|ReadStream)!} content The text you want to append.
* @param {Number?} [repeatCount = 1] The count you want to repeat.
* @returns {StringBuilder}
*/
this.appendRepeat = function (content, repeatCount = 1) {
if (repeatCount < 1) {
repeatCount = 1;
}
var contentBuffer = getBufferFromOutside(content);
if (contentBuffer === undefined) {
return this;
}
var contentBufferLength = contentBuffer.length;
var concatLength = length + (contentBufferLength * repeatCount);
reAlloc(concatLength);
// log2 copy
var log2Count = log2Floor(repeatCount);
contentBuffer.copy(buffer, length);
for (let i = 1; i <= log2Count; ++i) {
let addedLength = Math.pow(2, i - 1) * contentBufferLength;
buffer.copy(buffer, length + addedLength, length, length + addedLength);
}
var realAddedCount = Math.pow(2, log2Count);
length += contentBufferLength * realAddedCount;
// normal copy
var remainCount = repeatCount - realAddedCount;
for (let i = 0; i < remainCount; ++i) {
contentBuffer.copy(buffer, length);
length += contentBufferLength;
}
return this;
};
/**
* Append text into this StringBuilder.
* <br/>
* <b>#Sync</b>
* @param {(String|Buffer|Number|Boolean|StringBuilder|ReadStream)!} content The text you want to append.
* @returns {StringBuilder}
*/
this.append = function (content) {
var contentBuffer = getBufferFromOutside(content);
if (contentBuffer === undefined) {
return this;
}
var contentBufferLength = contentBuffer.length;
var concatLength = length + contentBufferLength;
reAlloc(concatLength);
contentBuffer.copy(buffer, length);
length = concatLength;
return this;
};
this.concat = this.append;
/**
* Reverse the text.
* <br/>
* <b>#Sync</b>
* @returns {StringBuilder}
*/
this.reverse = function () {
reAlloc(length * 2);
var capacity_dec_2 = capacity - 2;
for (let i = 0; i < length; i += 2) {
buffer.copy(buffer, capacity_dec_2 - i, i, i + 2);
}
buffer.copy(buffer, 0, capacity - length, capacity);
return this;
};
/**
* Convert the text into upper case.
* <br/>
* <b>#Sync</b>
* @returns {StringBuilder}
*/
this.upperCase = function () {
for (let i = 0; i < length; i += 2) {
let v = buffer.readUInt16LE(i);
if (v >= 97 && v <= 122) {
buffer[i] -= 32;
}
}
return this;
};
this.toUpperCase = this.upperCase;
/**
* Convert the text into lower case.
* <br/>
* <b>#Sync</b>
* @returns {StringBuilder}
*/
this.lowerCase = function () {
for (let i = 0; i < length; i += 2) {
let v = buffer.readUInt16LE(i);
if (v >= 65 && v <= 90) {
buffer[i] += 32;
}
}
return this;
};
this.toLowerCase = this.lowerCase;
/**
* Replace the substrings in a specific pattern in this StringBuilder.
* <br/>
* <b>#Sync</b>
* @param {(String|Buffer|Number|Boolean|StringBuilder|ReadStream|RegExp)!} pattern The pattern you want to search.
* @param {(String|Buffer|Number|Boolean|StringBuilder|ReadStream)!} content The text you want to replace.
* @param {Number?} [offset = 0] The number of characters you want to skip
* @param {Number?} [limit = 1] The max number of characters you want to search.
* @returns {StringBuilder}
*/
this.replacePattern = function (pattern, content, offset = 0, limit = 1) {
if (pattern instanceof RegExp) {
let searched = this.indexOfRegExp(pattern, offset, limit);
let indexArray = searched.index;
let lastIndexArray = searched.lastIndex;
for (let i = indexArray.length - 1; i >= 0; --i) {
this.replace(indexArray[i], lastIndexArray[i], content);
}
} else {
let sbPattern = new StringBuilder(pattern);
let sbPatternLength = sbPattern.length();
let index = this.indexOf(sbPattern, offset, limit);
let indexLength = index.length;
if (indexLength === 0) {
return this;
}
let replaceStartIndex = [index[0]];
let replaceEndIndex = [];
let j = 0;
for (let i = 1; i < indexLength; ++i) {
let lastEndIndex = replaceStartIndex[j] + sbPatternLength;
if (lastEndIndex <= index[i]) {
replaceEndIndex.push(lastEndIndex);
replaceStartIndex.push(index[i]);
++j;
}
}
replaceEndIndex.push(replaceStartIndex[j] + sbPatternLength);
for (; j >= 0; --j) {
this.replace(replaceStartIndex[j], replaceEndIndex[j], content);
}
}
return this;
};
/**
* Replace all the substrings in a specific pattern in this StringBuilder.
* <br/>
* <b>#Sync</b>
* @param {(String|Buffer|Number|Boolean|StringBuilder|ReadStream|RegExp)!} pattern The pattern you want to search.
* @param {(String|Buffer|Number|Boolean|StringBuilder|ReadStream)!} content The text you want to replace.
* @returns {StringBuilder}
*/
this.replaceAll = function (pattern, content) {
this.replacePattern(pattern, content, 0, 0);
return this;
};
/**
* Remove any leading and trailing whitespace.
* <br/>
* <b>#Sync</b>
* @returns {StringBuilder}
*/
this.trim = function () {
let start = 0;
for (; start < length; start += 2) {
let v = buffer.readUInt16LE(start);
if (isTrimmable(v)) {
continue;
}
break;
}
let end = length - 2;
for (; end > start; end -= 2) {
let v = buffer.readUInt16LE(end);
if (isTrimmable(v)) {
continue;
}
break;
}
this.substring(start / 2, end / 2 + 1);
return this;
};
/**
* Expand the capacity. If the length of the string is predictable, you can use this function to pre-allocate the space for the string in memory.
* <br/>
* <b>#Sync</b>
* @param {Number!} newCapacity The new capacity you want to set.
* @param {Boolean?} [returnUpdatedCapacity = false] Return the updated capacity or the reference of this StringBuilder.
* @returns {Number|StringBuilder} The updated capacity or the reference of this StringBuilder.
*/
this.expandCapacity = function (newCapacity, returnUpdatedCapacity = false) {
reAlloc(newCapacity * 2);
if (returnUpdatedCapacity) {
return capacity / 2;
}
return this;
};
/**
* Shrink the capacity.
* <br/>
* <b>#Sync</b>
* @param {Boolean?} [returnUpdatedCapacity = false] Return the updated capacity or the reference of this StringBuilder.
* @returns {Number|StringBuilder} The updated capacity or the reference of this StringBuilder.
*/
this.shrinkCapacity = function (returnUpdatedCapacity = false) {
let count = Math.ceil(length / blockSize);
if (count === 0) {
count = 1;
}
let newCapacity = count * blockSize;
if (newCapacity < capacity) {
let emptyBuffer = Buffer.allocUnsafe(newCapacity);
buffer.copy(emptyBuffer, 0, 0, length);
buffer = emptyBuffer;
capacity = newCapacity;
}
if (returnUpdatedCapacity) {
return capacity / 2;
}
return this;
};
/**
* Repeat this StringBuilder.
* <br/>
* <b>#Sync</b>
* @param {Number?} [repeatCount = 1] The count you want to repeat.
* @returns {StringBuilder}
*/
this.repeat = function (repeatCount = 1) {
if (repeatCount <= 0) {
return this;
}
var finalLength = length * repeatCount;
var originalLength = length;
reAlloc(finalLength);
// log2 copy
var log2Count = log2Floor(repeatCount);
buffer.copy(buffer, originalLength);
for (let i = 1; i <= log2Count; ++i) {
let addedLength = Math.pow(2, i - 1) * originalLength;
buffer.copy(buffer, originalLength + addedLength, originalLength, originalLength + addedLength);
}
var realAddedCount = Math.pow(2, log2Count);
length += originalLength * realAddedCount;
// normal copy
var remainCount = repeatCount - realAddedCount;
for (let i = 0; i < remainCount; ++i) {
buffer.copy(buffer, length, 0, originalLength);
length += originalLength;
}
return this;
};
// TODO -----Unchangers-----
/**
* Get the character at the specified position in this StringBuilder.
* <br/>
* <b>#Sync</b>
* @param {Number!} index The specified position.
* @returns {String}
*/
this.charAt = function (index) {
if (index < 0) {
index = length / 2 - start;
}
if (index < 0) {
index = 0;
} else if (index > length / 2) {
index = length;
} else {
index *= 2;
}
return buffer.slice(index, index + 2).toString('utf16le');
};
/**
* Search the substrings in the source text by using RegExp. This function needs to build the string.
* <br/>
* <b>#Sync</b>
* @param {RegExp!} regExp The regular expression pattern you want to use.
* @param {Number?} [offset = 0] The number of characters you want to skip
* @param {Number?} [limit = 0] The max number of substrings you want to search.
* @returns {{index: Array.<Number>, lastIndex: Array.<Number>}} Return two arrays of searched indices.
*/
this.indexOfRegExp = function (regExp, offset = 0, limit = 0) {
if (!(regExp instanceof RegExp)) {
regExp = new RegExp(regExp.toString());
}
offset = getRealIndex(offset);
var str = buffer.slice(offset, length).toString('utf16le');
var match;
var resultIndexList = [];
var resultLastIndexList = [];
if (regExp.global) {
while (match = regExp.exec(str)) {
let index = match.index;
resultIndexList.push(index);
resultLastIndexList.push(index + match[0].length);
if (limit > 0 && resultIndexList.length === limit) {
break;
}
}
} else if (match = regExp.exec(str)) {
let index = match.index;
resultIndexList.push(index);
resultLastIndexList.push(index + match[0].length);
}
return {
index: resultIndexList,
lastIndex: resultLastIndexList
};
};
/**
* Search the substrings in the source text by using Boyer-Moore-MagicLen algorithm.
* See '[this page]{@link https://magiclen.org/boyer-moore-magiclen/}' for more details.
* <br/>
* <b>#Sync</b>
* @param {(String|Buffer|Number|Boolean|StringBuilder|ReadStream|RegExp)!} pattern The pattern you want to search.
* @param {Number?} [offset = 0] The number of characters you want to skip
* @param {Number?} [limit = 0] The max number of substrings you want to search.
* @returns {Array.<Number>} Return an array of searched indices.
*/
this.indexOf = function (pattern, offset = 0, limit = 0) {
if (pattern instanceof RegExp) {
return this.indexOfRegExp(pattern, offset, limit).index;
}
var patternBuffer = getBufferFromOutside(pattern);
if (patternBuffer === undefined) {
return [];
}
var sourceLength = length;
var patternLength = patternBuffer.length;
offset = getRealIndex(offset);
if (patternLength === 0 || offset < 0 || sourceLength - offset < patternLength) {
return [];
}
var sourceLength_dec = sourceLength - 1;
var patternLength_dec = patternLength - 1;
var resultList = [];
var badCharShiftMap = new Array(256).fill(patternLength);
for (let i = 0; i < patternLength_dec; ++i) {
let index = patternBuffer[i];
badCharShiftMap[index] = patternLength_dec - i;
}
var specialChar = patternBuffer[patternLength_dec];
var specialShift = badCharShiftMap[specialChar];
badCharShiftMap[specialChar] = 0;
var sourcePointer = offset + patternLength_dec;
var patternPointer;
while (sourcePointer < sourceLength) {
patternPointer = patternLength_dec;
while (patternPointer >= 0) {
if (buffer[sourcePointer] !== patternBuffer[patternPointer]) {
break;
}
--sourcePointer;
--patternPointer;
}
let starePointer = sourcePointer;
let goodSuffixLength_inc = patternLength - patternPointer;
sourcePointer += goodSuffixLength_inc;
if (patternPointer < 0) {
resultList.push((starePointer + 1) / 2);
if (sourcePointer > sourceLength_dec || limit > 0 && resultList.length === limit) {
break;
} else {
sourcePointer += badCharShiftMap[buffer[sourcePointer]];
continue;
}
}
let shift1 = (sourcePointer <= sourceLength_dec) ? badCharShiftMap[buffer[sourcePointer]] : 0;
if (shift1 >= patternLength_dec) {
sourcePointer += shift1;
} else {
let c = buffer[starePointer];
let shift2 = ((c === specialChar) ? specialShift : badCharShiftMap[c]) - goodSuffixLength_inc;
sourcePointer += (shift1 >= shift2) ? shift1 : shift2;
}
}
return resultList;
};
/**
* Search the substrings in the source text by using Boyer-Moore-MagicLen algorithm.
* See '[this page]{@link https://magiclen.org/boyer-moore-magiclen/}' for more details.
* <br/>
* <b>#Sync</b>
* @param {(String|Buffer|Number|Boolean|StringBuilder|ReadStream)!} pattern The pattern you want to search.
* @param {Number?} [offset = 0] The number of characters you want to skip
* @param {Number?} [limit = 0] The max number of substrings you want to search.
* @returns {Array.<Number>} Return an array of searched indices.
*/
this.lastIndexOf = function (pattern, offset = 0, limit = 0) {
var patternBuffer = getBufferFromOutside(pattern);
if (patternBuffer === undefined) {
return [];
}
var sourceLength = length;
var patternLength = patternBuffer.length;
offset = getRealIndex(offset);
if (patternLength === 0 || offset < 0 || sourceLength - offset < patternLength) {
return [];
}
var sourceLength_dec = sourceLength - 1;
var patternLength_dec = patternLength - 1;
var resultList = [];
var badCharShiftMap = new Array(256).fill(patternLength);
for (let i = patternLength_dec; i > 0; --i) {
let index = patternBuffer[i];
badCharShiftMap[index] = i;
}
var specialChar = patternBuffer[patternLength_dec];
var specialShift = badCharShiftMap[specialChar];
badCharShiftMap[specialChar] = 0;
var sourcePointer = sourceLength_dec - patternLength_dec;
var patternPointer;
while (sourcePointer >= offset) {
patternPointer = 0;
while (patternPointer < patternLength) {
if (buffer[sourcePointer] !== patternBuffer[patternPointer]) {
break;
}
++sourcePointer;
++patternPointer;
}
let starePointer = sourcePointer;
let goodSuffixLength_inc = patternPointer + 1;
sourcePointer -= goodSuffixLength_inc;
if (patternPointer >= patternLength) {
resultList.push((sourcePointer + 1) / 2);
if (sourcePointer < 0 || limit > 0 && resultList.length === limit) {
break;
} else {
sourcePointer -= badCharShiftMap[buffer[sourcePointer]];
continue;
}
}
let shift1 = (sourcePointer >= 0) ? badCharShiftMap[buffer[sourcePointer]] : 0;
if (shift1 >= patternLength_dec) {
sourcePointer -= shift1;
} else {
let c = buffer[starePointer];
let shift2 = ((c === specialChar) ? specialShift : badCharShiftMap[c]) - goodSuffixLength_inc;
sourcePointer -= (shift1 >= shift2) ? shift1 : shift2;
}
}
return resultList;
};
/**
* Check the StringBuilder whether it starts with a specific pattern.
* <br/>
* <b>#Sync</b>
* @param {(String|Buffer|Number|Boolean|StringBuilder|ReadStream)!} pattern The pattern you want to search.
* @returns {Boolean}
*/
this.startsWith = function (pattern) {
var patternBuffer = getBufferFromOutside(pattern);
if (patternBuffer === undefined) {
return false;
}
let patternBufferLength = patternBuffer.length;
if (patternBufferLength > length) {
return false;
}
for (let i = 0; i < patternBufferLength; ++i) {
if (patternBuffer[i] !== buffer[i]) {
return false;
}
}
return true;
};
/**
* Check the StringBuilder whether it ends with a specific pattern.
* <br/>
* <b>#Sync</b>
* @param {(String|Buffer|Number|Boolean|StringBuilder|ReadStream)!} pattern The pattern you want to search.
* @returns {Boolean}
*/
this.endsWith = function (pattern) {
var patternBuffer = getBufferFromOutside(pattern);
if (patternBuffer === undefined) {
return false;
}
let offset = length - patternBuffer.length;
if (offset < 0) {
return false;
}
return buffer.includes(patternBuffer, offset);
};
/**
* Check the StringBuilder whether it logically equals with another data.
* <br/>
* <b>#Sync</b>
* @param {(String|Buffer|Number|Boolean|StringBuilder|ReadStream)!} data
* @returns {Boolean}
*/
this.equals = function (data) {
var dataBuffer = getBufferFromOutside(data);
if (dataBuffer === undefined) {
return false;
}
if (dataBuffer.length !== length) {
return false;
}
return buffer.includes(dataBuffer);
};
/**
* Check the StringBuilder whether it logically equals with another data.
* <br/>
* <b>#Sync</b>
* @param {(String|Buffer|Number|Boolean|StringBuilder|ReadStream)!} data
* @returns {Boolean}
*/
this.equalsIgnoreCase = function (data) {
var dataBuffer = getBufferFromOutside(data);
if (dataBuffer === undefined) {
return false;
}
if (dataBuffer.length !== length) {
return false;
}
for (let i = 0; i < length; i += 2) {
let v1 = buffer.readUInt16LE(i);
let v2 = dataBuffer.readUInt16LE(i);
if (v1 >= 97 && v1 <= 122) {
v1 -= 32;
}
if (v2 >= 97 && v2 <= 122) {
v2 -= 32;
}
if (v1 !== v2) {
return false;
}
}
return true;
};
/**
* Build a string.
* <br/>
* <b>#Sync</b>
* @param {Number?} [start = 0] The start index.
* @param {Number?} [end = length] The end index.
* @returns {String}
*/
this.toString = function (start = 0, end = length / 2) {
start = getRealIndex(start);
end = getRealIndex(end);
return buffer.slice(start, end).toString('utf16le');
};
/**
* Build a UTF-8 buffer.
* <br/>
* <b>#Sync</b>
* @param {Number?} [start = 0] The start index.
* @param {Number?} [end = length] The end index.
* @returns {Buffer}
*/
this.toBuffer = function (start = 0, end = length / 2) {
start = getRealIndex(start);
end = getRealIndex(end);
return Buffer2.transcode(buffer.slice(start, end), 'utf16le', 'utf8');
};
/**
* Clone this StringBuilder.
* <br/>
* <b>#Sync</b>
* @returns {StringBuilder} Return a new StringBuilder.
*/
this.clone = function () {
var newBuffer = Buffer.allocUnsafe(capacity);
buffer.copy(newBuffer, 0, 0, length);
return new StringBuilder(length, capacity, newBuffer);
};
/**
* Count the words.
* <br/>
* <b>#Sync</b>
* @returns {Number}
*/
this.count = function () {
var mode = 0; //0: nornal, 1: appending, 2: integer, 3: prefloat, 4: float
var sum = 0;
for (let i = 0; i < length; i += 2) {
let v = buffer.readUInt16LE(i);
if (v >= 48 && v <= 57) {
switch (mode) {
case 0:
mode = 2;
break;
case 3:
mode = 4;
break;
default:
break;
}
} else if (v >= 65 && v <= 90 || v >= 97 && v <= 122) {
switch (mode) {
case 0:
mode = 1;
break;
case 2:
mode = 1;
break;
case 3:
case 4:
++sum;
mode = 1;
break;
default:
break;
}
} else if (v > 127) {
switch (mode) {
case 0:
++sum;
break;
default:
sum += 2;
mode = 0;
break;
}
} else {
switch (mode) {
case 0: