goscript 0.0.38 → 0.0.40

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 (180) hide show
  1. package/compiler/analysis.go +15 -6
  2. package/compiler/compiler.go +184 -34
  3. package/compiler/expr-call.go +19 -9
  4. package/compiler/field.go +17 -3
  5. package/compiler/gs_dependencies_test.go +80 -0
  6. package/compiler/lit.go +12 -6
  7. package/compiler/output.go +10 -4
  8. package/compiler/spec.go +15 -2
  9. package/compiler/type-assert.go +111 -21
  10. package/compiler/type.go +37 -8
  11. package/dist/gs/builtin/builtin.d.ts +55 -0
  12. package/dist/gs/builtin/builtin.js +213 -0
  13. package/dist/gs/builtin/builtin.js.map +1 -1
  14. package/dist/gs/builtin/slice.js +13 -0
  15. package/dist/gs/builtin/slice.js.map +1 -1
  16. package/dist/gs/bytes/buffer.gs.d.ts +56 -0
  17. package/dist/gs/bytes/buffer.gs.js +611 -0
  18. package/dist/gs/bytes/buffer.gs.js.map +1 -0
  19. package/dist/gs/bytes/bytes.gs.d.ts +78 -0
  20. package/dist/gs/bytes/bytes.gs.js +1107 -0
  21. package/dist/gs/bytes/bytes.gs.js.map +1 -0
  22. package/dist/gs/bytes/index.d.ts +4 -0
  23. package/dist/gs/bytes/index.js +5 -0
  24. package/dist/gs/bytes/index.js.map +1 -0
  25. package/dist/gs/bytes/iter.gs.d.ts +9 -0
  26. package/dist/gs/bytes/iter.gs.js +143 -0
  27. package/dist/gs/bytes/iter.gs.js.map +1 -0
  28. package/dist/gs/bytes/reader.gs.d.ts +34 -0
  29. package/dist/gs/bytes/reader.gs.js +198 -0
  30. package/dist/gs/bytes/reader.gs.js.map +1 -0
  31. package/dist/gs/fmt/fmt.d.ts +49 -0
  32. package/dist/gs/fmt/fmt.js +322 -0
  33. package/dist/gs/fmt/fmt.js.map +1 -0
  34. package/dist/gs/fmt/index.d.ts +1 -0
  35. package/dist/gs/fmt/index.js +2 -0
  36. package/dist/gs/fmt/index.js.map +1 -0
  37. package/dist/gs/internal/bytealg/index.d.ts +14 -2
  38. package/dist/gs/internal/bytealg/index.js +114 -8
  39. package/dist/gs/internal/bytealg/index.js.map +1 -1
  40. package/dist/gs/path/filepath/index.d.ts +3 -0
  41. package/dist/gs/path/filepath/index.js +3 -0
  42. package/dist/gs/path/filepath/index.js.map +1 -0
  43. package/dist/gs/path/filepath/match.d.ts +3 -0
  44. package/dist/gs/path/filepath/match.js +212 -0
  45. package/dist/gs/path/filepath/match.js.map +1 -0
  46. package/dist/gs/path/filepath/path.d.ts +25 -0
  47. package/dist/gs/path/filepath/path.js +265 -0
  48. package/dist/gs/path/filepath/path.js.map +1 -0
  49. package/dist/gs/reflect/deepequal.d.ts +2 -1
  50. package/dist/gs/reflect/deepequal.js +5 -53
  51. package/dist/gs/reflect/deepequal.js.map +1 -1
  52. package/dist/gs/reflect/map.d.ts +14 -8
  53. package/dist/gs/reflect/map.js +15 -11
  54. package/dist/gs/reflect/map.js.map +1 -1
  55. package/dist/gs/reflect/type.d.ts +17 -9
  56. package/dist/gs/reflect/type.js +1 -1
  57. package/dist/gs/reflect/type.js.map +1 -1
  58. package/dist/gs/reflect/value.js +15 -6
  59. package/dist/gs/reflect/value.js.map +1 -1
  60. package/dist/gs/reflect/visiblefields.js +18 -12
  61. package/dist/gs/reflect/visiblefields.js.map +1 -1
  62. package/dist/gs/sort/index.d.ts +4 -0
  63. package/dist/gs/sort/index.js +4 -0
  64. package/dist/gs/sort/index.js.map +1 -0
  65. package/dist/gs/sort/search.gs.d.ts +6 -0
  66. package/dist/gs/sort/search.gs.js +125 -0
  67. package/dist/gs/sort/search.gs.js.map +1 -0
  68. package/dist/gs/sort/slice.gs.d.ts +4 -0
  69. package/dist/gs/sort/slice.gs.js +49 -0
  70. package/dist/gs/sort/slice.gs.js.map +1 -0
  71. package/dist/gs/sort/sort.gs.d.ts +37 -0
  72. package/dist/gs/sort/sort.gs.js +203 -0
  73. package/dist/gs/sort/sort.gs.js.map +1 -0
  74. package/dist/gs/unicode/utf8/utf8.d.ts +1 -1
  75. package/dist/gs/unicode/utf8/utf8.js +4 -2
  76. package/dist/gs/unicode/utf8/utf8.js.map +1 -1
  77. package/gs/builtin/builtin.ts +236 -0
  78. package/gs/builtin/slice.ts +17 -1
  79. package/gs/bytes/buffer.gs.ts +614 -0
  80. package/gs/bytes/bytes.gs.ts +1288 -0
  81. package/gs/bytes/godoc.txt +69 -0
  82. package/gs/bytes/index.ts +69 -0
  83. package/gs/bytes/iter.gs.ts +149 -0
  84. package/gs/bytes/metadata.go +12 -0
  85. package/gs/bytes/reader.gs.ts +230 -0
  86. package/gs/fmt/fmt.ts +407 -0
  87. package/gs/fmt/godoc.txt +382 -0
  88. package/gs/fmt/index.ts +31 -0
  89. package/gs/fmt/metadata.go +7 -0
  90. package/gs/internal/bytealg/index.ts +125 -10
  91. package/gs/internal/metadata.go +7 -0
  92. package/gs/io/metadata.go +11 -0
  93. package/gs/maps/metadata.go +8 -0
  94. package/gs/math/metadata.go +7 -0
  95. package/gs/os/metadata.go +17 -0
  96. package/gs/path/filepath/godoc.txt +35 -0
  97. package/gs/path/filepath/index.ts +27 -0
  98. package/gs/path/filepath/match.test.ts +274 -0
  99. package/gs/path/filepath/match.ts +249 -0
  100. package/gs/path/filepath/path.test.ts +246 -0
  101. package/gs/path/filepath/path.ts +328 -0
  102. package/gs/path/metadata.go +8 -0
  103. package/gs/reflect/deepequal.test.ts +41 -0
  104. package/gs/reflect/deepequal.ts +19 -4
  105. package/gs/reflect/map.test.ts +30 -0
  106. package/gs/reflect/map.ts +22 -18
  107. package/gs/reflect/metadata.go +7 -0
  108. package/gs/reflect/type.ts +19 -15
  109. package/gs/reflect/value.ts +21 -7
  110. package/gs/reflect/visiblefields.ts +17 -13
  111. package/gs/sort/godoc.txt +27 -0
  112. package/gs/sort/index.ts +24 -0
  113. package/gs/sort/search.gs.ts +128 -0
  114. package/gs/sort/slice.gs.ts +59 -0
  115. package/gs/sort/sort.gs.ts +227 -0
  116. package/gs/strconv/metadata.go +7 -0
  117. package/gs/strings/metadata.go +11 -0
  118. package/gs/sync/metadata.go +7 -0
  119. package/gs/unicode/utf8/utf8.ts +8 -5
  120. package/package.json +1 -1
  121. package/dist/gs/internal/testlog/index.d.ts +0 -1
  122. package/dist/gs/internal/testlog/index.js +0 -5
  123. package/dist/gs/internal/testlog/index.js.map +0 -1
  124. package/dist/gs/maps/iter.gs.d.ts +0 -7
  125. package/dist/gs/maps/iter.gs.js +0 -65
  126. package/dist/gs/maps/iter.gs.js.map +0 -1
  127. package/dist/gs/maps/maps.gs.d.ts +0 -7
  128. package/dist/gs/maps/maps.gs.js +0 -79
  129. package/dist/gs/maps/maps.gs.js.map +0 -1
  130. package/dist/gs/reflect/abi.d.ts +0 -59
  131. package/dist/gs/reflect/abi.gs.d.ts +0 -59
  132. package/dist/gs/reflect/abi.gs.js +0 -79
  133. package/dist/gs/reflect/abi.gs.js.map +0 -1
  134. package/dist/gs/reflect/abi.js +0 -79
  135. package/dist/gs/reflect/abi.js.map +0 -1
  136. package/dist/gs/reflect/badlinkname.d.ts +0 -52
  137. package/dist/gs/reflect/badlinkname.gs.d.ts +0 -52
  138. package/dist/gs/reflect/badlinkname.gs.js +0 -72
  139. package/dist/gs/reflect/badlinkname.gs.js.map +0 -1
  140. package/dist/gs/reflect/badlinkname.js +0 -72
  141. package/dist/gs/reflect/badlinkname.js.map +0 -1
  142. package/dist/gs/reflect/deepequal.gs.d.ts +0 -25
  143. package/dist/gs/reflect/deepequal.gs.js +0 -308
  144. package/dist/gs/reflect/deepequal.gs.js.map +0 -1
  145. package/dist/gs/reflect/float32reg_generic.gs.d.ts +0 -2
  146. package/dist/gs/reflect/float32reg_generic.gs.js +0 -10
  147. package/dist/gs/reflect/float32reg_generic.gs.js.map +0 -1
  148. package/dist/gs/reflect/index.gs.d.ts +0 -1
  149. package/dist/gs/reflect/index.gs.js +0 -3
  150. package/dist/gs/reflect/index.gs.js.map +0 -1
  151. package/dist/gs/reflect/iter.gs.d.ts +0 -3
  152. package/dist/gs/reflect/iter.gs.js +0 -24
  153. package/dist/gs/reflect/iter.gs.js.map +0 -1
  154. package/dist/gs/reflect/makefunc.gs.d.ts +0 -34
  155. package/dist/gs/reflect/makefunc.gs.js +0 -288
  156. package/dist/gs/reflect/makefunc.gs.js.map +0 -1
  157. package/dist/gs/reflect/map_swiss.gs.d.ts +0 -14
  158. package/dist/gs/reflect/map_swiss.gs.js +0 -70
  159. package/dist/gs/reflect/map_swiss.gs.js.map +0 -1
  160. package/dist/gs/reflect/reflect.gs.d.ts +0 -132
  161. package/dist/gs/reflect/reflect.gs.js +0 -437
  162. package/dist/gs/reflect/reflect.gs.js.map +0 -1
  163. package/dist/gs/reflect/swapper.gs.d.ts +0 -1
  164. package/dist/gs/reflect/swapper.gs.js +0 -32
  165. package/dist/gs/reflect/swapper.gs.js.map +0 -1
  166. package/dist/gs/reflect/type.gs.d.ts +0 -4
  167. package/dist/gs/reflect/type.gs.js +0 -21
  168. package/dist/gs/reflect/type.gs.js.map +0 -1
  169. package/dist/gs/reflect/value.gs.d.ts +0 -4
  170. package/dist/gs/reflect/value.gs.js +0 -12
  171. package/dist/gs/reflect/value.gs.js.map +0 -1
  172. package/dist/gs/reflect/visiblefields.gs.d.ts +0 -3
  173. package/dist/gs/reflect/visiblefields.gs.js +0 -123
  174. package/dist/gs/reflect/visiblefields.gs.js.map +0 -1
  175. package/dist/gs/stringslite/index.d.ts +0 -1
  176. package/dist/gs/stringslite/index.js +0 -2
  177. package/dist/gs/stringslite/index.js.map +0 -1
  178. package/dist/gs/stringslite/strings.d.ts +0 -11
  179. package/dist/gs/stringslite/strings.js +0 -67
  180. package/dist/gs/stringslite/strings.js.map +0 -1
