| [9af201e] | 1 | const bufLength = 1024 * 16;
|
|---|
| 2 |
|
|---|
| 3 | // Provide a fallback for older environments.
|
|---|
| 4 | const td =
|
|---|
| 5 | typeof TextDecoder !== 'undefined'
|
|---|
| 6 | ? /* #__PURE__ */ new TextDecoder()
|
|---|
| 7 | : typeof Buffer !== 'undefined'
|
|---|
| 8 | ? {
|
|---|
| 9 | decode(buf: Uint8Array): string {
|
|---|
| 10 | const out = Buffer.from(buf.buffer, buf.byteOffset, buf.byteLength);
|
|---|
| 11 | return out.toString();
|
|---|
| 12 | },
|
|---|
| 13 | }
|
|---|
| 14 | : {
|
|---|
| 15 | decode(buf: Uint8Array): string {
|
|---|
| 16 | let out = '';
|
|---|
| 17 | for (let i = 0; i < buf.length; i++) {
|
|---|
| 18 | out += String.fromCharCode(buf[i]);
|
|---|
| 19 | }
|
|---|
| 20 | return out;
|
|---|
| 21 | },
|
|---|
| 22 | };
|
|---|
| 23 |
|
|---|
| 24 | export class StringWriter {
|
|---|
| 25 | pos = 0;
|
|---|
| 26 | private out = '';
|
|---|
| 27 | private buffer = new Uint8Array(bufLength);
|
|---|
| 28 |
|
|---|
| 29 | write(v: number): void {
|
|---|
| 30 | const { buffer } = this;
|
|---|
| 31 | buffer[this.pos++] = v;
|
|---|
| 32 | if (this.pos === bufLength) {
|
|---|
| 33 | this.out += td.decode(buffer);
|
|---|
| 34 | this.pos = 0;
|
|---|
| 35 | }
|
|---|
| 36 | }
|
|---|
| 37 |
|
|---|
| 38 | flush(): string {
|
|---|
| 39 | const { buffer, out, pos } = this;
|
|---|
| 40 | return pos > 0 ? out + td.decode(buffer.subarray(0, pos)) : out;
|
|---|
| 41 | }
|
|---|
| 42 | }
|
|---|
| 43 |
|
|---|
| 44 | export class StringReader {
|
|---|
| 45 | pos = 0;
|
|---|
| 46 | declare private buffer: string;
|
|---|
| 47 |
|
|---|
| 48 | constructor(buffer: string) {
|
|---|
| 49 | this.buffer = buffer;
|
|---|
| 50 | }
|
|---|
| 51 |
|
|---|
| 52 | next(): number {
|
|---|
| 53 | return this.buffer.charCodeAt(this.pos++);
|
|---|
| 54 | }
|
|---|
| 55 |
|
|---|
| 56 | peek(): number {
|
|---|
| 57 | return this.buffer.charCodeAt(this.pos);
|
|---|
| 58 | }
|
|---|
| 59 |
|
|---|
| 60 | indexOf(char: string): number {
|
|---|
| 61 | const { buffer, pos } = this;
|
|---|
| 62 | const idx = buffer.indexOf(char, pos);
|
|---|
| 63 | return idx === -1 ? buffer.length : idx;
|
|---|
| 64 | }
|
|---|
| 65 | }
|
|---|