goscript 0.0.38 → 0.0.39

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 (116) hide show
  1. package/compiler/expr-call.go +12 -0
  2. package/compiler/lit.go +17 -6
  3. package/dist/gs/builtin/builtin.d.ts +45 -0
  4. package/dist/gs/builtin/builtin.js +197 -0
  5. package/dist/gs/builtin/builtin.js.map +1 -1
  6. package/dist/gs/bytes/buffer.gs.d.ts +56 -0
  7. package/dist/gs/bytes/buffer.gs.js +611 -0
  8. package/dist/gs/bytes/buffer.gs.js.map +1 -0
  9. package/dist/gs/bytes/bytes.gs.d.ts +78 -0
  10. package/dist/gs/bytes/bytes.gs.js +1011 -0
  11. package/dist/gs/bytes/bytes.gs.js.map +1 -0
  12. package/dist/gs/bytes/index.d.ts +4 -0
  13. package/dist/gs/bytes/index.js +5 -0
  14. package/dist/gs/bytes/index.js.map +1 -0
  15. package/dist/gs/bytes/iter.gs.d.ts +9 -0
  16. package/dist/gs/bytes/iter.gs.js +143 -0
  17. package/dist/gs/bytes/iter.gs.js.map +1 -0
  18. package/dist/gs/bytes/reader.gs.d.ts +34 -0
  19. package/dist/gs/bytes/reader.gs.js +198 -0
  20. package/dist/gs/bytes/reader.gs.js.map +1 -0
  21. package/dist/gs/internal/bytealg/index.d.ts +14 -2
  22. package/dist/gs/internal/bytealg/index.js +114 -8
  23. package/dist/gs/internal/bytealg/index.js.map +1 -1
  24. package/dist/gs/reflect/deepequal.d.ts +2 -1
  25. package/dist/gs/reflect/deepequal.js +5 -53
  26. package/dist/gs/reflect/deepequal.js.map +1 -1
  27. package/dist/gs/reflect/map.d.ts +14 -8
  28. package/dist/gs/reflect/map.js +15 -11
  29. package/dist/gs/reflect/map.js.map +1 -1
  30. package/dist/gs/reflect/type.d.ts +17 -9
  31. package/dist/gs/reflect/type.js +1 -1
  32. package/dist/gs/reflect/type.js.map +1 -1
  33. package/dist/gs/reflect/value.js +2 -1
  34. package/dist/gs/reflect/value.js.map +1 -1
  35. package/dist/gs/reflect/visiblefields.js +18 -12
  36. package/dist/gs/reflect/visiblefields.js.map +1 -1
  37. package/dist/gs/unicode/utf8/utf8.d.ts +1 -1
  38. package/dist/gs/unicode/utf8/utf8.js +4 -2
  39. package/dist/gs/unicode/utf8/utf8.js.map +1 -1
  40. package/gs/builtin/builtin.ts +219 -0
  41. package/gs/bytes/buffer.gs.ts +614 -0
  42. package/gs/bytes/bytes.gs.ts +1180 -0
  43. package/gs/bytes/godoc.txt +69 -0
  44. package/gs/bytes/index.ts +69 -0
  45. package/gs/bytes/iter.gs.ts +149 -0
  46. package/gs/bytes/reader.gs.ts +230 -0
  47. package/gs/internal/bytealg/index.ts +125 -10
  48. package/gs/reflect/deepequal.test.ts +41 -0
  49. package/gs/reflect/deepequal.ts +19 -4
  50. package/gs/reflect/map.test.ts +30 -0
  51. package/gs/reflect/map.ts +22 -18
  52. package/gs/reflect/type.ts +19 -15
  53. package/gs/reflect/value.ts +8 -2
  54. package/gs/reflect/visiblefields.ts +17 -13
  55. package/gs/unicode/utf8/utf8.ts +8 -5
  56. package/package.json +1 -1
  57. package/dist/gs/internal/testlog/index.d.ts +0 -1
  58. package/dist/gs/internal/testlog/index.js +0 -5
  59. package/dist/gs/internal/testlog/index.js.map +0 -1
  60. package/dist/gs/maps/iter.gs.d.ts +0 -7
  61. package/dist/gs/maps/iter.gs.js +0 -65
  62. package/dist/gs/maps/iter.gs.js.map +0 -1
  63. package/dist/gs/maps/maps.gs.d.ts +0 -7
  64. package/dist/gs/maps/maps.gs.js +0 -79
  65. package/dist/gs/maps/maps.gs.js.map +0 -1
  66. package/dist/gs/reflect/abi.d.ts +0 -59
  67. package/dist/gs/reflect/abi.gs.d.ts +0 -59
  68. package/dist/gs/reflect/abi.gs.js +0 -79
  69. package/dist/gs/reflect/abi.gs.js.map +0 -1
  70. package/dist/gs/reflect/abi.js +0 -79
  71. package/dist/gs/reflect/abi.js.map +0 -1
  72. package/dist/gs/reflect/badlinkname.d.ts +0 -52
  73. package/dist/gs/reflect/badlinkname.gs.d.ts +0 -52
  74. package/dist/gs/reflect/badlinkname.gs.js +0 -72
  75. package/dist/gs/reflect/badlinkname.gs.js.map +0 -1
  76. package/dist/gs/reflect/badlinkname.js +0 -72
  77. package/dist/gs/reflect/badlinkname.js.map +0 -1
  78. package/dist/gs/reflect/deepequal.gs.d.ts +0 -25
  79. package/dist/gs/reflect/deepequal.gs.js +0 -308
  80. package/dist/gs/reflect/deepequal.gs.js.map +0 -1
  81. package/dist/gs/reflect/float32reg_generic.gs.d.ts +0 -2
  82. package/dist/gs/reflect/float32reg_generic.gs.js +0 -10
  83. package/dist/gs/reflect/float32reg_generic.gs.js.map +0 -1
  84. package/dist/gs/reflect/index.gs.d.ts +0 -1
  85. package/dist/gs/reflect/index.gs.js +0 -3
  86. package/dist/gs/reflect/index.gs.js.map +0 -1
  87. package/dist/gs/reflect/iter.gs.d.ts +0 -3
  88. package/dist/gs/reflect/iter.gs.js +0 -24
  89. package/dist/gs/reflect/iter.gs.js.map +0 -1
  90. package/dist/gs/reflect/makefunc.gs.d.ts +0 -34
  91. package/dist/gs/reflect/makefunc.gs.js +0 -288
  92. package/dist/gs/reflect/makefunc.gs.js.map +0 -1
  93. package/dist/gs/reflect/map_swiss.gs.d.ts +0 -14
  94. package/dist/gs/reflect/map_swiss.gs.js +0 -70
  95. package/dist/gs/reflect/map_swiss.gs.js.map +0 -1
  96. package/dist/gs/reflect/reflect.gs.d.ts +0 -132
  97. package/dist/gs/reflect/reflect.gs.js +0 -437
  98. package/dist/gs/reflect/reflect.gs.js.map +0 -1
  99. package/dist/gs/reflect/swapper.gs.d.ts +0 -1
  100. package/dist/gs/reflect/swapper.gs.js +0 -32
  101. package/dist/gs/reflect/swapper.gs.js.map +0 -1
  102. package/dist/gs/reflect/type.gs.d.ts +0 -4
  103. package/dist/gs/reflect/type.gs.js +0 -21
  104. package/dist/gs/reflect/type.gs.js.map +0 -1
  105. package/dist/gs/reflect/value.gs.d.ts +0 -4
  106. package/dist/gs/reflect/value.gs.js +0 -12
  107. package/dist/gs/reflect/value.gs.js.map +0 -1
  108. package/dist/gs/reflect/visiblefields.gs.d.ts +0 -3
  109. package/dist/gs/reflect/visiblefields.gs.js +0 -123
  110. package/dist/gs/reflect/visiblefields.gs.js.map +0 -1
  111. package/dist/gs/stringslite/index.d.ts +0 -1
  112. package/dist/gs/stringslite/index.js +0 -2
  113. package/dist/gs/stringslite/index.js.map +0 -1
  114. package/dist/gs/stringslite/strings.d.ts +0 -11
  115. package/dist/gs/stringslite/strings.js +0 -67
  116. package/dist/gs/stringslite/strings.js.map +0 -1
