-
Notifications
You must be signed in to change notification settings - Fork 18
/
StringBuilder.hx
488 lines (431 loc) · 14.8 KB
/
StringBuilder.hx
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
/*
* SPDX-FileCopyrightText: © Vegard IT GmbH (https://vegardit.com) and contributors
* SPDX-FileContributor: Sebastian Thomschke, Vegard IT GmbH
* SPDX-License-Identifier: Apache-2.0
*/
package hx.strings;
import haxe.io.BytesOutput;
import haxe.io.Output;
using hx.strings.Strings;
/**
* An UTF-8 and fluent API supporting alternative to <code>StringBuf</code>
* with additional functions such as <code>clear()</code>, <code>insert()</code>, <code>isEmpty()</code>
* <br/>
* This implementation tries to avoid the creation of intermediate String objects as much as possible.
*/
#if (haxe_ver < 4.1) @:nullSafety(Off) #else @:nullSafety(Strict) #end
@notThreadSafe
class StringBuilder {
var sb = new StringBuf();
#if !(java_src || cs)
var pre:Null<Array<String>> = null;
var len:Int = 0;
#end
inline
public function new(?initialContent:AnyAsString)
if (initialContent != null)
add(initialContent);
/**
* <b>Important:</b> Invocation may result in temporary string object generation during invocation
* on platforms except Java.
*
* <pre><code>
* >>> new StringBuilder("").length == 0
* >>> new StringBuilder("はい").add("は").add("い").length == 4
* >>> new StringBuilder("ab").insert(0, "cd").insert(2, "はい").insertChar(1, 32).insertChar(4, 32).length == 8
* </code></pre>
*
* @return the length of the string representation of all added items
*/
public var length(get, never): Int;
inline
function get_length():Int
#if (java_src || cs)
return sb.length;
#else
return len;
#end
/**
* <pre><code>
* >>> new StringBuilder(null).add(null).toString() == "null"
* >>> new StringBuilder("hi").add(null).toString() == "hinull"
* >>> new StringBuilder("hi").add(1).toString() == "hi1"
* </code></pre>
*
* @return <code>this</code> for chained operations
*/
public function add(item:Null<AnyAsString>):StringBuilder {
sb.add(item == null ? "null" : item);
#if !(java_src || cs)
len += item.length8();
#end
return this;
}
/**
* <pre><code>
* >>> new StringBuilder("").addChar(223).toString() == "ß"
* >>> new StringBuilder("hi").addChar(12399).toString() == "hiは"
* </code></pre>
*
* @return <code>this</code> for chained operations
*/
#if java inline #end
public function addChar(ch:Char):StringBuilder {
#if (java || flash || cs || python)
sb.addChar(ch);
#else
if (ch.isAscii())
sb.addChar(ch);
else
sb.add(ch.toString());
#end
#if !(java_src || cs)
len++;
#end
return this;
}
/**
* <pre><code>
* >>> new StringBuilder("hi").addAll([1,2]).toString() == "hi12"
* >>> new StringBuilder("hi").addAll([1,2]).length == 4
* </code></pre>
* @return <code>this</code> for chained operations
*/
public function addAll(items:Array<AnyAsString>):StringBuilder {
for (item in items) {
sb.add(item);
#if !(java_src || cs)
len += item.length8();
#end
}
return this;
}
/**
* Resets the builders internal buffer
*
* <pre><code>
* >>> new StringBuilder("hi").clear().add(1).toString() == "1"
* </code></pre>
*
* @return <code>this</code> for chained operations
*/
public function clear():StringBuilder {
#if java_src
untyped __java__("this.sb.b.setLength(0)");
#elseif cs
untyped __cs__("this.sb.b.Clear()");
#else
pre = null;
sb = new StringBuf();
len = 0;
#end
return this;
}
/**
* <pre><code>
* >>> new StringBuilder("").isEmpty() == true
* >>> new StringBuilder("cat").isEmpty() == false
* </code></pre>
*
* @return <code>true</code> if no chars/strings have been added to the string builder yet
*/
inline
public function isEmpty():Bool
return length == 0;
/**
* adds the "\n" new line character
*
* @return <code>this</code> for chained operations
*/
public function newLine():StringBuilder {
sb.add(Strings.NEW_LINE_NIX);
#if !(java_src || cs)
len ++;
#end
return this;
}
/**
* <pre><code>
* >>> new StringBuilder("hi").insert(0, "はい").toString() == "はいhi"
* >>> new StringBuilder("hi").insert(1, "はい").toString() == "hはいi"
* >>> new StringBuilder("hi").insert(2, "はい").toString() == "hiはい"
* >>> new StringBuilder("hi").insert(0, "はい").insert(1, "はい").toString() == "ははいいhi"
* >>> new StringBuilder("hi").insert(0, "ho").insert(1, "はい").toString() == "hはいohi"
* >>> new StringBuilder("hi").insert(0, "ho").insert(1, "はい").toString() == "hはいohi"
* >>> new StringBuilder("hi").insert(0, "ho").insert(2, "はい").toString() == "hoはいhi"
* >>> new StringBuilder("hi").insert(0, "ho").insert(3, "はい").toString() == "hohはいi"
* >>> new StringBuilder("hi").insert(0, "ho").insert(4, "はい").toString() == "hohiはい"
* >>> new StringBuilder("hi").insert(3, " ").toString() throws "[pos] must not be greater than this.length"
* </code></pre>
*
* @return <code>this</code> for chained operations
* @throws exception if pos is out-of range (i.e. < 0 or > this.length)
*/
public function insert(pos:CharIndex, item:AnyAsString):StringBuilder {
if (pos < 0) throw "[pos] must not be negative";
if (pos > this.length) throw "[pos] must not be greater than this.length";
if (pos == this.length) {
add(item);
return this;
}
#if java_src
untyped __java__("this.sb.b.insert(pos, item)");
#elseif cs
untyped __cs__("this.sb.b.Insert(pos, item)");
#else
if (pos == 0) {
if (pre == null) pre = [];
pre.unshift(item);
len += item.length8();
return this;
}
// insert the item into the pre[] array if required
var pre_len = 0;
if (pre != null) {
final pre = this.pre; // TODO workaround for null-safety bug
final i = pre.length;
for(i in 0...pre.length) {
final next_pre_len = pre_len + pre[i].length8();
if (next_pre_len == pos) {
pre.insert(i + 1, item);
len += item.length8();
return this;
}
if (next_pre_len > pos) {
final preSplitted:Array<String> = cast pre[i].splitAt(pos - pre_len);
pre[i] = preSplitted[0];
pre.insert(i + 1, item);
pre.insert(i + 2, preSplitted[1]);
len += item.length8();
return this;
}
pre_len = next_pre_len;
}
}
if (sb.length == 0) {
add(item);
return this;
}
final sbSplitted:Array<String> = cast sb.toString().splitAt(pos - pre_len);
sb = new StringBuf();
sb.add(sbSplitted[0]);
sb.add(item);
len += item.length8();
sb.add(sbSplitted[1]);
#end
return this;
}
/**
* <pre><code>
* >>> new StringBuilder("hi").insertChar(0, Char.of("は")).toString() == "はhi"
* >>> new StringBuilder("hi").insertChar(1, Char.of("は")).toString() == "hはi"
* >>> new StringBuilder("hi").insertChar(2, Char.of("は")).toString() == "hiは"
* >>> new StringBuilder("hi").insertChar(0, Char.of("は")).insertChar(1, 32).toString() == "は hi"
* >>> new StringBuilder("hi").insert(0, "ho").insertChar(1, Char.of("は")).toString() == "hはohi"
* >>> new StringBuilder("hi").insert(0, "ho").insertChar(1, Char.of("は")).toString() == "hはohi"
* >>> new StringBuilder("hi").insert(0, "ho").insertChar(2, Char.of("は")).toString() == "hoはhi"
* >>> new StringBuilder("hi").insert(0, "ho").insertChar(3, Char.of("は")).toString() == "hohはi"
* >>> new StringBuilder("hi").insert(0, "ho").insertChar(4, Char.of("は")).toString() == "hohiは"
* >>> new StringBuilder("hi").insertChar(3, 32).toString() throws "[pos] must not be greater than this.length"
* </code></pre>
*
* @return <code>this</code> for chained operations
* @throws exception if pos is out-of range (i.e. < 0 or > this.length)
*/
public function insertChar(pos:CharIndex, ch:Char):StringBuilder {
if (pos < 0) throw "[pos] must not be negative";
if (pos > this.length) throw "[pos] must not be greater than this.length";
if (pos == this.length) {
addChar(ch);
return this;
}
#if java_src
untyped __java__("this.sb.b.insert(pos, (char)ch)");
#elseif cs
untyped __cs__("this.sb.b.Insert(pos, (System.Char)ch)");
#else
if (pos == 0) {
if (pre == null) pre = [];
pre.unshift(ch);
len++;
return this;
}
// insert the char into the pre[] array if required
var pre_len = 0;
if (pre != null) {
final pre = this.pre; // TODO workaround for null-safety bug
final i = pre.length;
for(i in 0...pre.length) {
final next_pre_len = pre_len + pre[i].length8();
if (next_pre_len == pos) {
pre.insert(i + 1, ch);
len++;
return this;
}
if (next_pre_len > pos) {
final preSplitted:Array<String> = cast pre[i].splitAt(pos - pre_len);
pre[i] = preSplitted[0];
pre.insert(i + 1, ch);
pre.insert(i + 2, preSplitted[1]);
len++;
return this;
}
pre_len = next_pre_len;
}
}
if (sb.length == 0) {
addChar(ch);
return this;
}
final sbSplitted:Array<String> = cast sb.toString().splitAt(pos - pre_len);
sb = new StringBuf();
sb.add(sbSplitted[0]);
addChar(ch);
sb.add(sbSplitted[1]);
#end
return this;
}
/**
* <pre><code>
* >>> new StringBuilder("hi").insertAll(0, [1,2]).toString() == "12hi"
* >>> new StringBuilder("hi").insertAll(1, [1,2]).toString() == "h12i"
* >>> new StringBuilder("hi").insertAll(2, [1,2]).toString() == "hi12"
* </code></pre>
*
* @return <code>this</code> for chained operations
*/
public function insertAll(pos:CharIndex, items:Array<AnyAsString>):StringBuilder {
if (pos < 0) throw "[pos] must not be negative";
if (pos > this.length) throw "[pos] must not be greater than this.length";
if (pos == this.length) {
addAll(items);
return this;
}
#if (java_src || cs)
var i = items.length;
while (i-- > 0) {
final item = items[i];
#if java_src
untyped __java__("this.sb.b.insert(pos, item)");
#else
untyped __cs__("this.sb.b.Insert(pos, item)");
#end
}
#else
if (pos == 0) {
if (pre == null) pre = [];
final pre = this.pre; // TODO workaround for null-safety bug
var i = items.length;
while (i-- > 0) {
final item = items[i];
pre.unshift(item);
len += item.length8();
}
return this;
}
// insert the items into the pre[] array if required
var pre_len = 0;
if (pre != null) {
final pre = this.pre; // TODO workaround for null-safety bug
final i = pre.length;
for(i in 0...pre.length) {
final next_pre_len = pre_len + pre[i].length8();
if (next_pre_len == pos) {
var j = items.length;
while (j-- > 0) {
final item = items[j];
pre.insert(i + 1, item);
len += item.length8();
}
return this;
}
if (next_pre_len > pos) {
final preSplitted:Array<String> = cast pre[i].splitAt(pos - pre_len);
pre[i] = preSplitted[0];
pre.insert(i + 1, preSplitted[1]);
var j = items.length;
while (j-- > 0) {
final item = items[j];
pre.insert(i + 1, item);
len += item.length8();
}
return this;
}
pre_len = next_pre_len;
}
}
if (sb.length == 0) {
for(item in items)
add(item);
return this;
}
final sbSplitted:Array<String> = cast sb.toString().splitAt(pos - pre_len);
sb = new StringBuf();
sb.add(sbSplitted[0]);
for (item in items) {
sb.add(item);
len += item.length8();
}
sb.add(sbSplitted[1]);
#end
return this;
}
/**
* <pre><code>
* >>> ({var sb=new StringBuilder("1"); final out=sb.asOutput(); out.writeByte(Char.TWO); out.writeString("3"); sb; }).toString() == "123"
* </code></pre>
*
* @return a haxe.ui.Output wrapper object around this instance
*/
public function asOutput():Output
return new OutputWrapper(this);
/**
* <pre><code>
* >>> new StringBuilder().toString() == ""
* >>> new StringBuilder("").toString() == ""
* >>> new StringBuilder(null).toString() == ""
* >>> new StringBuilder("hi").toString() == "hi"
* </code></pre>
*
* @return a new string object representing the builder's content
*/
#if java_src inline #end
public function toString():String {
#if (java_src || cs)
return sb.toString();
#else
if (pre == null)
return sb.toString();
final str = pre.join("") + sb.toString();
clear();
add(str);
return str;
#end
}
}
#if (haxe_ver < 4.1) @:nullSafety(Off) #else @:nullSafety(Strict) #end
private class OutputWrapper extends Output {
private final sb:StringBuilder;
private var bo:Null<BytesOutput>;
inline
public function new(sb:StringBuilder)
this.sb = sb;
override
public function flush() {
if (bo != null && bo.length > 0) {
sb.add(bo.getBytes().toString());
bo == null;
}
}
override
public function writeByte(c:Int):Void {
if (bo == null) bo = new BytesOutput();
bo.writeByte(c);
}
@:dox(hide)
override
function writeString(str:String, ?encoding:haxe.io.Encoding) {
flush();
sb.add(str);
}
}