@@ -0,0 +1,1107 @@
1
+ import * as $ from "@goscript/builtin/builtin.js";
2
+ import * as unicode from "@goscript/unicode/index.js";
3
+ import * as utf8 from "@goscript/unicode/utf8/index.js";
4
+ // Equal reports whether a and b
5
+ // are the same length and contain the same bytes.
6
+ // A nil argument is equivalent to an empty slice.
7
+ export function Equal(a, b) {
8
+ return $.bytesEqual(a, b);
9
+ }
10
+ // Compare returns an integer comparing two byte slices lexicographically.
11
+ // The result will be 0 if a == b, -1 if a < b, and +1 if a > b.
12
+ // A nil argument is equivalent to an empty slice.
13
+ export function Compare(a, b) {
14
+ return $.bytesCompare(a, b);
15
+ }
16
+ // explode splits s into a slice of UTF-8 sequences, one per Unicode code point (still slices of bytes),
17
+ // up to a maximum of n byte slices. Invalid UTF-8 sequences are chopped into individual bytes.
18
+ export function explode(s, n) {
19
+ if (n === 0) {
20
+ return null;
21
+ }
22
+ if (s === null || $.len(s) === 0) {
23
+ return null;
24
+ }
25
+ const result = [];
26
+ let i = 0;
27
+ while (i < $.len(s) && (n < 0 || result.length < n)) {
28
+ const [, size] = utf8.DecodeRune($.goSlice(s, i, undefined));
29
+ if (size <= 0) {
30
+ // Invalid UTF-8, take single byte
31
+ result.push($.goSlice(s, i, i + 1));
32
+ i++;
33
+ }
34
+ else {
35
+ result.push($.goSlice(s, i, i + size));
36
+ i += size;
37
+ }
38
+ }
39
+ // If we have remaining bytes and haven't reached n limit, add the rest
40
+ if (i < $.len(s) && (n < 0 || result.length < n)) {
41
+ result.push($.goSlice(s, i, undefined));
42
+ }
43
+ return $.arrayToSlice(result);
44
+ }
45
+ // Count counts the number of non-overlapping instances of sep in s.
46
+ // If sep is an empty slice, Count returns 1 + the number of UTF-8-encoded code points in s.
47
+ export function Count(s, sep) {
48
+ // Special case for empty separator
49
+ if (sep === null || $.len(sep) === 0) {
50
+ if (s === null)
51
+ return 1;
52
+ // For now, use simple byte count + 1 (TODO: proper UTF-8 rune counting)
53
+ return $.len(s) + 1;
54
+ }
55
+ // Single byte separator - optimized path
56
+ if ($.len(sep) === 1) {
57
+ return $.bytesCount(s, sep);
58
+ }
59
+ return $.bytesCount(s, sep);
60
+ }
61
+ // Contains reports whether subslice is within b.
62
+ export function Contains(b, subslice) {
63
+ return Index(b, subslice) !== -1;
64
+ }
65
+ // ContainsAny reports whether any of the UTF-8-encoded code points in chars are within b.
66
+ export function ContainsAny(b, chars) {
67
+ return IndexAny(b, chars) >= 0;
68
+ }
69
+ // ContainsRune reports whether the rune is contained in the UTF-8-encoded byte slice b.
70
+ export function ContainsRune(b, r) {
71
+ return IndexRune(b, r) >= 0;
72
+ }
73
+ // ContainsFunc reports whether any of the UTF-8-encoded code points r within b satisfy f(r).
74
+ export function ContainsFunc(b, f) {
75
+ return IndexFunc(b, f) >= 0;
76
+ }
77
+ // IndexByte returns the index of the first instance of c in b, or -1 if c is not present in b.
78
+ export function IndexByte(b, c) {
79
+ return $.bytesIndexByte(b, c);
80
+ }
81
+ export function indexBytePortable(s, c) {
82
+ if (s === null)
83
+ return -1;
84
+ const arr = $.bytesToArray(s);
85
+ return arr.indexOf(c);
86
+ }
87
+ // LastIndex returns the index of the last instance of sep in s, or -1 if sep is not present in s.
88
+ export function LastIndex(s, sep) {
89
+ if (sep === null || $.len(sep) === 0) {
90
+ return s === null ? 0 : $.len(s);
91
+ }
92
+ if ($.len(sep) === 1) {
93
+ return $.bytesLastIndexByte(s, sep[0]);
94
+ }
95
+ return $.bytesLastIndexOf(s, sep);
96
+ }
97
+ // LastIndexByte returns the index of the last instance of c in s, or -1 if c is not present in s.
98
+ export function LastIndexByte(s, c) {
99
+ return $.bytesLastIndexByte(s, c);
100
+ }
101
+ // IndexRune interprets s as a sequence of UTF-8-encoded code points.
102
+ // It returns the byte index of the first occurrence in s of the given rune.
103
+ // It returns -1 if rune is not present in s.
104
+ // If r is [utf8.RuneError], it returns the first instance of any
105
+ // invalid UTF-8 byte sequence.
106
+ export function IndexRune(s, r) {
107
+ if (s === null) {
108
+ return -1;
109
+ }
110
+ if (r < utf8.RuneSelf) {
111
+ // ASCII case - use IndexByte for efficiency
112
+ return IndexByte(s, r);
113
+ }
114
+ if (r === utf8.RuneError) {
115
+ // Look for invalid UTF-8 sequences
116
+ for (let i = 0; i < $.len(s);) {
117
+ const [r1, size] = utf8.DecodeRune($.goSlice(s, i, undefined));
118
+ if (r1 === utf8.RuneError && size === 1) {
119
+ return i;
120
+ }
121
+ if (size <= 0) {
122
+ return i;
123
+ }
124
+ i += size;
125
+ }
126
+ return -1;
127
+ }
128
+ // Encode the rune to bytes and search for it
129
+ const runeBytes = new Uint8Array(4);
130
+ const n = utf8.EncodeRune(runeBytes, r);
131
+ const needle = $.goSlice(runeBytes, 0, n);
132
+ return Index(s, needle);
133
+ }
134
+ // IndexAny interprets s as a sequence of UTF-8-encoded Unicode code points.
135
+ // It returns the byte index of the first occurrence in s of any of the Unicode
136
+ // code points in chars. It returns -1 if chars is empty or if there is no code
137
+ // point in common.
138
+ export function IndexAny(s, chars) {
139
+ if (s === null || chars.length === 0) {
140
+ return -1;
141
+ }
142
+ // Check if all chars are ASCII for optimization
143
+ let allASCII = true;
144
+ for (let i = 0; i < chars.length; i++) {
145
+ if (chars.charCodeAt(i) >= utf8.RuneSelf) {
146
+ allASCII = false;
147
+ break;
148
+ }
149
+ }
150
+ if (allASCII) {
151
+ // ASCII optimization
152
+ for (let i = 0; i < $.len(s); i++) {
153
+ const b = s[i];
154
+ if (b < utf8.RuneSelf && chars.indexOf(String.fromCharCode(b)) >= 0) {
155
+ return i;
156
+ }
157
+ }
158
+ return -1;
159
+ }
160
+ // Full UTF-8 handling
161
+ for (let i = 0; i < $.len(s);) {
162
+ const [r, size] = utf8.DecodeRune($.goSlice(s, i, undefined));
163
+ if (size <= 0) {
164
+ i++;
165
+ continue;
166
+ }
167
+ // Check if this rune is in chars
168
+ if (containsRune(chars, r)) {
169
+ return i;
170
+ }
171
+ i += size;
172
+ }
173
+ return -1;
174
+ }
175
+ // LastIndexAny interprets s as a sequence of UTF-8-encoded Unicode code
176
+ // points. It returns the byte index of the last occurrence in s of any of
177
+ // the Unicode code points in chars. It returns -1 if chars is empty or if
178
+ // there is no code point in common.
179
+ export function LastIndexAny(s, chars) {
180
+ if (s === null || chars.length === 0) {
181
+ return -1;
182
+ }
183
+ // Check if all chars are ASCII for optimization
184
+ let allASCII = true;
185
+ for (let i = 0; i < chars.length; i++) {
186
+ if (chars.charCodeAt(i) >= utf8.RuneSelf) {
187
+ allASCII = false;
188
+ break;
189
+ }
190
+ }
191
+ if (allASCII) {
192
+ // ASCII optimization - search backwards
193
+ for (let i = $.len(s) - 1; i >= 0; i--) {
194
+ const b = s[i];
195
+ if (b < utf8.RuneSelf && chars.indexOf(String.fromCharCode(b)) >= 0) {
196
+ return i;
197
+ }
198
+ }
199
+ return -1;
200
+ }
201
+ // Full UTF-8 handling - need to scan forward to find rune boundaries
202
+ let lastIndex = -1;
203
+ for (let i = 0; i < $.len(s);) {
204
+ const [r, size] = utf8.DecodeRune($.goSlice(s, i, undefined));
205
+ if (size <= 0) {
206
+ i++;
207
+ continue;
208
+ }
209
+ // Check if this rune is in chars
210
+ if (containsRune(chars, r)) {
211
+ lastIndex = i;
212
+ }
213
+ i += size;
214
+ }
215
+ return lastIndex;
216
+ }
217
+ // Generic split: splits after each instance of sep,
218
+ // including sepSave bytes of sep in the subslices.
219
+ export function genSplit(s, sep, sepSave, n) {
220
+ if (n === 0) {
221
+ return null;
222
+ }
223
+ if (sep === null || $.len(sep) === 0) {
224
+ return explode(s, n);
225
+ }
226
+ if (n < 0) {
227
+ n = Count(s, sep) + 1;
228
+ }
229
+ if (n > Count(s, sep) + 1) {
230
+ n = Count(s, sep) + 1;
231
+ }
232
+ const result = [];
233
+ let start = 0;
234
+ for (let i = 0; i < n - 1; i++) {
235
+ const m = Index($.goSlice(s, start, undefined), sep);
236
+ if (m < 0) {
237
+ break;
238
+ }
239
+ const end = start + m + sepSave;
240
+ result.push($.goSlice(s, start, end));
241
+ start += m + $.len(sep);
242
+ }
243
+ // Add the remaining part
244
+ result.push($.goSlice(s, start, undefined));
245
+ return $.arrayToSlice(result);
246
+ }
247
+ // SplitN slices s into subslices separated by sep and returns a slice of
248
+ // the subslices between those separators.
249
+ // If sep is empty, SplitN splits after each UTF-8 sequence.
250
+ // The count determines the number of subslices to return:
251
+ // - n > 0: at most n subslices; the last subslice will be the unsplit remainder;
252
+ // - n == 0: the result is nil (zero subslices);
253
+ // - n < 0: all subslices.
254
+ //
255
+ // To split around the first instance of a separator, see [Cut].
256
+ export function SplitN(s, sep, n) {
257
+ return genSplit(s, sep, 0, n);
258
+ }
259
+ // SplitAfterN slices s into subslices after each instance of sep and
260
+ // returns a slice of those subslices.
261
+ // If sep is empty, SplitAfterN splits after each UTF-8 sequence.
262
+ // The count determines the number of subslices to return:
263
+ // - n > 0: at most n subslices; the last subslice will be the unsplit remainder;
264
+ // - n == 0: the result is nil (zero subslices);
265
+ // - n < 0: all subslices.
266
+ export function SplitAfterN(s, sep, n) {
267
+ return genSplit(s, sep, $.len(sep), n);
268
+ }
269
+ // Split slices s into all subslices separated by sep and returns a slice of
270
+ // the subslices between those separators.
271
+ // If sep is empty, Split splits after each UTF-8 sequence.
272
+ // It is equivalent to SplitN with a count of -1.
273
+ //
274
+ // To split around the first instance of a separator, see [Cut].
275
+ export function Split(s, sep) {
276
+ return genSplit(s, sep, 0, -1);
277
+ }
278
+ // SplitAfter slices s into all subslices after each instance of sep and
279
+ // returns a slice of those subslices.
280
+ // If sep is empty, SplitAfter splits after each UTF-8 sequence.
281
+ // It is equivalent to SplitAfterN with a count of -1.
282
+ export function SplitAfter(s, sep) {
283
+ return genSplit(s, sep, $.len(sep), -1);
284
+ }
285
+ export let asciiSpace = $.arrayToSlice([0, 0, 0, 0, 0, 0, 0, 0, 0, 1, 1, 1, 1, 1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0]);
286
+ // Fields interprets s as a sequence of UTF-8-encoded code points.
287
+ // It splits the slice s around each instance of one or more consecutive white space
288
+ // characters, as defined by [unicode.IsSpace], returning a slice of subslices of s or an
289
+ // empty slice if s contains only white space.
290
+ export function Fields(s) {
291
+ // Use FieldsFunc with unicode.IsSpace
292
+ return FieldsFunc(s, unicode.IsSpace);
293
+ }
294
+ // FieldsFunc interprets s as a sequence of UTF-8-encoded code points.
295
+ // It splits the slice s at each run of code points c satisfying f(c) and
296
+ // returns a slice of subslices of s. If all code points in s satisfy f(c), or
297
+ // len(s) == 0, an empty slice is returned.
298
+ //
299
+ // FieldsFunc makes no guarantees about the order in which it calls f(c)
300
+ // and assumes that f always returns the same value for a given c.
301
+ export function FieldsFunc(s, f) {
302
+ if (s === null || f === null) {
303
+ return null;
304
+ }
305
+ const result = [];
306
+ let start = -1;
307
+ for (let i = 0; i < $.len(s);) {
308
+ let size = 1;
309
+ let r = s[i];
310
+ if (r >= utf8.RuneSelf) {
311
+ const [rune, runeSize] = utf8.DecodeRune($.goSlice(s, i, undefined));
312
+ r = rune;
313
+ size = runeSize;
314
+ }
315
+ if (f(r)) {
316
+ // Found separator
317
+ if (start >= 0) {
318
+ result.push($.goSlice(s, start, i));
319
+ start = -1;
320
+ }
321
+ }
322
+ else if (start < 0) {
323
+ // Start of new field
324
+ start = i;
325
+ }
326
+ i += size;
327
+ }
328
+ // Add final field if any
329
+ if (start >= 0) {
330
+ result.push($.goSlice(s, start, undefined));
331
+ }
332
+ return result.length === 0 ? null : $.arrayToSlice(result);
333
+ }
334
+ // Join concatenates the elements of s to create a new byte slice. The separator
335
+ // sep is placed between elements in the resulting slice.
336
+ export function Join(s, sep) {
337
+ if (s === null || $.len(s) === 0) {
338
+ return new Uint8Array(0);
339
+ }
340
+ // Just return a copy for single element
341
+ if ($.len(s) === 1) {
342
+ if (s[0] === null)
343
+ return new Uint8Array(0);
344
+ return new Uint8Array($.bytesToArray(s[0]));
345
+ }
346
+ // Calculate total length needed
347
+ let totalLen = 0;
348
+ const sepLen = sep === null ? 0 : $.len(sep);
349
+ for (let i = 0; i < $.len(s); i++) {
350
+ const elem = s[i];
351
+ if (elem !== null) {
352
+ totalLen += $.len(elem);
353
+ }
354
+ }
355
+ if (sepLen > 0) {
356
+ totalLen += sepLen * ($.len(s) - 1);
357
+ }
358
+ // Build result
359
+ const result = new Uint8Array(totalLen);
360
+ let pos = 0;
361
+ for (let i = 0; i < $.len(s); i++) {
362
+ if (i > 0 && sepLen > 0) {
363
+ const sepArr = $.bytesToArray(sep);
364
+ for (let j = 0; j < sepArr.length; j++) {
365
+ result[pos++] = sepArr[j];
366
+ }
367
+ }
368
+ const elem = s[i];
369
+ if (elem !== null) {
370
+ const elemArr = $.bytesToArray(elem);
371
+ for (let j = 0; j < elemArr.length; j++) {
372
+ result[pos++] = elemArr[j];
373
+ }
374
+ }
375
+ }
376
+ return result;
377
+ }
378
+ // HasPrefix reports whether the byte slice s begins with prefix.
379
+ export function HasPrefix(s, prefix) {
380
+ if (s === null)
381
+ s = new Uint8Array(0);
382
+ if (prefix === null)
383
+ prefix = new Uint8Array(0);
384
+ if ($.len(s) < $.len(prefix))
385
+ return false;
386
+ const sArr = $.bytesToArray(s);
387
+ const prefixArr = $.bytesToArray(prefix);
388
+ for (let i = 0; i < prefixArr.length; i++) {
389
+ if (sArr[i] !== prefixArr[i])
390
+ return false;
391
+ }
392
+ return true;
393
+ }
394
+ // HasSuffix reports whether the byte slice s ends with suffix.
395
+ export function HasSuffix(s, suffix) {
396
+ if (s === null)
397
+ s = new Uint8Array(0);
398
+ if (suffix === null)
399
+ suffix = new Uint8Array(0);
400
+ if ($.len(s) < $.len(suffix))
401
+ return false;
402
+ const sArr = $.bytesToArray(s);
403
+ const suffixArr = $.bytesToArray(suffix);
404
+ const offset = sArr.length - suffixArr.length;
405
+ for (let i = 0; i < suffixArr.length; i++) {
406
+ if (sArr[offset + i] !== suffixArr[i])
407
+ return false;
408
+ }
409
+ return true;
410
+ }
411
+ // Map returns a copy of the byte slice s with all its characters modified
412
+ // according to the mapping function. If mapping returns a negative value, the character is
413
+ // dropped from the byte slice with no replacement. The characters in s and the
414
+ // output are interpreted as UTF-8-encoded code points.
415
+ export function Map(mapping, s) {
416
+ if (s === null || $.len(s) === 0 || mapping === null) {
417
+ return s === null ? null : new Uint8Array(0);
418
+ }
419
+ const result = [];
420
+ for (let i = 0; i < $.len(s);) {
421
+ const [r, size] = utf8.DecodeRune($.goSlice(s, i, undefined));
422
+ if (size <= 0) {
423
+ // Invalid UTF-8, copy the byte as-is
424
+ result.push(s[i]);
425
+ i++;
426
+ }
427
+ else {
428
+ const mappedR = mapping(r);
429
+ if (mappedR >= 0) {
430
+ // Encode the mapped rune back to bytes
431
+ const runeBytes = new Uint8Array(utf8.UTFMax);
432
+ const n = utf8.EncodeRune(runeBytes, mappedR);
433
+ // Add the encoded bytes to result
434
+ for (let j = 0; j < n; j++) {
435
+ result.push(runeBytes[j]);
436
+ }
437
+ }
438
+ i += size;
439
+ }
440
+ }
441
+ return new Uint8Array(result);
442
+ }
443
+ // Repeat returns a new byte slice consisting of count copies of b.
444
+ //
445
+ // It panics if count is negative or if the result of (len(b) * count)
446
+ // overflows.
447
+ export function Repeat(b, count) {
448
+ if (count === 0) {
449
+ return new Uint8Array(0);
450
+ }
451
+ if (count < 0) {
452
+ $.panic("bytes: negative Repeat count");
453
+ }
454
+ if (b === null || $.len(b) === 0) {
455
+ return new Uint8Array(0);
456
+ }
457
+ const bArr = $.bytesToArray(b);
458
+ const totalLen = bArr.length * count;
459
+ // Check for overflow
460
+ if (totalLen / count !== bArr.length) {
461
+ $.panic("bytes: Repeat output length overflow");
462
+ }
463
+ const result = new Uint8Array(totalLen);
464
+ let pos = 0;
465
+ for (let i = 0; i < count; i++) {
466
+ for (let j = 0; j < bArr.length; j++) {
467
+ result[pos++] = bArr[j];
468
+ }
469
+ }
470
+ return result;
471
+ }
472
+ // ToUpper returns a copy of the byte slice s with all Unicode letters mapped to
473
+ // their upper case.
474
+ export function ToUpper(s) {
475
+ if (s === null || $.len(s) === 0) {
476
+ return new Uint8Array(0);
477
+ }
478
+ const result = [];
479
+ for (let i = 0; i < $.len(s);) {
480
+ const [r, size] = utf8.DecodeRune($.goSlice(s, i, undefined));
481
+ if (size <= 0) {
482
+ // Invalid UTF-8, copy the byte as-is
483
+ result.push(s[i]);
484
+ i++;
485
+ }
486
+ else {
487
+ // Convert rune to uppercase
488
+ const upperR = unicode.ToUpper(r);
489
+ // Encode the uppercase rune back to bytes
490
+ const runeBytes = new Uint8Array(utf8.UTFMax);
491
+ const n = utf8.EncodeRune(runeBytes, upperR);
492
+ // Add the encoded bytes to result
493
+ for (let j = 0; j < n; j++) {
494
+ result.push(runeBytes[j]);
495
+ }
496
+ i += size;
497
+ }
498
+ }
499
+ return new Uint8Array(result);
500
+ }
501
+ // ToLower returns a copy of the byte slice s with all Unicode letters mapped to
502
+ // their lower case.
503
+ export function ToLower(s) {
504
+ if (s === null || $.len(s) === 0) {
505
+ return new Uint8Array(0);
506
+ }
507
+ const result = [];
508
+ for (let i = 0; i < $.len(s);) {
509
+ const [r, size] = utf8.DecodeRune($.goSlice(s, i, undefined));
510
+ if (size <= 0) {
511
+ // Invalid UTF-8, copy the byte as-is
512
+ result.push(s[i]);
513
+ i++;
514
+ }
515
+ else {
516
+ // Convert rune to lowercase
517
+ const lowerR = unicode.ToLower(r);
518
+ // Encode the lowercase rune back to bytes
519
+ const runeBytes = new Uint8Array(utf8.UTFMax);
520
+ const n = utf8.EncodeRune(runeBytes, lowerR);
521
+ // Add the encoded bytes to result
522
+ for (let j = 0; j < n; j++) {
523
+ result.push(runeBytes[j]);
524
+ }
525
+ i += size;
526
+ }
527
+ }
528
+ return new Uint8Array(result);
529
+ }
530
+ // ToTitle treats s as UTF-8-encoded bytes and returns a copy with all the Unicode letters mapped to their title case.
531
+ export function ToTitle(s) {
532
+ return Map(unicode.ToTitle, s);
533
+ }
534
+ // ToUpperSpecial treats s as UTF-8-encoded bytes and returns a copy with all the Unicode letters mapped to their
535
+ // upper case, giving priority to the special casing rules.
536
+ export function ToUpperSpecial(c, s) {
537
+ // For now, ignore special case and fall back to regular ToUpper
538
+ return ToUpper(s);
539
+ }
540
+ // ToLowerSpecial treats s as UTF-8-encoded bytes and returns a copy with all the Unicode letters mapped to their
541
+ // lower case, giving priority to the special casing rules.
542
+ export function ToLowerSpecial(c, s) {
543
+ // For now, ignore special case and fall back to regular ToLower
544
+ return ToLower(s);
545
+ }
546
+ // ToTitleSpecial treats s as UTF-8-encoded bytes and returns a copy with all the Unicode letters mapped to their
547
+ // title case, giving priority to the special casing rules.
548
+ export function ToTitleSpecial(c, s) {
549
+ // For now, ignore special case and fall back to regular ToTitle
550
+ return ToTitle(s);
551
+ }
552
+ // ToValidUTF8 treats s as UTF-8-encoded bytes and returns a copy with each run of bytes
553
+ // representing invalid UTF-8 replaced with the bytes in replacement, which may be empty.
554
+ export function ToValidUTF8(s, replacement) {
555
+ if (s === null || $.len(s) === 0) {
556
+ return s === null ? null : new Uint8Array(0);
557
+ }
558
+ const result = [];
559
+ const replacementArr = replacement ? $.bytesToArray(replacement) : [];
560
+ for (let i = 0; i < $.len(s);) {
561
+ const [r, size] = utf8.DecodeRune($.goSlice(s, i, undefined));
562
+ if (size <= 0 || r === utf8.RuneError) {
563
+ // Invalid UTF-8, replace with replacement bytes
564
+ for (const b of replacementArr) {
565
+ result.push(b);
566
+ }
567
+ i++;
568
+ }
569
+ else {
570
+ for (let j = 0; j < size; j++) {
571
+ result.push(s[i + j]);
572
+ }
573
+ i += size;
574
+ }
575
+ }
576
+ return new Uint8Array(result);
577
+ }
578
+ // isSeparator reports whether the rune could mark a word boundary.
579
+ // TODO: update when package unicode captures more of the properties.
580
+ export function isSeparator(r) {
581
+ // ASCII alphanumerics and underscore are not separators
582
+ if (r <= 0x7F) {
583
+ if ((48 <= r && r <= 57) || // 0-9
584
+ (97 <= r && r <= 122) || // a-z
585
+ (65 <= r && r <= 90) || // A-Z
586
+ r === 95) { // _
587
+ return false;
588
+ }
589
+ return true;
590
+ }
591
+ // Letters and digits are not separators
592
+ if (unicode.IsLetter(r) || unicode.IsDigit(r)) {
593
+ return false;
594
+ }
595
+ // Otherwise, all we can do for now is treat spaces as separators.
596
+ return unicode.IsSpace(r);
597
+ }
598
+ // Title treats s as UTF-8-encoded bytes and returns a copy with all Unicode letters that begin
599
+ // words mapped to their title case.
600
+ //
601
+ // Deprecated: The rule Title uses for word boundaries does not handle Unicode
602
+ // punctuation properly. Use golang.org/x/text/cases instead.
603
+ export function Title(s) {
604
+ if (s === null || $.len(s) === 0) {
605
+ return s === null ? null : new Uint8Array(0);
606
+ }
607
+ const result = [];
608
+ let prevIsSep = true; // Start of string counts as separator
609
+ for (let i = 0; i < $.len(s);) {
610
+ const [r, size] = utf8.DecodeRune($.goSlice(s, i, undefined));
611
+ if (size <= 0) {
612
+ // Invalid UTF-8, copy the byte as-is
613
+ result.push(s[i]);
614
+ i++;
615
+ prevIsSep = true;
616
+ }
617
+ else {
618
+ let transformedR = r;
619
+ if (prevIsSep && unicode.IsLetter(r)) {
620
+ transformedR = unicode.ToTitle(r);
621
+ }
622
+ // Encode the (possibly transformed) rune back to bytes
623
+ const runeBytes = new Uint8Array(utf8.UTFMax);
624
+ const n = utf8.EncodeRune(runeBytes, transformedR);
625
+ // Add the encoded bytes to result
626
+ for (let j = 0; j < n; j++) {
627
+ result.push(runeBytes[j]);
628
+ }
629
+ prevIsSep = isSeparator(r);
630
+ i += size;
631
+ }
632
+ }
633
+ return new Uint8Array(result);
634
+ }
635
+ // TrimLeftFunc treats s as UTF-8-encoded bytes and returns a subslice of s by slicing off
636
+ // all leading UTF-8-encoded code points c that satisfy f(c).
637
+ export function TrimLeftFunc(s, f) {
638
+ const i = indexFunc(s, f, false);
639
+ if (i === -1) {
640
+ return null;
641
+ }
642
+ return $.goSlice(s, i, undefined);
643
+ }
644
+ // TrimRightFunc returns a subslice of s by slicing off all trailing
645
+ // UTF-8-encoded code points c that satisfy f(c).
646
+ export function TrimRightFunc(s, f) {
647
+ const i = lastIndexFunc(s, f, false);
648
+ if (i >= 0 && $.len(s) > i) {
649
+ const [, wid] = utf8.DecodeRune($.goSlice(s, i, undefined));
650
+ return $.goSlice(s, undefined, i + wid);
651
+ }
652
+ return null;
653
+ }
654
+ // TrimFunc returns a subslice of s by slicing off all leading and trailing
655
+ // UTF-8-encoded code points c that satisfy f(c).
656
+ export function TrimFunc(s, f) {
657
+ return TrimRightFunc(TrimLeftFunc(s, f), f);
658
+ }
659
+ // TrimPrefix returns s without the provided leading prefix string.
660
+ // If s doesn't start with prefix, s is returned unchanged.
661
+ export function TrimPrefix(s, prefix) {
662
+ if (HasPrefix(s, prefix)) {
663
+ return $.goSlice(s, $.len(prefix), undefined);
664
+ }
665
+ return s;
666
+ }
667
+ // TrimSuffix returns s without the provided trailing suffix string.
668
+ // If s doesn't end with suffix, s is returned unchanged.
669
+ export function TrimSuffix(s, suffix) {
670
+ if (HasSuffix(s, suffix)) {
671
+ return $.goSlice(s, undefined, $.len(s) - $.len(suffix));
672
+ }
673
+ return s;
674
+ }
675
+ // IndexFunc interprets s as a sequence of UTF-8-encoded code points.
676
+ // It returns the byte index in s of the first Unicode
677
+ // code point satisfying f(c), or -1 if none do.
678
+ export function IndexFunc(s, f) {
679
+ return indexFunc(s, f, true);
680
+ }
681
+ // LastIndexFunc interprets s as a sequence of UTF-8-encoded code points.
682
+ // It returns the byte index in s of the last Unicode
683
+ // code point satisfying f(c), or -1 if none do.
684
+ export function LastIndexFunc(s, f) {
685
+ return lastIndexFunc(s, f, true);
686
+ }
687
+ // indexFunc is the same as IndexFunc except that if
688
+ // truth==false, the sense of the predicate function is
689
+ // inverted.
690
+ export function indexFunc(s, f, truth) {
691
+ if (s === null || f === null) {
692
+ return -1;
693
+ }
694
+ for (let i = 0; i < $.len(s);) {
695
+ const [r, size] = utf8.DecodeRune($.goSlice(s, i, undefined));
696
+ if (size <= 0) {
697
+ // Invalid UTF-8
698
+ if (f(utf8.RuneError) === truth) {
699
+ return i;
700
+ }
701
+ i++;
702
+ }
703
+ else {
704
+ if (f(r) === truth) {
705
+ return i;
706
+ }
707
+ i += size;
708
+ }
709
+ }
710
+ return -1;
711
+ }
712
+ // lastIndexFunc is the same as LastIndexFunc except that if
713
+ // truth==false, the sense of the predicate function is
714
+ // inverted.
715
+ export function lastIndexFunc(s, f, truth) {
716
+ if (s === null || f === null) {
717
+ return -1;
718
+ }
719
+ let lastIndex = -1;
720
+ for (let i = 0; i < $.len(s);) {
721
+ const [r, size] = utf8.DecodeRune($.goSlice(s, i, undefined));
722
+ if (size <= 0) {
723
+ // Invalid UTF-8
724
+ if (f(utf8.RuneError) === truth) {
725
+ lastIndex = i;
726
+ }
727
+ i++;
728
+ }
729
+ else {
730
+ if (f(r) === truth) {
731
+ lastIndex = i;
732
+ }
733
+ i += size;
734
+ }
735
+ }
736
+ return lastIndex;
737
+ }
738
+ class asciiSet {
739
+ _value;
740
+ constructor(_value) {
741
+ this._value = _value;
742
+ }
743
+ valueOf() {
744
+ return this._value;
745
+ }
746
+ toString() {
747
+ return String(this._value);
748
+ }
749
+ static from(value) {
750
+ return new asciiSet(value);
751
+ }
752
+ // contains reports whether c is inside the set.
753
+ contains(c) {
754
+ const _as = this._value;
755
+ return ((_as[c >> 5] & (1 << (c & 31)))) !== 0;
756
+ }
757
+ }
758
+ // makeASCIISet creates a set of ASCII characters and reports whether all
759
+ // characters in chars are ASCII.
760
+ export function makeASCIISet(chars) {
761
+ const _as = new asciiSet([0, 0, 0, 0, 0, 0, 0, 0]);
762
+ for (let i = 0; i < chars.length; i++) {
763
+ const c = chars.charCodeAt(i);
764
+ if (c >= utf8.RuneSelf) {
765
+ return [_as, false];
766
+ }
767
+ _as._value[c >> 5] |= (1 << (c & 31));
768
+ }
769
+ return [_as, true];
770
+ }
771
+ // containsRune is a simplified version of strings.ContainsRune
772
+ // to avoid importing the strings package.
773
+ // We avoid bytes.ContainsRune to avoid allocating a temporary copy of s.
774
+ export function containsRune(s, r) {
775
+ for (let i = 0; i < s.length;) {
776
+ const c = s.codePointAt(i);
777
+ if (c === r)
778
+ return true;
779
+ i += c > 0xFFFF ? 2 : 1;
780
+ }
781
+ return false;
782
+ }
783
+ // Trim returns a subslice of s by slicing off all leading and
784
+ // trailing UTF-8-encoded code points contained in cutset.
785
+ export function Trim(s, cutset) {
786
+ if (s === null || cutset.length === 0) {
787
+ return s;
788
+ }
789
+ if (cutset.length === 1 && cutset.charCodeAt(0) < utf8.RuneSelf) {
790
+ // Single ASCII character optimization
791
+ return trimRightByte(trimLeftByte(s, cutset.charCodeAt(0)), cutset.charCodeAt(0));
792
+ }
793
+ const [as, allASCII] = makeASCIISet(cutset);
794
+ if (allASCII) {
795
+ return trimRightASCII(trimLeftASCII(s, as), as);
796
+ }
797
+ return trimRightUnicode(trimLeftUnicode(s, cutset), cutset);
798
+ }
799
+ // TrimLeft returns a subslice of s by slicing off all leading
800
+ // UTF-8-encoded code points contained in cutset.
801
+ export function TrimLeft(s, cutset) {
802
+ if (s === null || cutset.length === 0) {
803
+ return s;
804
+ }
805
+ if (cutset.length === 1 && cutset.charCodeAt(0) < utf8.RuneSelf) {
806
+ // Single ASCII character optimization
807
+ return trimLeftByte(s, cutset.charCodeAt(0));
808
+ }
809
+ const [as, allASCII] = makeASCIISet(cutset);
810
+ if (allASCII) {
811
+ return trimLeftASCII(s, as);
812
+ }
813
+ return trimLeftUnicode(s, cutset);
814
+ }
815
+ export function trimLeftByte(s, c) {
816
+ if (s === null)
817
+ return null;
818
+ for (let i = 0; i < $.len(s); i++) {
819
+ if (s[i] !== c) {
820
+ return $.goSlice(s, i, undefined);
821
+ }
822
+ }
823
+ return null;
824
+ }
825
+ export function trimLeftASCII(s, _as) {
826
+ if (s === null)
827
+ return null;
828
+ for (let i = 0; i < $.len(s); i++) {
829
+ const b = s[i];
830
+ if (b >= utf8.RuneSelf || !_as.contains(b)) {
831
+ return $.goSlice(s, i, undefined);
832
+ }
833
+ }
834
+ return null;
835
+ }
836
+ export function trimLeftUnicode(s, cutset) {
837
+ if (s === null)
838
+ return null;
839
+ for (let i = 0; i < $.len(s);) {
840
+ const [r, size] = utf8.DecodeRune($.goSlice(s, i, undefined));
841
+ if (size <= 0) {
842
+ // Invalid UTF-8, keep it
843
+ return $.goSlice(s, i, undefined);
844
+ }
845
+ if (!containsRune(cutset, r)) {
846
+ return $.goSlice(s, i, undefined);
847
+ }
848
+ i += size;
849
+ }
850
+ return null;
851
+ }
852
+ // TrimRight returns a subslice of s by slicing off all trailing
853
+ // UTF-8-encoded code points that are contained in cutset.
854
+ export function TrimRight(s, cutset) {
855
+ if (s === null || cutset.length === 0) {
856
+ return s;
857
+ }
858
+ if (cutset.length === 1 && cutset.charCodeAt(0) < utf8.RuneSelf) {
859
+ // Single ASCII character optimization
860
+ return trimRightByte(s, cutset.charCodeAt(0));
861
+ }
862
+ const [as, allASCII] = makeASCIISet(cutset);
863
+ if (allASCII) {
864
+ return trimRightASCII(s, as);
865
+ }
866
+ return trimRightUnicode(s, cutset);
867
+ }
868
+ export function trimRightByte(s, c) {
869
+ if (s === null)
870
+ return null;
871
+ for (let i = $.len(s) - 1; i >= 0; i--) {
872
+ if (s[i] !== c) {
873
+ return $.goSlice(s, undefined, i + 1);
874
+ }
875
+ }
876
+ return null;
877
+ }
878
+ export function trimRightASCII(s, _as) {
879
+ if (s === null)
880
+ return null;
881
+ for (let i = $.len(s) - 1; i >= 0; i--) {
882
+ const b = s[i];
883
+ if (b >= utf8.RuneSelf || !_as.contains(b)) {
884
+ return $.goSlice(s, undefined, i + 1);
885
+ }
886
+ }
887
+ return null;
888
+ }
889
+ export function trimRightUnicode(s, cutset) {
890
+ if (s === null)
891
+ return null;
892
+ // Need to scan from left to find rune boundaries, but track the last non-cutset position
893
+ let lastKeep = -1;
894
+ for (let i = 0; i < $.len(s);) {
895
+ const [r, size] = utf8.DecodeRune($.goSlice(s, i, undefined));
896
+ if (size <= 0) {
897
+ // Invalid UTF-8, keep everything up to here
898
+ return $.goSlice(s, undefined, i + 1);
899
+ }
900
+ if (!containsRune(cutset, r)) {
901
+ lastKeep = i + size;
902
+ }
903
+ i += size;
904
+ }
905
+ if (lastKeep < 0) {
906
+ return null;
907
+ }
908
+ return $.goSlice(s, undefined, lastKeep);
909
+ }
910
+ // TrimSpace returns a subslice of s by slicing off all leading and
911
+ // trailing white space, as defined by Unicode.
912
+ export function TrimSpace(s) {
913
+ return TrimFunc(s, unicode.IsSpace);
914
+ }
915
+ // Runes interprets s as a sequence of UTF-8-encoded code points.
916
+ // It returns a slice of runes (Unicode code points) equivalent to s.
917
+ export function Runes(s) {
918
+ if (s === null || $.len(s) === 0) {
919
+ return null;
920
+ }
921
+ const result = [];
922
+ for (let i = 0; i < $.len(s);) {
923
+ const [r, size] = utf8.DecodeRune($.goSlice(s, i, undefined));
924
+ if (size <= 0) {
925
+ // Invalid UTF-8, add replacement character
926
+ result.push(utf8.RuneError);
927
+ i++;
928
+ }
929
+ else {
930
+ result.push(r);
931
+ i += size;
932
+ }
933
+ }
934
+ return $.arrayToSlice(result);
935
+ }
936
+ // Replace returns a copy of the slice s with the first n
937
+ // non-overlapping instances of old replaced by new.
938
+ // If old is empty, it matches at the beginning of the slice
939
+ // and after each UTF-8 sequence, yielding up to k+1 replacements
940
+ // for a k-rune slice.
941
+ // If n < 0, there is no limit on the number of replacements.
942
+ export function Replace(s, old, _new, n) {
943
+ if (s === null) {
944
+ return new Uint8Array(0);
945
+ }
946
+ if (n === 0) {
947
+ // Make a copy without any replacements
948
+ return new Uint8Array($.bytesToArray(s));
949
+ }
950
+ // Handle empty old pattern - replace at beginning and after each UTF-8 sequence
951
+ if (old === null || $.len(old) === 0) {
952
+ const result = [];
953
+ const newBytes = _new === null ? [] : $.bytesToArray(_new);
954
+ // Add replacement at beginning
955
+ if (n !== 0) {
956
+ result.push(...newBytes);
957
+ if (n > 0)
958
+ n--;
959
+ }
960
+ // Add replacement after each UTF-8 sequence
961
+ for (let i = 0; i < $.len(s) && n !== 0;) {
962
+ const [, size] = utf8.DecodeRune($.goSlice(s, i, undefined));
963
+ if (size <= 0) {
964
+ result.push(s[i]);
965
+ i++;
966
+ }
967
+ else {
968
+ // Add the rune bytes
969
+ for (let j = 0; j < size; j++) {
970
+ result.push(s[i + j]);
971
+ }
972
+ i += size;
973
+ }
974
+ // Add replacement after this rune
975
+ if (n !== 0) {
976
+ result.push(...newBytes);
977
+ if (n > 0)
978
+ n--;
979
+ }
980
+ }
981
+ return new Uint8Array(result);
982
+ }
983
+ // Normal case - replace occurrences of old with new
984
+ const result = [];
985
+ const sBytes = $.bytesToArray(s);
986
+ const oldBytes = $.bytesToArray(old);
987
+ const newBytes = _new === null ? [] : $.bytesToArray(_new);
988
+ let i = 0;
989
+ let replacements = 0;
990
+ while (i <= sBytes.length - oldBytes.length && (n < 0 || replacements < n)) {
991
+ // Check if old pattern matches at current position
992
+ let matches = true;
993
+ for (let j = 0; j < oldBytes.length; j++) {
994
+ if (sBytes[i + j] !== oldBytes[j]) {
995
+ matches = false;
996
+ break;
997
+ }
998
+ }
999
+ if (matches) {
1000
+ // Replace with new bytes
1001
+ result.push(...newBytes);
1002
+ i += oldBytes.length;
1003
+ replacements++;
1004
+ }
1005
+ else {
1006
+ // Copy one byte and advance
1007
+ result.push(sBytes[i]);
1008
+ i++;
1009
+ }
1010
+ }
1011
+ // Copy remaining bytes
1012
+ while (i < sBytes.length) {
1013
+ result.push(sBytes[i]);
1014
+ i++;
1015
+ }
1016
+ return new Uint8Array(result);
1017
+ }
1018
+ // ReplaceAll returns a copy of the slice s with all
1019
+ // non-overlapping instances of old replaced by new.
1020
+ // If old is empty, it matches at the beginning of the slice
1021
+ // and after each UTF-8 sequence, yielding up to k+1 replacements
1022
+ // for a k-rune slice.
1023
+ export function ReplaceAll(s, old, _new) {
1024
+ return Replace(s, old, _new, -1);
1025
+ }
1026
+ // EqualFold reports whether s and t, interpreted as UTF-8 strings,
1027
+ // are equal under simple Unicode case-folding, which is a more general
1028
+ // form of case-insensitivity.
1029
+ export function EqualFold(s, t) {
1030
+ if (s === null && t === null)
1031
+ return true;
1032
+ if (s === null || t === null)
1033
+ return false;
1034
+ let si = 0, ti = 0;
1035
+ while (si < $.len(s) && ti < $.len(t)) {
1036
+ const [sr, ssize] = utf8.DecodeRune($.goSlice(s, si, undefined));
1037
+ const [tr, tsize] = utf8.DecodeRune($.goSlice(t, ti, undefined));
1038
+ if (ssize <= 0 || tsize <= 0) {
1039
+ // Invalid UTF-8, fall back to byte comparison
1040
+ if (s[si] !== t[ti])
1041
+ return false;
1042
+ si++;
1043
+ ti++;
1044
+ }
1045
+ else {
1046
+ // Convert both to lowercase for comparison
1047
+ const sLower = unicode.ToLower(sr);
1048
+ const tLower = unicode.ToLower(tr);
1049
+ if (sLower !== tLower)
1050
+ return false;
1051
+ si += ssize;
1052
+ ti += tsize;
1053
+ }
1054
+ }
1055
+ return si === $.len(s) && ti === $.len(t);
1056
+ }
1057
+ // Index returns the index of the first instance of sep in s, or -1 if sep is not present in s.
1058
+ export function Index(s, sep) {
1059
+ return $.bytesIndexOf(s, sep);
1060
+ }
1061
+ // Cut slices s around the first instance of sep,
1062
+ // returning the text before and after sep.
1063
+ // The found result reports whether sep appears in s.
1064
+ // If sep does not appear in s, cut returns s, nil, false.
1065
+ //
1066
+ // Cut returns slices of the original slice s, not copies.
1067
+ export function Cut(s, sep) {
1068
+ const i = Index(s, sep);
1069
+ if (i >= 0) {
1070
+ return [$.goSlice(s, undefined, i), $.goSlice(s, i + $.len(sep), undefined), true];
1071
+ }
1072
+ return [s, null, false];
1073
+ }
1074
+ // Clone returns a copy of b[:len(b)].
1075
+ // The result may have additional unused capacity.
1076
+ // Clone(nil) returns nil.
1077
+ export function Clone(b) {
1078
+ if (b === null) {
1079
+ return null;
1080
+ }
1081
+ return new Uint8Array($.bytesToArray(b));
1082
+ }
1083
+ // CutPrefix returns s without the provided leading prefix byte slice
1084
+ // and reports whether it found the prefix.
1085
+ // If s doesn't start with prefix, CutPrefix returns s, false.
1086
+ // If prefix is the empty byte slice, CutPrefix returns s, true.
1087
+ //
1088
+ // CutPrefix returns slices of the original slice s, not copies.
1089
+ export function CutPrefix(s, prefix) {
1090
+ if (!HasPrefix(s, prefix)) {
1091
+ return [s, false];
1092
+ }
1093
+ return [$.goSlice(s, $.len(prefix), undefined), true];
1094
+ }
1095
+ // CutSuffix returns s without the provided ending suffix byte slice
1096
+ // and reports whether it found the suffix.
1097
+ // If s doesn't end with suffix, CutSuffix returns s, false.
1098
+ // If suffix is the empty byte slice, CutSuffix returns s, true.
1099
+ //
1100
+ // CutSuffix returns slices of the original slice s, not copies.
1101
+ export function CutSuffix(s, suffix) {
1102
+ if (!HasSuffix(s, suffix)) {
1103
+ return [s, false];
1104
+ }
1105
+ return [$.goSlice(s, undefined, $.len(s) - $.len(suffix)), true];
1106
+ }
1107
+ //# sourceMappingURL=bytes.gs.js.map