warpo 3.2.0 → 3.2.1

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (93) hide show
  1. package/assemblyscript/std/README.md +5 -0
  2. package/assemblyscript/std/assembly/array.ts +542 -0
  3. package/assemblyscript/std/assembly/arraybuffer.ts +72 -0
  4. package/assemblyscript/std/assembly/builtins.ts +2312 -0
  5. package/assemblyscript/std/assembly/compat.ts +2 -0
  6. package/assemblyscript/std/assembly/dataview.ts +144 -0
  7. package/assemblyscript/std/assembly/date.ts +383 -0
  8. package/assemblyscript/std/assembly/diagnostics.ts +11 -0
  9. package/assemblyscript/std/assembly/error.ts +39 -0
  10. package/assemblyscript/std/assembly/function.ts +44 -0
  11. package/assemblyscript/std/assembly/iterator.ts +24 -0
  12. package/assemblyscript/std/assembly/map.ts +304 -0
  13. package/assemblyscript/std/assembly/math.ts +3401 -0
  14. package/assemblyscript/std/assembly/memory.ts +110 -0
  15. package/assemblyscript/std/assembly/number.ts +388 -0
  16. package/assemblyscript/std/assembly/object.ts +30 -0
  17. package/assemblyscript/std/assembly/polyfills.ts +24 -0
  18. package/assemblyscript/std/assembly/process.ts +7 -0
  19. package/assemblyscript/std/assembly/promise.ts +172 -0
  20. package/assemblyscript/std/assembly/reference.ts +48 -0
  21. package/assemblyscript/std/assembly/regexp.ts +16 -0
  22. package/assemblyscript/std/assembly/rt/common.ts +79 -0
  23. package/assemblyscript/std/assembly/rt/index-incremental.ts +4 -0
  24. package/assemblyscript/std/assembly/rt/index-radical.ts +4 -0
  25. package/assemblyscript/std/assembly/rt/index.d.ts +37 -0
  26. package/assemblyscript/std/assembly/rt/itcms.ts +415 -0
  27. package/assemblyscript/std/assembly/rt/tcms.ts +276 -0
  28. package/assemblyscript/std/assembly/rt/tlsf.ts +564 -0
  29. package/assemblyscript/std/assembly/rt.ts +71 -0
  30. package/assemblyscript/std/assembly/set.ts +261 -0
  31. package/assemblyscript/std/assembly/shared/feature.ts +69 -0
  32. package/assemblyscript/std/assembly/shared/host.ts +12 -0
  33. package/assemblyscript/std/assembly/shared/runtime.ts +9 -0
  34. package/assemblyscript/std/assembly/shared/target.ts +9 -0
  35. package/assemblyscript/std/assembly/shared/tsconfig.json +9 -0
  36. package/assemblyscript/std/assembly/shared/typeinfo.ts +72 -0
  37. package/assemblyscript/std/assembly/staticarray.ts +446 -0
  38. package/assemblyscript/std/assembly/string.ts +892 -0
  39. package/assemblyscript/std/assembly/symbol.ts +61 -0
  40. package/assemblyscript/std/assembly/table.ts +15 -0
  41. package/assemblyscript/std/assembly/tsconfig.json +4 -0
  42. package/assemblyscript/std/assembly/tuple.ts +36 -0
  43. package/assemblyscript/std/assembly/typedarray.ts +2000 -0
  44. package/assemblyscript/std/assembly/uri.ts +17 -0
  45. package/assemblyscript/std/assembly/util/bytes.ts +90 -0
  46. package/assemblyscript/std/assembly/util/casemap.ts +264 -0
  47. package/assemblyscript/std/assembly/util/error.ts +54 -0
  48. package/assemblyscript/std/assembly/util/hash.ts +117 -0
  49. package/assemblyscript/std/assembly/util/math.ts +1922 -0
  50. package/assemblyscript/std/assembly/util/memory.ts +309 -0
  51. package/assemblyscript/std/assembly/util/number.ts +884 -0
  52. package/assemblyscript/std/assembly/util/sort.ts +282 -0
  53. package/assemblyscript/std/assembly/util/string.ts +1075 -0
  54. package/assemblyscript/std/assembly/util/uri.ts +251 -0
  55. package/assemblyscript/std/assembly/vector.ts +3 -0
  56. package/dist/debug_server/debugSession.d.ts +1 -0
  57. package/dist/debug_server/debugSession.js +15 -6
  58. package/dist/debug_server/debugSession.js.map +1 -1
  59. package/dist/debug_server/debuggerWasmModule.d.ts +3 -0
  60. package/dist/debug_server/debuggerWasmModule.js +47 -2
  61. package/dist/debug_server/debuggerWasmModule.js.map +1 -1
  62. package/dist/debug_server/tests/debugSession.test.js +96 -0
  63. package/dist/debug_server/tests/debugSession.test.js.map +1 -1
  64. package/dist/debug_server/tests/testModule/debugger_closure_stepping.d.ts +1 -0
  65. package/dist/debug_server/tests/testModule/debugger_closure_stepping.js +12 -0
  66. package/dist/debug_server/tests/testModule/debugger_closure_stepping.js.map +1 -0
  67. package/dist/debug_server/tests/testModule/debugger_std_array.d.ts +1 -0
  68. package/dist/debug_server/tests/testModule/debugger_std_array.js +6 -0
  69. package/dist/debug_server/tests/testModule/debugger_std_array.js.map +1 -0
  70. package/dist/debug_server/tests/testModule/debugger_string_stepping.d.ts +1 -0
  71. package/dist/debug_server/tests/testModule/debugger_string_stepping.js +9 -0
  72. package/dist/debug_server/tests/testModule/debugger_string_stepping.js.map +1 -0
  73. package/package.json +7 -7
  74. package/warpo_extension/std/tsconfig.json +4 -0
  75. package/warpo_extension/std/warpo/ffi.ts +16 -0
  76. package/warpo_extension/std/warpo/test/comparison.ts +122 -0
  77. package/warpo_extension/std/warpo/test/env.ts +33 -0
  78. package/warpo_extension/std/warpo/test/expect.ts +101 -0
  79. package/warpo_extension/std/warpo/test/formatPrint.ts +99 -0
  80. package/warpo_extension/std/warpo/test/implement.ts +41 -0
  81. package/warpo_extension/std/warpo/test/index.d.ts +0 -0
  82. package/warpo_extension/std/warpo/test/index.ts +39 -0
  83. package/warpo_extension/std/warpo/test/mockInstrument.ts +26 -0
  84. package/warpo_extension/std/warpo/utf8/const_str.ts +17 -0
  85. package/warpo_extension/std/wasi_snapshot_preview1/LICENSE +202 -0
  86. package/warpo_extension/std/wasi_snapshot_preview1/README.md +1 -0
  87. package/warpo_extension/std/wasi_snapshot_preview1/bindings/wasi_snapshot_preview1.ts +1731 -0
  88. package/warpo_extension/std/wasi_snapshot_preview1/wasi_console.ts +96 -0
  89. package/warpo_extension/std/wasi_snapshot_preview1/wasi_crypto.ts +8 -0
  90. package/warpo_extension/std/wasi_snapshot_preview1/wasi_date.ts +13 -0
  91. package/warpo_extension/std/wasi_snapshot_preview1/wasi_internal.ts +119 -0
  92. package/warpo_extension/std/wasi_snapshot_preview1/wasi_performance.ts +11 -0
  93. package/warpo_extension/std/wasi_snapshot_preview1/wasi_process.ts +191 -0