@@ -0,0 +1,614 @@
1
+ import * as $ from "@goscript/builtin/builtin.js";
2
+ const { copy, recover } = $;
3
+
4
+ import { IndexByte } from "./bytes.gs.js";
5
+
6
+ import * as errors from "@goscript/errors/index.js"
7
+
8
+ import * as io from "@goscript/io/index.js"
9
+
10
+ import * as utf8 from "@goscript/unicode/utf8/index.js"
11
+
12
+ let smallBufferSize: number = 64
13
+
14
+ export class Buffer {
15
+ // contents are the bytes buf[off : len(buf)]
16
+ public get buf(): $.Bytes {
17
+ return this._fields.buf.value
18
+ }
19
+ public set buf(value: $.Bytes) {
20
+ this._fields.buf.value = value
21
+ }
22
+
23
+ // read at &buf[off], write at &buf[len(buf)]
24
+ public get off(): number {
25
+ return this._fields.off.value
26
+ }
27
+ public set off(value: number) {
28
+ this._fields.off.value = value
29
+ }
30
+
31
+ // last read operation, so that Unread* can work correctly.
32
+ public get lastRead(): readOp {
33
+ return this._fields.lastRead.value
34
+ }
35
+ public set lastRead(value: readOp) {
36
+ this._fields.lastRead.value = value
37
+ }
38
+
39
+ public _fields: {
40
+ buf: $.VarRef<$.Bytes>;
41
+ off: $.VarRef<number>;
42
+ lastRead: $.VarRef<readOp>;
43
+ }
44
+
45
+ constructor(init?: Partial<{buf?: $.Bytes, lastRead?: readOp, off?: number}>) {
46
+ this._fields = {
47
+ buf: $.varRef(init?.buf ?? null),
48
+ off: $.varRef(init?.off ?? 0),
49
+ lastRead: $.varRef(init?.lastRead ?? 0)
50
+ }
51
+ }
52
+
53
+ public clone(): Buffer {
54
+ const cloned = new Buffer()
55
+ cloned._fields = {
56
+ buf: $.varRef(this._fields.buf.value),
57
+ off: $.varRef(this._fields.off.value),
58
+ lastRead: $.varRef(this._fields.lastRead.value)
59
+ }
60
+ return cloned
61
+ }
62
+
63
+ // Bytes returns a slice of length b.Len() holding the unread portion of the buffer.
64
+ // The slice is valid for use only until the next buffer modification (that is,
65
+ // only until the next call to a method like [Buffer.Read], [Buffer.Write], [Buffer.Reset], or [Buffer.Truncate]).
66
+ // The slice aliases the buffer content at least until the next buffer modification,
67
+ // so immediate changes to the slice will affect the result of future reads.
68
+ public Bytes(): $.Bytes {
69
+ const b = this
70
+ return $.goSlice(b.buf, b.off, undefined)
71
+ }
72
+
73
+ // AvailableBuffer returns an empty buffer with b.Available() capacity.
74
+ // This buffer is intended to be appended to and
75
+ // passed to an immediately succeeding [Buffer.Write] call.
76
+ // The buffer is only valid until the next write operation on b.
77
+ public AvailableBuffer(): $.Bytes {
78
+ const b = this
79
+ return $.goSlice(b.buf, $.len(b.buf), undefined)
80
+ }
81
+
82
+ // String returns the contents of the unread portion of the buffer
83
+ // as a string. If the [Buffer] is a nil pointer, it returns "<nil>".
84
+ //
85
+ // To build strings more efficiently, see the [strings.Builder] type.
86
+ public String(): string {
87
+ const b = this
88
+ if (b == null) {
89
+ // Special case, useful in debugging.
90
+ return "<nil>"
91
+ }
92
+ if (b.buf === null) {
93
+ return ""
94
+ }
95
+ return $.bytesToString($.goSlice(b.buf, b.off, undefined))
96
+ }
97
+
98
+ // empty reports whether the unread portion of the buffer is empty.
99
+ public empty(): boolean {
100
+ const b = this
101
+ return $.len(b.buf) <= b.off
102
+ }
103
+
104
+ // Len returns the number of bytes of the unread portion of the buffer;
105
+ // b.Len() == len(b.Bytes()).
106
+ public Len(): number {
107
+ const b = this
108
+ return $.len(b.buf) - b.off
109
+ }
110
+
111
+ // Cap returns the capacity of the buffer's underlying byte slice, that is, the
112
+ // total space allocated for the buffer's data.
113
+ public Cap(): number {
114
+ const b = this
115
+ return $.cap(b.buf)
116
+ }
117
+
118
+ // Available returns how many bytes are unused in the buffer.
119
+ public Available(): number {
120
+ const b = this
121
+ return $.cap(b.buf) - $.len(b.buf)
122
+ }
123
+
124
+ // Truncate discards all but the first n unread bytes from the buffer
125
+ // but continues to use the same allocated storage.
126
+ // It panics if n is negative or greater than the length of the buffer.
127
+ public Truncate(n: number): void {
128
+ const b = this
129
+ if (n == 0) {
130
+ b.Reset()
131
+ return
132
+ }
133
+ b.lastRead = 0
134
+ if (n < 0 || n > b.Len()) {
135
+ $.panic("bytes.Buffer: truncation out of range")
136
+ }
137
+ b.buf = $.goSlice(b.buf, undefined, b.off + n)
138
+ }
139
+
140
+ // Reset resets the buffer to be empty,
141
+ // but it retains the underlying storage for use by future writes.
142
+ // Reset is the same as [Buffer.Truncate](0).
143
+ public Reset(): void {
144
+ const b = this
145
+ b.buf = $.goSlice(b.buf, undefined, 0)
146
+ b.off = 0
147
+ b.lastRead = 0
148
+ }
149
+
150
+ // tryGrowByReslice is an inlineable version of grow for the fast-case where the
151
+ // internal buffer only needs to be resliced.
152
+ // It returns the index where bytes should be written and whether it succeeded.
153
+ public tryGrowByReslice(n: number): [number, boolean] {
154
+ const b = this
155
+ if (b.buf === null) {
156
+ return [0, false]
157
+ }
158
+ {
159
+ let l = $.len(b.buf)
160
+ if (n <= $.cap(b.buf) - l) {
161
+ b.buf = $.goSlice(b.buf, undefined, l + n)
162
+ return [l, true]
163
+ }
164
+ }
165
+ return [0, false]
166
+ }
167
+
168
+ // grow grows the buffer to guarantee space for n more bytes.
169
+ // It returns the index where bytes should be written.
170
+ // If the buffer can't grow it will panic with ErrTooLarge.
171
+ public grow(n: number): number {
172
+ const b = this
173
+ let m = b.Len()
174
+ if (m == 0 && b.off != 0) {
175
+ b.Reset()
176
+ }
177
+ {
178
+ let [i, ok] = b.tryGrowByReslice(n)
179
+ if (ok) {
180
+ return i
181
+ }
182
+ }
183
+ if (b.buf == null) {
184
+ // Handle null buffer case - create new buffer
185
+ const capacity = n <= 64 ? 64 : n
186
+ b.buf = $.makeSlice<number>(n, capacity, 'byte')
187
+ return 0
188
+ }
189
+ let c = $.cap(b.buf)
190
+ if (n <= c / 2 - m) {
191
+ // We can slide things down instead of allocating a new
192
+ // slice. We only need m+n <= c to slide, but
193
+ // we instead let capacity get twice as large so we
194
+ // don't spend all our time copying.
195
+ $.copy($.bytesToUint8Array(b.buf), $.bytesToUint8Array($.goSlice(b.buf, b.off, undefined)))
196
+ } else if (c > 9223372036854775807 - c - n) {
197
+ $.panic(ErrTooLarge)
198
+ } else {
199
+ // Add b.off to account for b.buf[:b.off] being sliced off the front.
200
+ b.buf = growSlice($.goSlice(b.buf, b.off, undefined), b.off + n)
201
+ }
202
+ b.off = 0
203
+ b.buf = $.goSlice(b.buf, undefined, m + n)
204
+ return m
205
+ }
206
+
207
+ // Grow grows the buffer's capacity, if necessary, to guarantee space for
208
+ // another n bytes. After Grow(n), at least n bytes can be written to the
209
+ // buffer without another allocation.
210
+ // If n is negative, Grow will panic.
211
+ // If the buffer can't grow it will panic with [ErrTooLarge].
212
+ public Grow(n: number): void {
213
+ const b = this
214
+ if (n < 0) {
215
+ $.panic("bytes.Buffer.Grow: negative count")
216
+ }
217
+ let m = b.grow(n)
218
+ b.buf = $.goSlice(b.buf, undefined, m)
219
+ }
220
+
221
+ // Write appends the contents of p to the buffer, growing the buffer as
222
+ // needed. The return value n is the length of p; err is always nil. If the
223
+ // buffer becomes too large, Write will panic with [ErrTooLarge].
224
+ public Write(p: $.Bytes): [number, $.GoError] {
225
+ const b = this
226
+ b.lastRead = 0
227
+ let [m, ok] = b.tryGrowByReslice($.len(p))
228
+ if (!ok) {
229
+ m = b.grow($.len(p))
230
+ }
231
+ // Copy directly to the buffer at position m
232
+ const targetSlice = $.goSlice(b.buf, m, m + $.len(p))
233
+ if (p instanceof Uint8Array) {
234
+ // Direct Uint8Array copy
235
+ for (let i = 0; i < $.len(p); i++) {
236
+ targetSlice![i] = p[i]
237
+ }
238
+ } else {
239
+ // Copy from Slice<number>
240
+ for (let i = 0; i < $.len(p); i++) {
241
+ targetSlice![i] = p![i]
242
+ }
243
+ }
244
+ return [$.len(p), null]
245
+ }
246
+
247
+ // WriteString appends the contents of s to the buffer, growing the buffer as
248
+ // needed. The return value n is the length of s; err is always nil. If the
249
+ // buffer becomes too large, WriteString will panic with [ErrTooLarge].
250
+ public WriteString(s: string): [number, $.GoError] {
251
+ const b = this
252
+ b.lastRead = 0
253
+ let [m, ok] = b.tryGrowByReslice($.len(s))
254
+ if (!ok) {
255
+ m = b.grow($.len(s))
256
+ }
257
+ // Copy string directly to the buffer at position m
258
+ const targetSlice = $.goSlice(b.buf, m, m + $.len(s))
259
+ const encoder = new TextEncoder()
260
+ const encoded = encoder.encode(s)
261
+ for (let i = 0; i < encoded.length; i++) {
262
+ targetSlice![i] = encoded[i]
263
+ }
264
+ return [encoded.length, null]
265
+ }
266
+
267
+ // ReadFrom reads data from r until EOF and appends it to the buffer, growing
268
+ // the buffer as needed. The return value n is the number of bytes read. Any
269
+ // error except io.EOF encountered during the read is also returned. If the
270
+ // buffer becomes too large, ReadFrom will panic with [ErrTooLarge].
271
+ public ReadFrom(r: io.Reader): [number, $.GoError] {
272
+ const b = this
273
+ b.lastRead = 0
274
+ let n = 0
275
+ for (; ; ) {
276
+ let i = b.grow(512)
277
+ b.buf = $.goSlice(b.buf, undefined, i)
278
+ let [m, e] = r!.Read($.bytesToUint8Array($.goSlice(b.buf, i, $.cap(b.buf))))
279
+ if (m < 0) {
280
+ $.panic(errNegativeRead)
281
+ }
282
+
283
+ b.buf = $.goSlice(b.buf, undefined, i + m)
284
+ n += (m as number)
285
+
286
+ // e is EOF, so return nil explicitly
287
+ if (e == io.EOF) {
288
+ return [n, null]
289
+ }
290
+ if (e != null) {
291
+ return [n, e]
292
+ }
293
+ }
294
+ }
295
+
296
+ // WriteTo writes data to w until the buffer is drained or an error occurs.
297
+ // The return value n is the number of bytes written; it always fits into an
298
+ // int, but it is int64 to match the [io.WriterTo] interface. Any error
299
+ // encountered during the write is also returned.
300
+ public WriteTo(w: io.Writer): [number, $.GoError] {
301
+ const b = this
302
+ b.lastRead = 0
303
+ let n = 0
304
+ {
305
+ let nBytes = b.Len()
306
+ if (nBytes > 0) {
307
+ let [m, e] = w!.Write($.bytesToUint8Array($.goSlice(b.buf, b.off, undefined)))
308
+ if (m > nBytes) {
309
+ $.panic("bytes.Buffer.WriteTo: invalid Write count")
310
+ }
311
+ b.off += m
312
+ n = (m as number)
313
+ if (e != null) {
314
+ return [n, e]
315
+ }
316
+ // all bytes should have been written, by definition of
317
+ // Write method in io.Writer
318
+ if (m != nBytes) {
319
+ return [n, io.ErrShortWrite]
320
+ }
321
+ }
322
+ }
323
+ b.Reset()
324
+ return [n, null]
325
+ }
326
+
327
+ // WriteByte appends the byte c to the buffer, growing the buffer as needed.
328
+ // The returned error is always nil, but is included to match [bufio.Writer]'s
329
+ // WriteByte. If the buffer becomes too large, WriteByte will panic with
330
+ // [ErrTooLarge].
331
+ public WriteByte(c: number): $.GoError {
332
+ const b = this
333
+ b.lastRead = 0
334
+ let [m, ok] = b.tryGrowByReslice(1)
335
+ if (!ok) {
336
+ m = b.grow(1)
337
+ }
338
+ b.buf![m] = c
339
+ return null
340
+ }
341
+
342
+ // WriteRune appends the UTF-8 encoding of Unicode code point r to the
343
+ // buffer, returning its length and an error, which is always nil but is
344
+ // included to match [bufio.Writer]'s WriteRune. The buffer is grown as needed;
345
+ // if it becomes too large, WriteRune will panic with [ErrTooLarge].
346
+ public WriteRune(r: number): [number, $.GoError] {
347
+ const b = this
348
+ if ((r as number) < utf8.RuneSelf) {
349
+ b.WriteByte($.byte(r))
350
+ return [1, null]
351
+ }
352
+ b.lastRead = 0
353
+ let [m, ok] = b.tryGrowByReslice(utf8.UTFMax)
354
+ if (!ok) {
355
+ m = b.grow(utf8.UTFMax)
356
+ }
357
+ b.buf = utf8.AppendRune($.bytesToUint8Array($.goSlice(b.buf, undefined, m)), r)
358
+ return [$.len(b.buf) - m, null]
359
+ }
360
+
361
+ // Read reads the next len(p) bytes from the buffer or until the buffer
362
+ // is drained. The return value n is the number of bytes read. If the
363
+ // buffer has no data to return, err is [io.EOF] (unless len(p) is zero);
364
+ // otherwise it is nil.
365
+ public Read(p: $.Bytes): [number, $.GoError] {
366
+ const b = this
367
+ b.lastRead = 0
368
+ if (b.empty()) {
369
+ // Buffer is empty, reset to recover space.
370
+ b.Reset()
371
+ if ($.len(p) == 0) {
372
+ return [0, null]
373
+ }
374
+ return [0, io.EOF]
375
+ }
376
+ const n = $.copy($.bytesToUint8Array(p), $.bytesToUint8Array($.goSlice(b.buf, b.off, undefined)))
377
+ b.off += n
378
+ if (n > 0) {
379
+ b.lastRead = -1
380
+ }
381
+ return [n, null]
382
+ }
383
+
384
+ // Next returns a slice containing the next n bytes from the buffer,
385
+ // advancing the buffer as if the bytes had been returned by [Buffer.Read].
386
+ // If there are fewer than n bytes in the buffer, Next returns the entire buffer.
387
+ // The slice is only valid until the next call to a read or write method.
388
+ public Next(n: number): $.Bytes {
389
+ const b = this
390
+ b.lastRead = 0
391
+ let m = b.Len()
392
+ if (n > m) {
393
+ n = m
394
+ }
395
+ let data = $.goSlice(b.buf, b.off, b.off + n)
396
+ b.off += n
397
+ if (n > 0) {
398
+ b.lastRead = -1
399
+ }
400
+ return data
401
+ }
402
+
403
+ // ReadByte reads and returns the next byte from the buffer.
404
+ // If no byte is available, it returns error [io.EOF].
405
+ public ReadByte(): [number, $.GoError] {
406
+ const b = this
407
+ if (b.empty()) {
408
+ // Buffer is empty, reset to recover space.
409
+ b.Reset()
410
+ return [0, io.EOF]
411
+ }
412
+ let c = b.buf![b.off]
413
+ b.off++
414
+ b.lastRead = -1
415
+ return [c, null]
416
+ }
417
+
418
+ // ReadRune reads and returns the next UTF-8-encoded
419
+ // Unicode code point from the buffer.
420
+ // If no bytes are available, the error returned is io.EOF.
421
+ // If the bytes are an erroneous UTF-8 encoding, it
422
+ // consumes one byte and returns U+FFFD, 1.
423
+ public ReadRune(): [number, number, $.GoError] {
424
+ const b = this
425
+ if (b.empty()) {
426
+ // Buffer is empty, reset to recover space.
427
+ b.Reset()
428
+ return [0, 0, io.EOF]
429
+ }
430
+ let c = b.buf![b.off]
431
+ if (c < utf8.RuneSelf) {
432
+ b.off++
433
+ b.lastRead = 1
434
+ return [(c as number), 1, null]
435
+ }
436
+ let r: number
437
+ let n: number
438
+ [r, n] = utf8.DecodeRune($.goSlice(b.buf, b.off, undefined))
439
+ b.off += n
440
+ b.lastRead = (n as readOp)
441
+ return [r, n, null]
442
+ }
443
+
444
+ // UnreadRune unreads the last rune returned by [Buffer.ReadRune].
445
+ // If the most recent read or write operation on the buffer was
446
+ // not a successful [Buffer.ReadRune], UnreadRune returns an error. (In this regard
447
+ // it is stricter than [Buffer.UnreadByte], which will unread the last byte
448
+ // from any read operation.)
449
+ public UnreadRune(): $.GoError {
450
+ const b = this
451
+ if (b.lastRead <= 0) {
452
+ return errors.New("bytes.Buffer: UnreadRune: previous operation was not a successful ReadRune")
453
+ }
454
+ if (b.off >= $.int(b.lastRead)) {
455
+ b.off -= $.int(b.lastRead)
456
+ }
457
+ b.lastRead = 0
458
+ return null
459
+ }
460
+
461
+ // UnreadByte unreads the last byte returned by the most recent successful
462
+ // read operation that read at least one byte. If a write has happened since
463
+ // the last read, if the last read returned an error, or if the read read zero
464
+ // bytes, UnreadByte returns an error.
465
+ public UnreadByte(): $.GoError {
466
+ const b = this
467
+ if (b.lastRead == 0) {
468
+ return errUnreadByte
469
+ }
470
+ b.lastRead = 0
471
+ if (b.off > 0) {
472
+ b.off--
473
+ }
474
+ return null
475
+ }
476
+
477
+ // ReadBytes reads until the first occurrence of delim in the input,
478
+ // returning a slice containing the data up to and including the delimiter.
479
+ // If ReadBytes encounters an error before finding a delimiter,
480
+ // it returns the data read before the error and the error itself (often [io.EOF]).
481
+ // ReadBytes returns err != nil if and only if the returned data does not end in
482
+ // delim.
483
+ public ReadBytes(delim: number): [$.Bytes, $.GoError] {
484
+ const b = this
485
+ let [slice, err] = b.readSlice(delim)
486
+ let line = $.append<number>(null, slice)
487
+ return [line, err]
488
+ }
489
+
490
+ // readSlice is like ReadBytes but returns a reference to internal buffer data.
491
+ public readSlice(delim: number): [$.Bytes, $.GoError] {
492
+ const b = this
493
+ let i = IndexByte($.goSlice(b.buf, b.off, undefined), delim)
494
+ let end = b.off + i + 1
495
+ let err: $.GoError = null
496
+ let line: $.Bytes
497
+ if (i < 0) {
498
+ end = $.len(b.buf)
499
+ err = io.EOF
500
+ }
501
+ line = $.goSlice(b.buf, b.off, end)
502
+ b.off = end
503
+ b.lastRead = -1
504
+ return [line, err]
505
+ }
506
+
507
+ // ReadString reads until the first occurrence of delim in the input,
508
+ // returning a string containing the data up to and including the delimiter.
509
+ // If ReadString encounters an error before finding a delimiter,
510
+ // it returns the data read before the error and the error itself (often [io.EOF]).
511
+ // ReadString returns err != nil if and only if the returned data does not end
512
+ // in delim.
513
+ public ReadString(delim: number): [string, $.GoError] {
514
+ const b = this
515
+ let slice: $.Bytes
516
+ let err: $.GoError
517
+ [slice, err] = b.readSlice(delim)
518
+ return [$.bytesToString(slice), err]
519
+ }
520
+
521
+ // Register this type with the runtime type system
522
+ static __typeInfo = $.registerStructType(
523
+ 'Buffer',
524
+ new Buffer(),
525
+ [{ name: "Bytes", args: [], returns: [{ type: { kind: $.TypeKind.Slice, elemType: { kind: $.TypeKind.Basic, name: "number" } } }] }, { name: "AvailableBuffer", args: [], returns: [{ type: { kind: $.TypeKind.Slice, elemType: { kind: $.TypeKind.Basic, name: "number" } } }] }, { name: "String", args: [], returns: [{ type: { kind: $.TypeKind.Basic, name: "string" } }] }, { name: "empty", args: [], returns: [{ type: { kind: $.TypeKind.Basic, name: "boolean" } }] }, { name: "Len", args: [], returns: [{ type: { kind: $.TypeKind.Basic, name: "number" } }] }, { name: "Cap", args: [], returns: [{ type: { kind: $.TypeKind.Basic, name: "number" } }] }, { name: "Available", args: [], returns: [{ type: { kind: $.TypeKind.Basic, name: "number" } }] }, { name: "Truncate", args: [{ name: "n", type: { kind: $.TypeKind.Basic, name: "number" } }], returns: [] }, { name: "Reset", args: [], returns: [] }, { name: "tryGrowByReslice", args: [{ name: "n", type: { kind: $.TypeKind.Basic, name: "number" } }], returns: [{ type: { kind: $.TypeKind.Basic, name: "number" } }, { type: { kind: $.TypeKind.Basic, name: "boolean" } }] }, { name: "grow", args: [{ name: "n", type: { kind: $.TypeKind.Basic, name: "number" } }], returns: [{ type: { kind: $.TypeKind.Basic, name: "number" } }] }, { name: "Grow", args: [{ name: "n", type: { kind: $.TypeKind.Basic, name: "number" } }], returns: [] }, { name: "Write", args: [{ name: "p", type: { kind: $.TypeKind.Slice, elemType: { kind: $.TypeKind.Basic, name: "number" } } }], returns: [{ type: { kind: $.TypeKind.Basic, name: "number" } }, { type: { kind: $.TypeKind.Interface, name: 'GoError', methods: [{ name: 'Error', args: [], returns: [{ type: { kind: $.TypeKind.Basic, name: 'string' } }] }] } }] }, { name: "WriteString", args: [{ name: "s", type: { kind: $.TypeKind.Basic, name: "string" } }], returns: [{ type: { kind: $.TypeKind.Basic, name: "number" } }, { type: { kind: $.TypeKind.Interface, name: 'GoError', methods: [{ name: 'Error', args: [], returns: [{ type: { kind: $.TypeKind.Basic, name: 'string' } }] }] } }] }, { name: "ReadFrom", args: [{ name: "r", type: "Reader" }], returns: [{ type: { kind: $.TypeKind.Basic, name: "number" } }, { type: { kind: $.TypeKind.Interface, name: 'GoError', methods: [{ name: 'Error', args: [], returns: [{ type: { kind: $.TypeKind.Basic, name: 'string' } }] }] } }] }, { name: "WriteTo", args: [{ name: "w", type: "Writer" }], returns: [{ type: { kind: $.TypeKind.Basic, name: "number" } }, { type: { kind: $.TypeKind.Interface, name: 'GoError', methods: [{ name: 'Error', args: [], returns: [{ type: { kind: $.TypeKind.Basic, name: 'string' } }] }] } }] }, { name: "WriteByte", args: [{ name: "c", type: { kind: $.TypeKind.Basic, name: "number" } }], returns: [{ type: { kind: $.TypeKind.Interface, name: 'GoError', methods: [{ name: 'Error', args: [], returns: [{ type: { kind: $.TypeKind.Basic, name: 'string' } }] }] } }] }, { name: "WriteRune", args: [{ name: "r", type: { kind: $.TypeKind.Basic, name: "number" } }], returns: [{ type: { kind: $.TypeKind.Basic, name: "number" } }, { type: { kind: $.TypeKind.Interface, name: 'GoError', methods: [{ name: 'Error', args: [], returns: [{ type: { kind: $.TypeKind.Basic, name: 'string' } }] }] } }] }, { name: "Read", args: [{ name: "p", type: { kind: $.TypeKind.Slice, elemType: { kind: $.TypeKind.Basic, name: "number" } } }], returns: [{ type: { kind: $.TypeKind.Basic, name: "number" } }, { type: { kind: $.TypeKind.Interface, name: 'GoError', methods: [{ name: 'Error', args: [], returns: [{ type: { kind: $.TypeKind.Basic, name: 'string' } }] }] } }] }, { name: "Next", args: [{ name: "n", type: { kind: $.TypeKind.Basic, name: "number" } }], returns: [{ type: { kind: $.TypeKind.Slice, elemType: { kind: $.TypeKind.Basic, name: "number" } } }] }, { name: "ReadByte", args: [], returns: [{ type: { kind: $.TypeKind.Basic, name: "number" } }, { type: { kind: $.TypeKind.Interface, name: 'GoError', methods: [{ name: 'Error', args: [], returns: [{ type: { kind: $.TypeKind.Basic, name: 'string' } }] }] } }] }, { name: "ReadRune", args: [], returns: [{ type: { kind: $.TypeKind.Basic, name: "number" } }, { type: { kind: $.TypeKind.Basic, name: "number" } }, { type: { kind: $.TypeKind.Interface, name: 'GoError', methods: [{ name: 'Error', args: [], returns: [{ type: { kind: $.TypeKind.Basic, name: 'string' } }] }] } }] }, { name: "UnreadRune", args: [], returns: [{ type: { kind: $.TypeKind.Interface, name: 'GoError', methods: [{ name: 'Error', args: [], returns: [{ type: { kind: $.TypeKind.Basic, name: 'string' } }] }] } }] }, { name: "UnreadByte", args: [], returns: [{ type: { kind: $.TypeKind.Interface, name: 'GoError', methods: [{ name: 'Error', args: [], returns: [{ type: { kind: $.TypeKind.Basic, name: 'string' } }] }] } }] }, { name: "ReadBytes", args: [{ name: "delim", type: { kind: $.TypeKind.Basic, name: "number" } }], returns: [{ type: { kind: $.TypeKind.Slice, elemType: { kind: $.TypeKind.Basic, name: "number" } } }, { type: { kind: $.TypeKind.Interface, name: 'GoError', methods: [{ name: 'Error', args: [], returns: [{ type: { kind: $.TypeKind.Basic, name: 'string' } }] }] } }] }, { name: "readSlice", args: [{ name: "delim", type: { kind: $.TypeKind.Basic, name: "number" } }], returns: [{ type: { kind: $.TypeKind.Slice, elemType: { kind: $.TypeKind.Basic, name: "number" } } }, { type: { kind: $.TypeKind.Interface, name: 'GoError', methods: [{ name: 'Error', args: [], returns: [{ type: { kind: $.TypeKind.Basic, name: 'string' } }] }] } }] }, { name: "ReadString", args: [{ name: "delim", type: { kind: $.TypeKind.Basic, name: "number" } }], returns: [{ type: { kind: $.TypeKind.Basic, name: "string" } }, { type: { kind: $.TypeKind.Interface, name: 'GoError', methods: [{ name: 'Error', args: [], returns: [{ type: { kind: $.TypeKind.Basic, name: 'string' } }] }] } }] }],
526
+ Buffer,
527
+ {"buf": { kind: $.TypeKind.Slice, elemType: { kind: $.TypeKind.Basic, name: "number" } }, "off": { kind: $.TypeKind.Basic, name: "number" }, "lastRead": "readOp"}
528
+ );
529
+ }
530
+
531
+ type readOp = number;
532
+
533
+ // Any other read operation.
534
+ let opRead: readOp = -1
535
+
536
+ // Non-read operation.
537
+ let opInvalid: readOp = 0
538
+
539
+ // Read rune of size 1.
540
+ let opReadRune1: readOp = 1
541
+
542
+ // Read rune of size 2.
543
+ let opReadRune2: readOp = 2
544
+
545
+ // Read rune of size 3.
546
+ let opReadRune3: readOp = 3
547
+
548
+ // Read rune of size 4.
549
+ let opReadRune4: readOp = 4
550
+
551
+ export let ErrTooLarge: $.GoError = errors.New("bytes.Buffer: too large")
552
+
553
+ let errNegativeRead: $.GoError = errors.New("bytes.Buffer: reader returned negative count from Read")
554
+
555
+ let maxInt: number = $.int((~(0 as number) >> 1))
556
+
557
+ export let MinRead: number = 512
558
+
559
+ // growSlice grows b by n, preserving the original content of b.
560
+ // If the allocation fails, it panics with ErrTooLarge.
561
+ export function growSlice(b: $.Bytes, n: number): $.Bytes {
562
+ using __defer = new $.DisposableStack();
563
+ __defer.defer(() => {
564
+ if (recover() != null) {
565
+ $.panic(ErrTooLarge)
566
+ }
567
+ });
568
+ // TODO(http://golang.org/issue/51462): We should rely on the append-make
569
+ // pattern so that the compiler can call runtime.growslice. For example:
570
+ // return append(b, make([]byte, n)...)
571
+ // This avoids unnecessary zero-ing of the first len(b) bytes of the
572
+ // allocated slice, but this pattern causes b to escape onto the heap.
573
+ //
574
+ // Instead use the append-make pattern with a nil slice to ensure that
575
+ // we allocate buffers rounded up to the closest size class.
576
+ let c = $.len(b) + n // ensure enough space for n elements
577
+
578
+ // The growth rate has historically always been 2x. In the future,
579
+ // we could rely purely on append to determine the growth rate.
580
+ if (c < 2 * $.cap(b)) {
581
+ // The growth rate has historically always been 2x. In the future,
582
+ // we could rely purely on append to determine the growth rate.
583
+ c = 2 * $.cap(b)
584
+ }
585
+ let b2 = $.append<number>(null, new Uint8Array(c))
586
+ let i = $.copy($.bytesToUint8Array(b2), $.bytesToUint8Array(b))
587
+ return $.goSlice(b2, undefined, i)
588
+ }
589
+
590
+ let errUnreadByte: $.GoError = errors.New("bytes.Buffer: UnreadByte: previous operation was not a successful read")
591
+
592
+ // NewBuffer creates and initializes a new [Buffer] using buf as its
593
+ // initial contents. The new [Buffer] takes ownership of buf, and the
594
+ // caller should not use buf after this call. NewBuffer is intended to
595
+ // prepare a [Buffer] to read existing data. It can also be used to set
596
+ // the initial size of the internal buffer for writing. To do that,
597
+ // buf should have the desired capacity but a length of zero.
598
+ //
599
+ // In most cases, new([Buffer]) (or just declaring a [Buffer] variable) is
600
+ // sufficient to initialize a [Buffer].
601
+ export function NewBuffer(buf: $.Bytes): Buffer | null {
602
+ return new Buffer({buf: buf})
603
+ }
604
+
605
+ // NewBufferString creates and initializes a new [Buffer] using string s as its
606
+ // initial contents. It is intended to prepare a buffer to read an existing
607
+ // string.
608
+ //
609
+ // In most cases, new([Buffer]) (or just declaring a [Buffer] variable) is
610
+ // sufficient to initialize a [Buffer].
611
+ export function NewBufferString(s: string): Buffer | null {
612
+ return new Buffer({buf: $.stringToBytes(s)})
613
+ }
614
+