/
StringBuffer.as
706 lines (705 loc) · 22.7 KB
/
StringBuffer.as
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
package skyboy.utils {
/**
* StringBuffer by skyboy. January 29th 2012.
* Visit http://github.com/skyboy for documentation, updates
* and more free code.
*
*
* Copyright (c) 2010, skyboy
* All rights reserved.
*
* Permission is hereby granted, free of charge, to any person
* obtaining a copy of this software and associated documentation
* files (the "Software"), to deal in the Software with
* restriction, with limitation the rights to use, copy, modify,
* merge, publish, distribute, sublicense copies of the Software,
* and to permit persons to whom the Software is furnished to do so,
* subject to the following conditions and limitations:
*
* ^ Attribution will be given to:
* skyboy, http://www.kongregate.com/accounts/skyboy;
* http://github.com/skyboy; http://skybov.deviantart.com
*
* ^ Redistributions of source code must retain the above copyright notice,
* this list of conditions and the following disclaimer in all copies or
* substantial portions of the Software.
*
* ^ Redistributions of modified source code must be marked as such, with
* the modifications marked and ducumented and the modifer's name clearly
* listed as having modified the source code.
*
* ^ Redistributions of source code may not add to, subtract from, or in
* any other way modify the above copyright notice, this list of conditions,
* or the following disclaimer for any reason.
*
* ^ Redistributions in binary form must reproduce the above copyright
* notice, this list of conditions and the following disclaimer in the
* documentation and/or other materials provided with the distribution.
*
* THE SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS
* IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT
* NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A
* PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS
* OR COPYRIGHT HOLDERS OR CONTRIBUTORS BE LIABLE FOR ANY CLAIM, DIRECT,
* INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
* OR OTHER LIABILITY,(INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
* SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR
* BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY,
* WHETHER AN ACTION OF IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING
* NEGLIGENCE OR OTHERWISE) ARISING FROM, OUT OF, IN CONNECTION OR
* IN ANY OTHER WAY OUT OF THE USE OF OR OTHER DEALINGS WITH THIS
* SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
*/
import flash.utils.ByteArray;
import flash.utils.Endian;
public class StringBuffer {
protected var buffer:ByteArray = new ByteArray;
protected var backBuffer:ByteArray = new ByteArray;
/**
* @param uint: size Initial size of the buffer in bytes.
* @param String: str Initial content of the buffer.
*/
public function StringBuffer(size:uint = 0, str:String = null):void {
buffer.endian = Endian.BIG_ENDIAN;
buffer.length = size;
if (str) buffer.writeUTFBytes(str);
}
//{ APPEND
public function appendByteArray(a:ByteArray, start:uint = 0, len:uint = 0):StringBuffer {
var end:uint = start + len;
var e:uint = a.length;
if (end > e) end = e;
if (start >= end) return this;
buffer.writeBytes(a, start, end - start);
return this;
}
public function appendChar(a:int):StringBuffer {
buffer.writeByte(a);
return this;
}
public function appendChars(...a):StringBuffer {
var buffer:ByteArray = this.buffer;
for each(var b:* in a)
buffer.writeByte(int(b));
return this;
}
public function appendCharArray(a:Array):StringBuffer {
var buffer:ByteArray = this.buffer;
for each(var b:* in a)
buffer.writeByte(int(b));
return this;
}
public function appendCharVectorI(a:Vector.<int>):StringBuffer {
var buffer:ByteArray = this.buffer;
for each(var b:int in a)
buffer.writeByte(b);
return this;
}
public function appendCharVectorU(a:Vector.<uint>):StringBuffer {
var buffer:ByteArray = this.buffer;
for each(var b:uint in a)
buffer.writeByte(b);
return this;
}
public function appendInt(a:int):StringBuffer {
buffer.writeInt(a);
return this;
}
public function appendInts(...a):StringBuffer {
var buffer:ByteArray = this.buffer;
for each(var b:* in a)
buffer.writeInt(int(b));
return this;
}
public function appendIntArray(a:Array):StringBuffer {
var buffer:ByteArray = this.buffer;
for each(var b:* in a)
buffer.writeInt(int(b));
return this;
}
public function appendIntVector(a:Vector.<int>):StringBuffer {
var buffer:ByteArray = this.buffer;
for each(var b:int in a)
buffer.writeInt(b);
return this;
}
public function appendString(a:*):StringBuffer {
buffer.writeUTFBytes(String(a));
return this;
}
public function appendStrings(...a):StringBuffer {
var buffer:ByteArray = this.buffer;
for each(var b:* in a)
buffer.writeUTFBytes(String(b));
return this;
}
public function appendStringArray(a:Array):StringBuffer {
var buffer:ByteArray = this.buffer;
for each(var b:* in a)
buffer.writeUTFBytes(String(b));
return this;
}
public function appendStringBuffer(a:StringBuffer, start:uint = 0, len:uint = 0):StringBuffer {
var b:ByteArray = a.buffer, e:uint = b.length;
var end:uint = start + len;
if (end > e) end = e;
if (start >= end) return this;
buffer.writeBytes(b, start, end - start);
return this;
}
public function appendStringVector(a:Vector.<String>):StringBuffer {
var buffer:ByteArray = this.buffer;
for each(var b:String in a)
buffer.writeUTFBytes(String(b));
return this;
}
public function appendUintVector(a:Vector.<uint>):StringBuffer {
var buffer:ByteArray = this.buffer;
for each(var b:uint in a)
buffer.writeInt(b);
return this;
}
//} APPEND
public function byteAt(i:uint):int {
if (i < buffer.length) return buffer[i];
return 0;
}
public function charAt(i:uint):String {
return toString().charAt(i);
}
public function clear():void {
buffer.clear();
backBuffer.clear();
}
public function count(str:String):uint {
if (!str) return 0;
var a:ByteArray = buffer, b:ByteArray = backBuffer;
var p:uint = a.position, e:uint = a.length, i:uint, c:uint;
b.clear();
b.writeUTFBytes(str);
a.position = 0;
while (e--) {
if (a.readByte() == b[i]) {
++i;
if (i == b.length) ++c, i = 0;
} else i = 0;
}
i = a.position - i;
a.position = p;
return c;
}
public function countChar(char:int):uint {
var a:ByteArray = buffer, i:uint, e:uint = a.length, c:uint;
while (i < e) c += uint(a[i] == char), ++i;
return c;
}
public function deleteCharAt(i:uint):void {
var a:ByteArray = buffer, l:uint = a.length - 1;
if (int(!length) | int(i >= l)) return;
var p:uint = a.position;
a.position = i;
a.writeBytes(a, i + 1, l - i);
a.position = p - 1;
}
public function deleteCharsAt(i:uint, len:uint):void {
var a:ByteArray = buffer, l:uint = a.length - len;
if (int(!length) | int(i >= l)) return;
var p:uint = a.position;
a.position = i;
a.writeBytes(a, i + len, l - i);
a.position = p - len;
}
public function empty():Boolean {
return !buffer.position;
}
public function getBytesAt(start:uint, end:uint, dst:ByteArray = null, dstStart:uint = 0):ByteArray {
if (!dst) dst = new ByteArray;
if (dstStart < dst.length) dstStart = dst.length;
dst.writeBytes(buffer, start, end);
return dst;
}
//{ INSERT
public function insertByteArray(offset:uint, a:ByteArray, start:int = 0, end:int = 0):StringBuffer {
var buffer:ByteArray = this.buffer;
if (offset > length) {
appendByteArray(a, start, end);
return this;
}
var tem:uint = buffer.position;
if (offset < buffer.position) buffer.position = offset;
backBuffer.clear();
backBuffer.writeBytes(buffer, offset, 0);
var e:uint = a.length;
if (end > e) end = e;
if (start > end) return this;
buffer.writeBytes(a, start, end);
buffer.writeBytes(backBuffer, 0, backBuffer.length);
return this;
}
public function insertChar(offset:uint, a:int):StringBuffer {
var buffer:ByteArray = this.buffer;
if (offset > length) {
appendChar(a);
return this;
}
var tem:uint = buffer.position;
if (offset < buffer.position) buffer.position = offset;
backBuffer.clear();
backBuffer.writeBytes(buffer, offset, 0);
buffer.writeByte(a);
buffer.writeBytes(backBuffer, 0, backBuffer.length);
return this;
}
public function insertChars(offset:uint, ...a):StringBuffer {
var buffer:ByteArray = this.buffer;
if (offset > length) {
appendCharArray(a);
return this;
}
var tem:uint = buffer.position;
if (offset < buffer.position) buffer.position = offset;
backBuffer.clear();
backBuffer.writeBytes(buffer, offset, 0);
for each(var b:* in a)
buffer.writeByte(int(b));
buffer.writeBytes(backBuffer, 0, backBuffer.length);
return this;
}
public function insertCharArray(offset:uint, a:Array):StringBuffer {
var buffer:ByteArray = this.buffer;
if (offset > length) {
appendCharArray(a);
return this;
}
var tem:uint = buffer.position;
if (offset < buffer.position) buffer.position = offset;
backBuffer.clear();
backBuffer.writeBytes(buffer, offset, 0);
for each(var b:* in a)
buffer.writeByte(int(b));
buffer.writeBytes(backBuffer, 0, backBuffer.length);
return this;
}
public function insertCharVectorI(offset:uint, a:Vector.<int>):StringBuffer {
var buffer:ByteArray = this.buffer;
if (offset > length) {
appendCharVectorI(a);
return this;
}
var tem:uint = buffer.position;
if (offset < buffer.position) buffer.position = offset;
backBuffer.clear();
backBuffer.writeBytes(buffer, offset, 0);
for each(var b:int in a)
buffer.writeByte(b);
buffer.writeBytes(backBuffer, 0, backBuffer.length);
return this;
}
public function insertCharVectorU(offset:uint, a:Vector.<uint>):StringBuffer {
var buffer:ByteArray = this.buffer;
if (offset > length) {
appendCharVectorU(a);
return this;
}
var tem:uint = buffer.position;
if (offset < buffer.position) buffer.position = offset;
backBuffer.clear();
backBuffer.writeBytes(buffer, offset, 0);
for each(var b:uint in a)
buffer.writeByte(b);
buffer.writeBytes(backBuffer, 0, backBuffer.length);
return this;
}
public function insertInt(offset:uint, a:int):StringBuffer {
var buffer:ByteArray = this.buffer;
if (offset > length) {
appendInt(a);
return this;
}
var tem:uint = buffer.position;
if (offset < buffer.position) buffer.position = offset;
backBuffer.clear();
backBuffer.writeBytes(buffer, offset, 0);
buffer.writeInt(a);
buffer.writeBytes(backBuffer, 0, backBuffer.length);
return this;
}
public function insertInts(offset:uint, ...a):StringBuffer {
var buffer:ByteArray = this.buffer;
if (offset > length) {
appendIntArray(a);
return this;
}
var tem:uint = buffer.position;
if (offset < buffer.position) buffer.position = offset;
backBuffer.clear();
backBuffer.writeBytes(buffer, offset, 0);
for each(var b:* in a)
buffer.writeInt(int(b));
buffer.writeBytes(backBuffer, 0, backBuffer.length);
return this;
}
public function insertIntArray(offset:uint, a:Array):StringBuffer {
var buffer:ByteArray = this.buffer;
if (offset > length) {
appendIntArray(a);
return this;
}
var tem:uint = buffer.position;
if (offset < buffer.position) buffer.position = offset;
backBuffer.clear();
backBuffer.writeBytes(buffer, offset, 0);
for each(var b:* in a)
buffer.writeInt(int(b));
buffer.writeBytes(backBuffer, 0, backBuffer.length);
return this;
}
public function insertIntVector(offset:uint, a:Vector.<int>):StringBuffer {
var buffer:ByteArray = this.buffer;
if (offset > length) {
appendIntVector(a);
return this;
}
var tem:uint = buffer.position;
if (offset < buffer.position) buffer.position = offset;
backBuffer.clear();
backBuffer.writeBytes(buffer, offset, 0);
for each(var b:int in a)
buffer.writeInt(b);
buffer.writeBytes(backBuffer, 0, backBuffer.length);
return this;
}
public function insertString(offset:uint, a:*):StringBuffer {
var buffer:ByteArray = this.buffer;
if (offset > length) {
appendString(a);
return this;
}
var tem:uint = buffer.position;
if (offset < buffer.position) buffer.position = offset;
backBuffer.clear();
backBuffer.writeBytes(buffer, offset, 0);
buffer.writeUTFBytes(String(a));
buffer.writeBytes(backBuffer, 0, backBuffer.length);
return this;
}
public function insertStrings(offset:uint, ...a):StringBuffer {
var buffer:ByteArray = this.buffer;
if (offset > length) {
appendStringArray(a);
return this;
}
var tem:uint = buffer.position;
if (offset < buffer.position) buffer.position = offset;
backBuffer.clear();
backBuffer.writeBytes(buffer, offset, 0);
for each(var b:* in a)
buffer.writeUTFBytes(String(b));
buffer.writeBytes(backBuffer, 0, backBuffer.length);
return this;
}
public function insertStringArray(offset:uint, a:Array):StringBuffer {
var buffer:ByteArray = this.buffer;
if (offset > length) {
appendStringArray(a);
return this;
}
var tem:uint = buffer.position;
if (offset < buffer.position) buffer.position = offset;
backBuffer.clear();
backBuffer.writeBytes(buffer, offset, 0);
for each(var b:* in a)
buffer.writeUTFBytes(String(b));
buffer.writeBytes(backBuffer, 0, backBuffer.length);
return this;
}
public function insertStringBuffer(offset:uint, a:StringBuffer, start:int = 0, end:int = 0):StringBuffer {
var buffer:ByteArray = this.buffer;
if (offset > length) {
appendStringBuffer(a, start, end);
return this;
}
var tem:uint = buffer.position;
if (offset < buffer.position) buffer.position = offset;
backBuffer.clear();
backBuffer.writeBytes(buffer, offset, 0);
var b:ByteArray = a.buffer, e:uint = b.length;
if (end > e) end = e;
if (start > end) return this;
buffer.writeBytes(b, start, end);
buffer.writeBytes(backBuffer, 0, backBuffer.length);
return this;
}
public function insertStringVector(offset:uint, a:Vector.<String>):StringBuffer {
var buffer:ByteArray = this.buffer;
if (offset > length) {
appendStringVector(a);
return this;
}
var tem:uint = buffer.position;
if (offset < buffer.position) buffer.position = offset;
backBuffer.clear();
backBuffer.writeBytes(buffer, offset, 0);
for each(var b:String in a)
buffer.writeUTFBytes(String(b));
buffer.writeBytes(backBuffer, 0, backBuffer.length);
return this;
}
public function insertUintVector(offset:uint, a:Vector.<uint>):StringBuffer {
var buffer:ByteArray = this.buffer;
if (offset > length) {
appendUintVector(a);
return this;
}
var tem:uint = buffer.position;
if (offset < buffer.position) buffer.position = offset;
backBuffer.clear();
backBuffer.writeBytes(buffer, offset, 0);
for each(var b:uint in a)
buffer.writeInt(b);
buffer.writeBytes(backBuffer, 0, backBuffer.length);
return this;
}
//} INSERT
public function indexOf(str:String, start:uint = 0):Number {
if (!str || start >= length) return -1;
var a:ByteArray = buffer, b:ByteArray = backBuffer;
var p:uint = a.position, e:uint = a.length, i:uint;
b.clear();
b.writeUTFBytes(str);
a.position = 0;
while (e--) {
if (a.readByte() == b[i]) {
++i;
if (i == b.length) break;
} else i = 0;
}
i = a.position - i;
a.position = p;
if (~e) return Number(i);
return -1;
}
public function indexOfChar(char:int, start:uint = 0):Number {
if (start >= length) return -1;
var a:ByteArray = buffer;
var p:uint = a.position, e:uint = a.length;
a.position = 0;
while (e--)
if (a.readByte() == char) break;
var i:uint = a.position - i;
a.position = p;
if (~e) return Number(i);
return -1;
}
public function lastIndexOf(str:String, start:uint = uint.MAX_VALUE):Number {
if (!str || start >= length) return -1;
var a:ByteArray = buffer, b:ByteArray = backBuffer;
var p:uint = a.position, e:uint = a.length, i:uint, l:uint;
b.clear();
b.writeUTFBytes(str);
a.position = 0;
if (start < e) e = start;
while (e--) {
if (a.readByte() == b[i]) {
++i;
if (i == b.length) l = a.position - i, i = 0;
} else i = 0;
}
a.position = p;
if (~e) return Number(l);
return -1;
}
public function lastIndexOfChar(char:int, start:uint = uint.MAX_VALUE):Number {
if (start >= length) return -1;
var a:ByteArray = buffer, e:uint = a.length;
if (start < e) e = start;
while (e--)
if (a[e] == char) return Number(e);
return -1;
}
public function get length():uint {
return buffer.length;
}
public function set length(l:uint):void {
buffer.length = l;
if (l < buffer.position) buffer.position = l;
}
//{ REPLACE
public function replace(start:uint, str:String):StringBuffer {
var i:uint = buffer.position;
buffer.position = start;
buffer.writeUTFBytes(String(str));
if (buffer.position < i) buffer.position = i;
return this;
}
public function replaceBytes(start:uint, end:uint, bytes:ByteArray, bytesStart:uint = 0):StringBuffer {
var a:ByteArray = buffer, i:uint = bytesStart, e:uint = bytes.length;
if (end > a.length) end = a.length;
while (int(start < end) & int(i < e)) a[start++] = bytes[i++];
return this;
}
public function replaceChar(char:int, replace:int):uint {
var a:ByteArray = buffer, i:uint, e:uint = buffer.length, c:int;
while (i < e) {
if (a[i] == char) ++c, a[i] = replace;
++i;
}
return c;
}
public function replaceCharVoid(char:int, replace:int):StringBuffer {
var a:ByteArray = buffer, i:uint, e:uint = buffer.length;
while (i < e) {
if (a[i] == char) a[i] = replace;
++i;
}
return this;
}
public function replaceFirstChar(char:int, replace:int):StringBuffer {
var a:ByteArray = buffer, i:uint, e:uint = buffer.length;
while (i < e) {
if (a[i] == char) {
a[i] = replace;
return this;
}
++i;
}
return this;
}
public function replaceLastChar(char:int, replace:int):StringBuffer {
var a:ByteArray = buffer, e:uint = buffer.length;
while (e--) {
if (a[e] == char) {
a[e] = replace;
return this;
}
}
return this;
}
public function replaceNthChar(char:int, replace:int, n:uint):StringBuffer {
var a:ByteArray = buffer, i:uint, e:uint = buffer.length, c:uint;
while (i < e) {
if (a[i] == char) {
if (c++ == n) {
a[i] = replace;
return this;
}
}
++i;
}
return this;
}
public function replaceRange(start:uint, end:uint, str:String, strStart:uint = 0):StringBuffer {
var a:ByteArray = buffer, i:uint = strStart, e:uint = str.length;
if (end > a.length) end = a.length;
while (int(start < end) & int(i < e)) a[start++] = str.charCodeAt(i++);
return this;
}
//} REPLACE
/**
* Discards the current buffers and creates new ones.
* @return ByteArray: The old ByteArray buffer.
*/
public function reset(newSize:uint = 0):ByteArray {
var a:ByteArray = buffer;
buffer = new ByteArray;
backBuffer = new ByteArray;
buffer.length = newSize;
return a;
}
public function reverse():StringBuffer {
var a:ByteArray = buffer, i:uint, l:uint = a.length, v:int;
while (i < l) {
v = a[i];
a[i] = a[l];
a[l] = v;
++i, --l;
}
return this;
}
public function setByteAt(i:uint, v:int):StringBuffer {
if (i < buffer.length) buffer[i] = v;
return this;
}
public function setCharAt(i:uint, char:String):StringBuffer {
if (!char || char.length !== 1) throw new ArgumentError("Char length must be 1.");
if (i >= length) throw new ArgumentError("Index " + i + " outside range " + length + ".");
backBuffer.clear();
if (i) backBuffer.writeUTFBytes(toString(0, i - 1));
backBuffer.writeUTFBytes(char);
backBuffer.writeUTFBytes(toString(i + 1));
buffer.clear();
buffer.writeBytes(backBuffer, 0, backBuffer.length);
backBuffer = new ByteArray;
return this;
}
/**
* Returns the length of the string to the first null byte.
* If there are no null bytes, this function returns the same value the length property returns.
* @return uint: The length of the string to the first null byte.
*/
public function size():uint {
var a:ByteArray = buffer;
if (!a.length) return 0;
var p:uint = a.position, e:uint = a.length;
a.position = 0;
while (e && a.readByte()) --e;
var l:uint = a.position;
a.position = p;
return l - 1 * int(e != 0);
}
public function subbuf(start:uint = 0, len:uint = uint.MAX_VALUE):StringBuffer {
var a:StringBuffer = new StringBuffer();
a.appendStringBuffer(this, start, len);
return a;
}
public function subbuffer(start:uint, end:uint = uint.MAX_VALUE):StringBuffer {
var a:StringBuffer = new StringBuffer(), e:uint = length;
if (end > e) end = e;
a.appendStringBuffer(this, start, end - start);
return a;
}
public function substr(start:uint = 0, len:uint = uint.MAX_VALUE):String {
if (len) return toString(start, Math.min(Number(start) + Number(len), uint.MAX_VALUE));
return '';
}
public function substring(start:uint, end:uint = uint.MAX_VALUE):String {
if (end > start) return toString(start, end);
return '';
}
public function toString(start:uint = 0, end:uint = 0):String {
var a:ByteArray = buffer, i:int = a.position;
if (int(!end) | int(end > i)) end = i;
if (start >= end) return '';
a.position = start;
var r:String = a.readUTFBytes(end - start);
a.position = i;
return r;
}
/**
* Trims the internal array down to the passed value if $size is > 0; otherwise trims the inernal array to the first null byte.
* If the string contains no null bytes and $size is 0, this method does nothing.
* @param uint: $size The length to trim the interal array to. (default: 0)
* @return uint: The new length of the internal array.
*/
public function trimToSize($size:uint = 0):uint {
var i:uint = $size || size();
if (i >= buffer.length) return buffer.length;
buffer.position = i;
buffer.length = i;
return i;
}
/**
* Trims the internal array down to the passed value if $size is > 0; otherwise trims the inernal array to the first null byte.
* If the string contains no null bytes and $size is 0, this method does nothing.
* @param uint: $size The length to trim the interal array to. (default: 0)
* @return StringBuffer: A reference to this StringBuffer.
*/
public function trimToSizeVoid($size:uint = 0):StringBuffer {
var i:uint = $size || size();
if (i >= buffer.length) return this;
buffer.position = i;
buffer.length = i;
return this;
}
}
}