@@ -0,0 +1,892 @@
1
+ /// <reference path="./rt/index.d.ts" />
2
+
3
+ import { OBJECT, BLOCK_MAXSIZE, TOTAL_OVERHEAD } from "./rt/common";
4
+ import { compareImpl, strtol, strtod, isSpace, isAscii, isFinalSigma, toLower8, toUpper8 } from "./util/string";
5
+ import { SPECIALS_UPPER, casemap, bsearch } from "./util/casemap";
6
+ import { E_INDEXOUTOFRANGE, E_INVALIDLENGTH, E_UNPAIRED_SURROGATE } from "./util/error";
7
+ import { idof } from "./builtins";
8
+ import { Array } from "./array";
9
+
10
+ class StringIterator implements Iterator<string> {
11
+ private index: i32 = 0;
12
+ private len: i32;
13
+
14
+ constructor(private readonly str: String) {
15
+ this.len = str.length;
16
+ }
17
+
18
+ next(): IteratorResult<string> {
19
+ const current = this.index;
20
+ const len = this.len;
21
+ if (current >= len) return IteratorResult.done<string>();
22
+
23
+ const ptr = changetype<usize>(this.str) + ((<usize>current) << 1);
24
+ const first = <i32>load<u16>(ptr);
25
+
26
+ if ((first & 0xfc00) == 0xd800 && current + 1 < len) {
27
+ const second = <i32>load<u16>(ptr, 2);
28
+ if ((second & 0xfc00) == 0xdc00) {
29
+ this.index = current + 2;
30
+ return IteratorResult.fromValue<string>(changetype<string>(String.fromCharCode(first, second)));
31
+ }
32
+ }
33
+
34
+ this.index = current + 1;
35
+ return IteratorResult.fromValue<string>(changetype<string>(String.fromCharCode(first)));
36
+ }
37
+ }
38
+
39
+
40
+ @final export abstract class String implements Iterable<string> {
41
+
42
+ @lazy static readonly MAX_LENGTH: i32 = <i32>(BLOCK_MAXSIZE >>> alignof<u16>());
43
+
44
+ static fromCharCode(unit: i32, surr: i32 = -1): String {
45
+ let hasSur = surr > 0;
46
+ let out = changetype<String>(__new(2 << i32(hasSur), idof<String>()));
47
+ store<u16>(changetype<usize>(out), <u16>unit);
48
+ if (hasSur) store<u16>(changetype<usize>(out), <u16>surr, 2);
49
+ return out;
50
+ }
51
+
52
+ static fromCharCodes(units: Array<i32>): String {
53
+ let length = units.length;
54
+ let out = changetype<String>(__new((<usize>length) << 1, idof<String>()));
55
+ let ptr = units.dataStart;
56
+ for (let i = 0; i < length; ++i) {
57
+ store<u16>(changetype<usize>(out) + ((<usize>i) << 1), load<i32>(ptr + ((<usize>i) << 2)));
58
+ }
59
+ return out;
60
+ }
61
+
62
+ static fromCodePoint(code: i32): String {
63
+ let hasSur = <u32>code > 0xffff;
64
+ let out = changetype<String>(__new(2 << i32(hasSur), idof<String>()));
65
+ if (!hasSur) {
66
+ store<u16>(changetype<usize>(out), <u16>code);
67
+ } else {
68
+ // Checks valid code point range
69
+ assert(<u32>code <= 0x10ffff);
70
+ code -= 0x10000;
71
+ let hi = (code & 0x03ff) | 0xdc00;
72
+ let lo = (code >>> 10) | 0xd800;
73
+ store<u32>(changetype<usize>(out), lo | (hi << 16));
74
+ }
75
+ return out;
76
+ }
77
+
78
+
79
+ @builtin static raw(parts: TemplateStringsArray, ...args: unknown[]): string {
80
+ return unreachable();
81
+ }
82
+
83
+ get length(): i32 {
84
+ return changetype<OBJECT>(changetype<usize>(this) - TOTAL_OVERHEAD).rtSize >> 1;
85
+ }
86
+
87
+ [Symbol.iterator](): StringIterator {
88
+ return new StringIterator(this);
89
+ }
90
+
91
+ at(pos: i32): String {
92
+ let len = this.length;
93
+ pos += select(0, len, pos >= 0);
94
+ if (<u32>pos >= <u32>len) throw new RangeError(E_INDEXOUTOFRANGE);
95
+ let out = __new(2, idof<String>());
96
+ store<u16>(out, load<u16>(changetype<usize>(this) + ((<usize>pos) << 1)));
97
+ return changetype<String>(out); // retains
98
+ }
99
+
100
+
101
+ @operator("[]") charAt(pos: i32): String {
102
+ if (<u32>pos >= <u32>this.length) return changetype<String>("");
103
+ let out = changetype<String>(__new(2, idof<String>()));
104
+ store<u16>(changetype<usize>(out), load<u16>(changetype<usize>(this) + ((<usize>pos) << 1)));
105
+ return out;
106
+ }
107
+
108
+ charCodeAt(pos: i32): i32 {
109
+ if (<u32>pos >= <u32>this.length) return -1; // (NaN)
110
+ return load<u16>(changetype<usize>(this) + ((<usize>pos) << 1));
111
+ }
112
+
113
+ codePointAt(pos: i32): i32 {
114
+ let len = this.length;
115
+ if (<u32>pos >= <u32>len) return -1; // (undefined)
116
+ let first = <i32>load<u16>(changetype<usize>(this) + ((<usize>pos) << 1));
117
+ if ((first & 0xfc00) != 0xd800 || pos + 1 == len) return first;
118
+ let second = <i32>load<u16>(changetype<usize>(this) + ((<usize>pos) << 1), 2);
119
+ if ((second & 0xfc00) != 0xdc00) return first;
120
+ return ((first - 0xd800) << 10) + (second - 0xdc00) + 0x10000;
121
+ }
122
+
123
+
124
+ @operator("+") private static __concat(left: String, right: String): String {
125
+ return left.concat(right);
126
+ }
127
+
128
+ concat(other: String): String {
129
+ let thisSize: isize = this.length << 1;
130
+ let otherSize: isize = other.length << 1;
131
+ let outSize: usize = thisSize + otherSize;
132
+ if (outSize == 0) return changetype<String>("");
133
+ let out = changetype<String>(__new(outSize, idof<String>()));
134
+ memory.copy(changetype<usize>(out), changetype<usize>(this), thisSize);
135
+ memory.copy(changetype<usize>(out) + thisSize, changetype<usize>(other), otherSize);
136
+ return out;
137
+ }
138
+
139
+ endsWith(search: String, end: i32 = String.MAX_LENGTH): bool {
140
+ end = min(max(end, 0), this.length);
141
+ let searchLength = <isize>search.length;
142
+ let searchStart = <isize>end - searchLength;
143
+ if (searchStart < 0) return false;
144
+ // @ts-ignore: string <-> String
145
+ return !compareImpl(this, searchStart, search, 0, searchLength);
146
+ }
147
+
148
+
149
+ @operator("==") private static __eq(left: String | null, right: String | null): bool {
150
+ if (changetype<usize>(left) == changetype<usize>(right)) return true;
151
+ if (changetype<usize>(left) == 0 || changetype<usize>(right) == 0) return false;
152
+ let leftLength = changetype<string>(left).length;
153
+ if (leftLength != changetype<string>(right).length) return false;
154
+ // @ts-ignore: string <-> String
155
+ return !compareImpl(left, 0, right, 0, leftLength);
156
+ }
157
+
158
+
159
+ @operator.prefix("!")
160
+ private static __not(str: String | null): bool {
161
+ return changetype<usize>(str) == 0 || !changetype<string>(str).length;
162
+ }
163
+
164
+
165
+ @operator("!=")
166
+ private static __ne(left: String | null, right: String | null): bool {
167
+ return !this.__eq(left, right);
168
+ }
169
+
170
+
171
+ @operator(">") private static __gt(left: String, right: String): bool {
172
+ if (changetype<usize>(left) == changetype<usize>(right)) return false;
173
+ let leftLength = left.length;
174
+ if (!leftLength) return false;
175
+ let rightLength = right.length;
176
+ if (!rightLength) return true;
177
+ // @ts-ignore: string <-> String
178
+ let res = compareImpl(left, 0, right, 0, min(leftLength, rightLength));
179
+ return res ? res > 0 : leftLength > rightLength;
180
+ }
181
+
182
+
183
+ @operator(">=") private static __gte(left: String, right: String): bool {
184
+ return !this.__lt(left, right);
185
+ }
186
+
187
+
188
+ @operator("<") private static __lt(left: String, right: String): bool {
189
+ if (changetype<usize>(left) == changetype<usize>(right)) return false;
190
+ let rightLength = right.length;
191
+ if (!rightLength) return false;
192
+ let leftLength = left.length;
193
+ if (!leftLength) return true;
194
+ // @ts-ignore: string <-> String
195
+ let res = compareImpl(left, 0, right, 0, min(leftLength, rightLength));
196
+ return res ? res < 0 : leftLength < rightLength;
197
+ }
198
+
199
+
200
+ @operator("<=") private static __lte(left: String, right: String): bool {
201
+ return !this.__gt(left, right);
202
+ }
203
+
204
+ includes(search: String, start: i32 = 0): bool {
205
+ return this.indexOf(search, start) != -1;
206
+ }
207
+
208
+ indexOf(search: String, start: i32 = 0): i32 {
209
+ let searchLen = <isize>search.length;
210
+ if (!searchLen) return 0;
211
+ let len = <isize>this.length;
212
+ if (!len) return -1;
213
+ let searchStart = min(max(<isize>start, 0), len);
214
+ for (len -= searchLen; searchStart <= len; ++searchStart) {
215
+ // @ts-ignore: string <-> String
216
+ if (!compareImpl(this, searchStart, search, 0, searchLen)) return <i32>searchStart;
217
+ }
218
+ return -1;
219
+ }
220
+
221
+ lastIndexOf(search: String, start: i32 = i32.MAX_VALUE): i32 {
222
+ let searchLen = <isize>search.length;
223
+ if (!searchLen) return this.length;
224
+ let len = this.length;
225
+ if (!len) return -1;
226
+ let searchStart = min(max(<isize>start, 0), <isize>len - searchLen);
227
+ for (; searchStart >= 0; --searchStart) {
228
+ // @ts-ignore: string <-> String
229
+ if (!compareImpl(this, searchStart, search, 0, searchLen)) return <i32>searchStart;
230
+ }
231
+ return -1;
232
+ }
233
+
234
+ // TODO: implement full locale comparison with locales and Collator options
235
+ localeCompare(other: String): i32 {
236
+ if (changetype<usize>(other) == changetype<usize>(this)) return 0;
237
+ let alen = this.length;
238
+ let blen = other.length;
239
+ // @ts-ignore: string <-> String
240
+ let res = compareImpl(this, 0, other, 0, <usize>min(alen, blen));
241
+ res = res ? res : alen - blen;
242
+ // normalize to [-1, 1] range
243
+ return i32(res > 0) - i32(res < 0);
244
+ }
245
+
246
+ startsWith(search: String, start: i32 = 0): bool {
247
+ let len = <isize>this.length;
248
+ let searchStart = min(max(<isize>start, 0), len);
249
+ let searchLength = <isize>search.length;
250
+ if (searchLength + searchStart > len) return false;
251
+ // @ts-ignore: string <-> String
252
+ return !compareImpl(this, searchStart, search, 0, searchLength);
253
+ }
254
+
255
+ substr(start: i32, length: i32 = i32.MAX_VALUE): String {
256
+ // legacy
257
+ let intStart: isize = start;
258
+ let end: isize = length;
259
+ let len: isize = this.length;
260
+ if (intStart < 0) intStart = max(len + intStart, 0);
261
+ let size = min(max(end, 0), len - intStart) << 1;
262
+ if (size <= 0) return changetype<String>("");
263
+ let out = changetype<String>(__new(size, idof<String>()));
264
+ memory.copy(changetype<usize>(out), changetype<usize>(this) + (intStart << 1), size);
265
+ return out;
266
+ }
267
+
268
+ substring(start: i32, end: i32 = i32.MAX_VALUE): String {
269
+ let len: isize = this.length;
270
+ let finalStart = min<isize>(max(start, 0), len);
271
+ let finalEnd = min<isize>(max(end, 0), len);
272
+ let fromPos = min<isize>(finalStart, finalEnd) << 1;
273
+ let toPos = max<isize>(finalStart, finalEnd) << 1;
274
+ let size = toPos - fromPos;
275
+ if (!size) return changetype<String>("");
276
+ if (!fromPos && toPos == len << 1) return this;
277
+ let out = changetype<String>(__new(size, idof<String>()));
278
+ memory.copy(changetype<usize>(out), changetype<usize>(this) + fromPos, size);
279
+ return out;
280
+ }
281
+
282
+ trim(): String {
283
+ let len = this.length;
284
+ let size: usize = len << 1;
285
+ while (size && isSpace(load<u16>(changetype<usize>(this) + size - 2))) {
286
+ size -= 2;
287
+ }
288
+ let offset: usize = 0;
289
+ while (offset < size && isSpace(load<u16>(changetype<usize>(this) + offset))) {
290
+ offset += 2;
291
+ size -= 2;
292
+ }
293
+ if (!size) return changetype<String>("");
294
+ if (!offset && size == len << 1) return this;
295
+ let out = changetype<String>(__new(size, idof<String>()));
296
+ memory.copy(changetype<usize>(out), changetype<usize>(this) + offset, size);
297
+ return out;
298
+ }
299
+
300
+
301
+ @inline
302
+ trimLeft(): String {
303
+ return this.trimStart();
304
+ }
305
+
306
+
307
+ @inline
308
+ trimRight(): String {
309
+ return this.trimEnd();
310
+ }
311
+
312
+ trimStart(): String {
313
+ let size = (<usize>this.length) << 1;
314
+ let offset: usize = 0;
315
+ while (offset < size && isSpace(load<u16>(changetype<usize>(this) + offset))) {
316
+ offset += 2;
317
+ }
318
+ if (!offset) return this;
319
+ size -= offset;
320
+ if (!size) return changetype<String>("");
321
+ let out = changetype<String>(__new(size, idof<String>()));
322
+ memory.copy(changetype<usize>(out), changetype<usize>(this) + offset, size);
323
+ return out;
324
+ }
325
+
326
+ trimEnd(): String {
327
+ let originalSize = (<usize>this.length) << 1;
328
+ let size = originalSize;
329
+ while (size && isSpace(load<u16>(changetype<usize>(this) + size - 2))) {
330
+ size -= 2;
331
+ }
332
+ if (!size) return changetype<String>("");
333
+ if (size == originalSize) return this;
334
+ let out = changetype<String>(__new(size, idof<String>()));
335
+ memory.copy(changetype<usize>(out), changetype<usize>(this), size);
336
+ return out;
337
+ }
338
+
339
+ padStart(length: i32, pad: string = " "): String {
340
+ let thisSize = (<usize>this.length) << 1;
341
+ let targetSize = (<usize>length) << 1;
342
+ let padSize = (<usize>pad.length) << 1;
343
+ if (targetSize < thisSize || !padSize) return this;
344
+ let prependSize = targetSize - thisSize;
345
+ let out = changetype<String>(__new(targetSize, idof<String>()));
346
+ if (prependSize > padSize) {
347
+ let repeatCount = (prependSize - 2) / padSize;
348
+ let restBase = repeatCount * padSize;
349
+ let restSize = prependSize - restBase;
350
+ memory.repeat(changetype<usize>(out), changetype<usize>(pad), padSize, repeatCount);
351
+ memory.copy(changetype<usize>(out) + restBase, changetype<usize>(pad), restSize);
352
+ } else {
353
+ memory.copy(changetype<usize>(out), changetype<usize>(pad), prependSize);
354
+ }
355
+ memory.copy(changetype<usize>(out) + prependSize, changetype<usize>(this), thisSize);
356
+ return out;
357
+ }
358
+
359
+ padEnd(length: i32, pad: string = " "): String {
360
+ let thisSize = (<usize>this.length) << 1;
361
+ let targetSize = (<usize>length) << 1;
362
+ let padSize = (<usize>pad.length) << 1;
363
+ if (targetSize < thisSize || !padSize) return this;
364
+ let appendSize = targetSize - thisSize;
365
+ let out = changetype<String>(__new(targetSize, idof<String>()));
366
+ memory.copy(changetype<usize>(out), changetype<usize>(this), thisSize);
367
+ if (appendSize > padSize) {
368
+ let repeatCount = (appendSize - 2) / padSize;
369
+ let restBase = repeatCount * padSize;
370
+ let restSize = appendSize - restBase;
371
+ memory.repeat(changetype<usize>(out) + thisSize, changetype<usize>(pad), padSize, repeatCount);
372
+ memory.copy(changetype<usize>(out) + thisSize + restBase, changetype<usize>(pad), restSize);
373
+ } else {
374
+ memory.copy(changetype<usize>(out) + thisSize, changetype<usize>(pad), appendSize);
375
+ }
376
+ return out;
377
+ }
378
+
379
+ repeat(count: i32 = 0): String {
380
+ let length = this.length;
381
+
382
+ // Most browsers can't handle strings 1 << 28 chars or longer
383
+ if (count < 0 || <u64>length * count > 1 << 28) {
384
+ throw new RangeError(E_INVALIDLENGTH);
385
+ }
386
+
387
+ if (count == 0 || !length) return changetype<String>("");
388
+ if (count == 1) return this;
389
+ let out = changetype<String>(__new((length * count) << 1, idof<String>()));
390
+ memory.repeat(changetype<usize>(out), changetype<usize>(this), (<usize>length) << 1, count);
391
+ return out;
392
+ }
393
+
394
+ replace(search: String, replacement: String): String {
395
+ let len: usize = this.length;
396
+ let slen: usize = search.length;
397
+ if (len <= slen) {
398
+ return len < slen ? this : select<String>(replacement, this, search == this);
399
+ }
400
+ let index: isize = this.indexOf(search);
401
+ if (~index) {
402
+ let rlen: usize = replacement.length;
403
+ len -= slen;
404
+ let olen = len + rlen;
405
+ if (olen) {
406
+ let out = changetype<String>(__new(olen << 1, idof<String>()));
407
+ memory.copy(changetype<usize>(out), changetype<usize>(this), index << 1);
408
+ memory.copy(changetype<usize>(out) + (index << 1), changetype<usize>(replacement), rlen << 1);
409
+ memory.copy(
410
+ changetype<usize>(out) + ((index + rlen) << 1),
411
+ changetype<usize>(this) + ((index + slen) << 1),
412
+ (len - index) << 1
413
+ );
414
+ return out;
415
+ }
416
+ }
417
+ return this;
418
+ }
419
+
420
+ replaceAll(search: String, replacement: String): String {
421
+ let thisLen: usize = this.length;
422
+ let searchLen: usize = search.length;
423
+ if (thisLen <= searchLen) {
424
+ return thisLen < searchLen ? this : select<String>(replacement, this, search == this);
425
+ }
426
+ let replaceLen: usize = replacement.length;
427
+ if (!searchLen) {
428
+ if (!replaceLen) return this;
429
+ // Special case: 'abc'.replaceAll('', '-') -> '-a-b-c-'
430
+ let out = changetype<String>(__new((thisLen + (thisLen + 1) * replaceLen) << 1, idof<String>()));
431
+ memory.copy(changetype<usize>(out), changetype<usize>(replacement), replaceLen << 1);
432
+ let offset = replaceLen;
433
+ for (let i: usize = 0; i < thisLen; ++i) {
434
+ store<u16>(changetype<usize>(out) + (offset++ << 1), load<u16>(changetype<usize>(this) + (i << 1)));
435
+ memory.copy(changetype<usize>(out) + (offset << 1), changetype<usize>(replacement), replaceLen << 1);
436
+ offset += replaceLen;
437
+ }
438
+ return out;
439
+ }
440
+ let prev: isize = 0,
441
+ next: isize = 0;
442
+ if (searchLen == replaceLen) {
443
+ // Fast path when search and replacement have same length
444
+ let outSize = thisLen << 1;
445
+ let out = changetype<String>(__new(outSize, idof<String>()));
446
+ memory.copy(changetype<usize>(out), changetype<usize>(this), outSize);
447
+ while (~(next = <isize>this.indexOf(search, <i32>prev))) {
448
+ memory.copy(changetype<usize>(out) + (next << 1), changetype<usize>(replacement), replaceLen << 1);
449
+ prev = next + searchLen;
450
+ }
451
+ return out;
452
+ }
453
+ let out: String | null = null,
454
+ offset: usize = 0,
455
+ outSize = thisLen;
456
+ while (~(next = <isize>this.indexOf(search, <i32>prev))) {
457
+ if (!out) out = changetype<String>(__new(thisLen << 1, idof<String>()));
458
+ let chunk = next - prev;
459
+ if (offset + chunk + replaceLen > outSize) {
460
+ outSize <<= 1;
461
+ out = changetype<String>(__renew(changetype<usize>(out), outSize << 1));
462
+ }
463
+ memory.copy(changetype<usize>(out) + (offset << 1), changetype<usize>(this) + (prev << 1), chunk << 1);
464
+ offset += chunk;
465
+ memory.copy(changetype<usize>(out) + (offset << 1), changetype<usize>(replacement), replaceLen << 1);
466
+ offset += replaceLen;
467
+ prev = next + searchLen;
468
+ }
469
+ if (out) {
470
+ let rest = thisLen - prev;
471
+ if (offset + rest > outSize) {
472
+ outSize <<= 1;
473
+ out = changetype<String>(__renew(changetype<usize>(out), outSize << 1));
474
+ }
475
+ if (rest) {
476
+ memory.copy(changetype<usize>(out) + (offset << 1), changetype<usize>(this) + (prev << 1), rest << 1);
477
+ }
478
+ rest += offset;
479
+ if (outSize > rest) {
480
+ out = changetype<String>(__renew(changetype<usize>(out), rest << 1));
481
+ }
482
+ return out;
483
+ }
484
+ return this;
485
+ }
486
+
487
+ slice(start: i32, end: i32 = i32.MAX_VALUE): String {
488
+ let len = this.length;
489
+ start = start < 0 ? max(start + len, 0) : min(start, len);
490
+ end = end < 0 ? max(end + len, 0) : min(end, len);
491
+ len = end - start;
492
+ if (len <= 0) return changetype<String>("");
493
+ let out = changetype<String>(__new(len << 1, idof<String>()));
494
+ memory.copy(changetype<usize>(out), changetype<usize>(this) + ((<usize>start) << 1), (<usize>len) << 1);
495
+ return out;
496
+ }
497
+
498
+ split(separator: String | null = null, limit: i32 = i32.MAX_VALUE): String[] {
499
+ if (!limit) return changetype<String[]>(__newArray(0, alignof<String>(), idof<Array<String>>()));
500
+ if (changetype<usize>(separator) == 0) return [this];
501
+ let length: isize = this.length;
502
+ let sepLen = changetype<string>(separator).length;
503
+ if (limit < 0) limit = i32.MAX_VALUE;
504
+ if (!sepLen) {
505
+ if (!length) return changetype<String[]>(__newArray(0, alignof<String>(), idof<Array<String>>()));
506
+ // split by chars
507
+ length = min<isize>(length, <isize>limit);
508
+ let result = changetype<String[]>(__newArray(<i32>length, alignof<String>(), idof<Array<String>>()));
509
+ // @ts-ignore: cast
510
+ let resultStart = result.dataStart as usize;
511
+ for (let i: isize = 0; i < length; ++i) {
512
+ let charStr = changetype<String>(__new(2, idof<String>()));
513
+ store<u16>(changetype<usize>(charStr), load<u16>(changetype<usize>(this) + ((<usize>i) << 1)));
514
+ store<usize>(resultStart + ((<usize>i) << alignof<usize>()), changetype<usize>(charStr)); // result[i] = charStr
515
+ __link(changetype<usize>(result), changetype<usize>(charStr), true);
516
+ }
517
+ return result;
518
+ } else if (!length) {
519
+ let result = changetype<String[]>(__newArray(1, alignof<String>(), idof<Array<String>>()));
520
+ // @ts-ignore: cast
521
+ store<usize>(result.dataStart as usize, changetype<usize>("")); // static ""
522
+ return result;
523
+ }
524
+ let result = changetype<String[]>(__newArray(0, alignof<String>(), idof<Array<String>>()));
525
+ let end = 0,
526
+ start = 0,
527
+ i = 0;
528
+ while (~(end = this.indexOf(changetype<string>(separator), start))) {
529
+ let len = end - start;
530
+ if (len > 0) {
531
+ let out = changetype<String>(__new((<usize>len) << 1, idof<String>()));
532
+ memory.copy(changetype<usize>(out), changetype<usize>(this) + ((<usize>start) << 1), (<usize>len) << 1);
533
+ result.push(out);
534
+ } else {
535
+ result.push(changetype<String>(""));
536
+ }
537
+ if (++i == limit) return result;
538
+ start = end + sepLen;
539
+ }
540
+ if (!start) {
541
+ // also means: loop above didn't do anything
542
+ result.push(this);
543
+ return result;
544
+ }
545
+ let len = length - start;
546
+ if (len > 0) {
547
+ let out = changetype<String>(__new((<usize>len) << 1, idof<String>()));
548
+ memory.copy(changetype<usize>(out), changetype<usize>(this) + ((<usize>start) << 1), (<usize>len) << 1);
549
+ result.push(out);
550
+ } else {
551
+ result.push(changetype<String>("")); // static ""
552
+ }
553
+ return result;
554
+ }
555
+
556
+ toLowerCase(): String {
557
+ let len = <usize>this.length;
558
+ if (!len) return this;
559
+ let codes = changetype<String>(__new(len * 2 * 2, idof<String>()));
560
+ let j: usize = 0;
561
+ for (let i: usize = 0; i < len; ++i, ++j) {
562
+ let c = <u32>load<u16>(changetype<usize>(this) + (i << 1));
563
+ if (isAscii(c)) {
564
+ store<u16>(changetype<usize>(codes) + (j << 1), toLower8(c));
565
+ } else {
566
+ // check and read surrogate pair
567
+ if (c - 0xd7ff < 0xdc00 - 0xd7ff && i < len - 1) {
568
+ let c1 = <u32>load<u16>(changetype<usize>(this) + (i << 1), 2);
569
+ if (c1 - 0xdbff < 0xe000 - 0xdbff) {
570
+ let c0 = c;
571
+ c = (((c & 0x03ff) << 10) | (c1 & 0x03ff)) + 0x10000;
572
+ ++i;
573
+ if (c >= 0x20000) {
574
+ store<u32>(changetype<usize>(codes) + (j << 1), c0 | (c1 << 16));
575
+ ++j;
576
+ continue;
577
+ }
578
+ }
579
+ }
580
+ // check special casing for lower table. It has one ently so instead lookup we just inline this.
581
+ if (c == 0x0130) {
582
+ // 0x0130 -> [0x0069, 0x0307]
583
+ store<u32>(changetype<usize>(codes) + (j << 1), (0x0307 << 16) | 0x0069);
584
+ ++j;
585
+ } else if (c == 0x03a3) {
586
+ // 'Σ'
587
+ // Σ maps to σ but except at the end of a word where it maps to ς
588
+ let sigma = 0x03c3; // σ
589
+ if (len > 1 && isFinalSigma(changetype<usize>(this), i, len)) {
590
+ sigma = 0x03c2; // ς
591
+ }
592
+ store<u16>(changetype<usize>(codes) + (j << 1), sigma);
593
+ } else if (c - 0x24b6 <= 0x24cf - 0x24b6) {
594
+ // Range 0x24B6 <= c <= 0x24CF not covered by casemap and require special early handling
595
+ store<u16>(changetype<usize>(codes) + (j << 1), c + 26);
596
+ } else {
597
+ let code = casemap(c, 0) & 0x1fffff;
598
+ if (code < 0x10000) {
599
+ store<u16>(changetype<usize>(codes) + (j << 1), code);
600
+ } else {
601
+ // store as surrogare pair
602
+ code -= 0x10000;
603
+ let lo = (code >>> 10) | 0xd800;
604
+ let hi = (code & 0x03ff) | 0xdc00;
605
+ store<u32>(changetype<usize>(codes) + (j << 1), lo | (hi << 16));
606
+ ++j;
607
+ }
608
+ }
609
+ }
610
+ }
611
+ return changetype<String>(__renew(changetype<usize>(codes), j << 1));
612
+ }
613
+
614
+ toUpperCase(): String {
615
+ let len = <usize>this.length;
616
+ if (!len) return this;
617
+ let codes = changetype<String>(__new(len * 3 * 2, idof<String>()));
618
+ let specialsPtr = changetype<usize>(SPECIALS_UPPER);
619
+ let specialsLen = SPECIALS_UPPER.length;
620
+ let j: usize = 0;
621
+ for (let i: usize = 0; i < len; ++i, ++j) {
622
+ let c = <u32>load<u16>(changetype<usize>(this) + (i << 1));
623
+ if (isAscii(c)) {
624
+ store<u16>(changetype<usize>(codes) + (j << 1), toUpper8(c));
625
+ } else {
626
+ // check and read surrogate pair
627
+ if (c - 0xd7ff < 0xdc00 - 0xd7ff && i < len - 1) {
628
+ let c1 = <u32>load<u16>(changetype<usize>(this) + (i << 1), 2);
629
+ if (c1 - 0xdbff < 0xe000 - 0xdbff) {
630
+ let c0 = c;
631
+ c = (((c & 0x03ff) << 10) | (c1 & 0x03ff)) + 0x10000;
632
+ ++i;
633
+ if (c >= 0x20000) {
634
+ store<u32>(changetype<usize>(codes) + (j << 1), c0 | (c1 << 16));
635
+ ++j;
636
+ continue;
637
+ }
638
+ }
639
+ }
640
+ // Range 0x24D0 <= c <= 0x24E9 not covered by casemap and require special early handling
641
+ if (c - 0x24d0 <= 0x24e9 - 0x24d0) {
642
+ // monkey patch
643
+ store<u16>(changetype<usize>(codes) + (j << 1), c - 26);
644
+ } else {
645
+ let index: usize = -1;
646
+ // Fast range check. See first and last rows in specialsUpper table
647
+ if (c - 0x00df <= 0xfb17 - 0x00df) {
648
+ index = <usize>bsearch(c, specialsPtr, specialsLen);
649
+ }
650
+ if (~index) {
651
+ // load next 3 code points from row with `index` offset for specialsUpper table
652
+ let ab = load<u32>(specialsPtr + (index << 1), 2);
653
+ let cc = load<u16>(specialsPtr + (index << 1), 6);
654
+ store<u32>(changetype<usize>(codes) + (j << 1), ab, 0);
655
+ store<u16>(changetype<usize>(codes) + (j << 1), cc, 4);
656
+ j += 1 + usize(cc != 0);
657
+ } else {
658
+ let code = casemap(c, 1) & 0x1fffff;
659
+ if (code < 0x10000) {
660
+ store<u16>(changetype<usize>(codes) + (j << 1), code);
661
+ } else {
662
+ // store as surrogare pair
663
+ code -= 0x10000;
664
+ let lo = (code >>> 10) | 0xd800;
665
+ let hi = (code & 0x03ff) | 0xdc00;
666
+ store<u32>(changetype<usize>(codes) + (j << 1), lo | (hi << 16));
667
+ ++j;
668
+ }
669
+ }
670
+ }
671
+ }
672
+ }
673
+ return changetype<String>(__renew(changetype<usize>(codes), j << 1));
674
+ }
675
+
676
+ toString(): String {
677
+ return this;
678
+ }
679
+ }
680
+
681
+ // @ts-ignore: nolib
682
+ export type string = String;
683
+
684
+ export function parseInt(str: string, radix: i32 = 0): f64 {
685
+ return strtol<f64>(str, radix);
686
+ }
687
+
688
+ export function parseFloat(str: string): f64 {
689
+ return strtod(str);
690
+ }
691
+
692
+ // Encoding helpers
693
+ export namespace String {
694
+ export namespace UTF8 {
695
+ export const enum ErrorMode {
696
+ WTF8,
697
+ REPLACE,
698
+ ERROR,
699
+ }
700
+
701
+ export function byteLength(str: string, nullTerminated: bool = false): i32 {
702
+ let strOff = changetype<usize>(str);
703
+ let strEnd = strOff + <usize>changetype<OBJECT>(changetype<usize>(str) - TOTAL_OVERHEAD).rtSize;
704
+ let bufLen = i32(nullTerminated);
705
+ while (strOff < strEnd) {
706
+ let c1 = <u32>load<u16>(strOff);
707
+ if (c1 < 128) {
708
+ // @ts-ignore: cast
709
+ if (nullTerminated & !c1) break;
710
+ bufLen += 1;
711
+ } else if (c1 < 2048) {
712
+ bufLen += 2;
713
+ } else {
714
+ if ((c1 & 0xfc00) == 0xd800 && strOff + 2 < strEnd) {
715
+ if (((<u32>load<u16>(strOff, 2)) & 0xfc00) == 0xdc00) {
716
+ bufLen += 4;
717
+ strOff += 4;
718
+ continue;
719
+ }
720
+ }
721
+ bufLen += 3;
722
+ }
723
+ strOff += 2;
724
+ }
725
+ return bufLen;
726
+ }
727
+
728
+ export function encode(
729
+ str: string,
730
+ nullTerminated: bool = false,
731
+ errorMode: ErrorMode = ErrorMode.WTF8
732
+ ): ArrayBuffer {
733
+ let buf = changetype<ArrayBuffer>(__new(<usize>byteLength(str, nullTerminated), idof<ArrayBuffer>()));
734
+ encodeUnsafe(changetype<usize>(str), str.length, changetype<usize>(buf), nullTerminated, errorMode);
735
+ return buf;
736
+ }
737
+
738
+ // @ts-ignore: decorator
739
+ @unsafe
740
+ export function encodeUnsafe(
741
+ str: usize,
742
+ len: i32,
743
+ buf: usize,
744
+ nullTerminated: bool = false,
745
+ errorMode: ErrorMode = ErrorMode.WTF8
746
+ ): usize {
747
+ let strEnd = str + ((<usize>len) << 1);
748
+ let bufOff = buf;
749
+ while (str < strEnd) {
750
+ let c1 = <u32>load<u16>(str);
751
+ if (c1 < 128) {
752
+ store<u8>(bufOff, c1);
753
+ bufOff++;
754
+ // @ts-ignore: cast
755
+ if (nullTerminated & !c1) return bufOff - buf;
756
+ } else if (c1 < 2048) {
757
+ let b0 = (c1 >> 6) | 192;
758
+ let b1 = (c1 & 63) | 128;
759
+ store<u16>(bufOff, (b1 << 8) | b0);
760
+ bufOff += 2;
761
+ } else {
762
+ // D800: 11011 0 0000000000 Lead
763
+ // DBFF: 11011 0 1111111111
764
+ // DC00: 11011 1 0000000000 Trail
765
+ // DFFF: 11011 1 1111111111
766
+ // F800: 11111 0 0000000000 Mask
767
+ // FC00: 11111 1 0000000000
768
+ if ((c1 & 0xf800) == 0xd800) {
769
+ if (c1 < 0xdc00 && str + 2 < strEnd) {
770
+ let c2 = <u32>load<u16>(str, 2);
771
+ if ((c2 & 0xfc00) == 0xdc00) {
772
+ c1 = (0x10000 + ((c1 & 0x03ff) << 10)) | (c2 & 0x03ff);
773
+ let b0 = (c1 >> 18) | 240;
774
+ let b1 = ((c1 >> 12) & 63) | 128;
775
+ let b2 = ((c1 >> 6) & 63) | 128;
776
+ let b3 = (c1 & 63) | 128;
777
+ store<u32>(bufOff, (b3 << 24) | (b2 << 16) | (b1 << 8) | b0);
778
+ bufOff += 4;
779
+ str += 4;
780
+ continue;
781
+ }
782
+ }
783
+ if (errorMode != ErrorMode.WTF8) {
784
+ // unlikely
785
+ if (errorMode == ErrorMode.ERROR) throw new Error(E_UNPAIRED_SURROGATE);
786
+ c1 = 0xfffd;
787
+ }
788
+ }
789
+ let b0 = (c1 >> 12) | 224;
790
+ let b1 = ((c1 >> 6) & 63) | 128;
791
+ let b2 = (c1 & 63) | 128;
792
+ store<u16>(bufOff, (b1 << 8) | b0);
793
+ store<u8>(bufOff, b2, 2);
794
+ bufOff += 3;
795
+ }
796
+ str += 2;
797
+ }
798
+ if (nullTerminated) {
799
+ store<u8>(bufOff++, 0);
800
+ }
801
+ return bufOff - buf;
802
+ }
803
+
804
+ export function decode(buf: ArrayBuffer, nullTerminated: bool = false): String {
805
+ return decodeUnsafe(changetype<usize>(buf), buf.byteLength, nullTerminated);
806
+ }
807
+
808
+ // @ts-ignore: decorator
809
+ @unsafe
810
+ export function decodeUnsafe(buf: usize, len: usize, nullTerminated: bool = false): String {
811
+ let bufOff = buf;
812
+ let bufEnd = buf + len;
813
+ assert(bufEnd >= bufOff); // guard wraparound
814
+ let str = changetype<String>(__new(len << 1, idof<String>())); // max is one u16 char per u8 byte
815
+ let strOff = changetype<usize>(str);
816
+ while (bufOff < bufEnd) {
817
+ let u0 = <u32>load<u8>(bufOff);
818
+ ++bufOff;
819
+ if (!(u0 & 128)) {
820
+ // @ts-ignore: cast
821
+ if (nullTerminated & !u0) break;
822
+ store<u16>(strOff, u0);
823
+ } else {
824
+ if (bufEnd == bufOff) break;
825
+ let u1 = (<u32>load<u8>(bufOff)) & 63;
826
+ ++bufOff;
827
+ if ((u0 & 224) == 192) {
828
+ store<u16>(strOff, ((u0 & 31) << 6) | u1);
829
+ } else {
830
+ if (bufEnd == bufOff) break;
831
+ let u2 = (<u32>load<u8>(bufOff)) & 63;
832
+ ++bufOff;
833
+ if ((u0 & 240) == 224) {
834
+ u0 = ((u0 & 15) << 12) | (u1 << 6) | u2;
835
+ } else {
836
+ if (bufEnd == bufOff) break;
837
+ u0 = ((u0 & 7) << 18) | (u1 << 12) | (u2 << 6) | ((<u32>load<u8>(bufOff)) & 63);
838
+ ++bufOff;
839
+ }
840
+ if (u0 < 0x10000) {
841
+ store<u16>(strOff, u0);
842
+ } else {
843
+ u0 -= 0x10000;
844
+ let lo = (u0 >> 10) | 0xd800;
845
+ let hi = (u0 & 0x03ff) | 0xdc00;
846
+ store<u32>(strOff, lo | (hi << 16));
847
+ strOff += 2;
848
+ }
849
+ }
850
+ }
851
+ strOff += 2;
852
+ }
853
+ return changetype<String>(__renew(changetype<usize>(str), strOff - changetype<usize>(str)));
854
+ }
855
+ }
856
+
857
+ export namespace UTF16 {
858
+ export function byteLength(str: string): i32 {
859
+ return changetype<OBJECT>(changetype<usize>(str) - TOTAL_OVERHEAD).rtSize;
860
+ }
861
+
862
+ export function encode(str: string): ArrayBuffer {
863
+ let buf = changetype<ArrayBuffer>(__new(<usize>byteLength(str), idof<ArrayBuffer>()));
864
+ encodeUnsafe(changetype<usize>(str), str.length, changetype<usize>(buf));
865
+ return buf;
866
+ }
867
+
868
+ // @ts-ignore: decorator
869
+ @unsafe
870
+ export function encodeUnsafe(str: usize, len: i32, buf: usize): usize {
871
+ let size = (<usize>len) << 1;
872
+ memory.copy(buf, changetype<usize>(str), size);
873
+ return size;
874
+ }
875
+
876
+ export function decode(buf: ArrayBuffer): String {
877
+ return decodeUnsafe(changetype<usize>(buf), buf.byteLength);
878
+ }
879
+
880
+ // @ts-ignore: decorator
881
+ @unsafe
882
+ export function decodeUnsafe(buf: usize, len: usize): String {
883
+ let str = changetype<String>(__new((len &= ~1), idof<String>()));
884
+ memory.copy(changetype<usize>(str), buf, len);
885
+ return str;
886
+ }
887
+ }
888
+ }
889
+
890
+ export class TemplateStringsArray extends Array<string> {
891
+ readonly raw: string[];
892
+ }