|
1 | 1 | /* eslint-disable @typescript-eslint/no-unnecessary-condition */
|
2 | 2 | import { UINT32_MAX } from "./int";
|
3 | 3 |
|
4 |
| -const TEXT_ENCODING_AVAILABLE = |
5 |
| - (typeof process === "undefined" || process?.env?.["TEXT_ENCODING"] !== "never") && |
6 |
| - typeof TextEncoder !== "undefined" && |
7 |
| - typeof TextDecoder !== "undefined"; |
8 |
| - |
9 | 4 | export function utf8Count(str: string): number {
|
10 | 5 | const strLength = str.length;
|
11 | 6 |
|
@@ -89,22 +84,26 @@ export function utf8EncodeJs(str: string, output: Uint8Array, outputOffset: numb
|
89 | 84 | }
|
90 | 85 | }
|
91 | 86 |
|
92 |
| -const sharedTextEncoder = TEXT_ENCODING_AVAILABLE ? new TextEncoder() : undefined; |
93 |
| -export const TEXT_ENCODER_THRESHOLD = !TEXT_ENCODING_AVAILABLE |
94 |
| - ? UINT32_MAX |
95 |
| - : typeof process !== "undefined" && process?.env?.["TEXT_ENCODING"] !== "force" |
96 |
| - ? 200 |
97 |
| - : 0; |
| 87 | +const sharedTextEncoder = new TextEncoder(); |
| 88 | +const TEXT_ENCODER_THRESHOLD = 200; |
98 | 89 |
|
99 | 90 | function utf8EncodeTEencode(str: string, output: Uint8Array, outputOffset: number): void {
|
100 |
| - output.set(sharedTextEncoder!.encode(str), outputOffset); |
| 91 | + output.set(sharedTextEncoder.encode(str), outputOffset); |
101 | 92 | }
|
102 | 93 |
|
103 | 94 | function utf8EncodeTEencodeInto(str: string, output: Uint8Array, outputOffset: number): void {
|
104 |
| - sharedTextEncoder!.encodeInto(str, output.subarray(outputOffset)); |
| 95 | + sharedTextEncoder.encodeInto(str, output.subarray(outputOffset)); |
105 | 96 | }
|
106 | 97 |
|
107 |
| -export const utf8EncodeTE = sharedTextEncoder?.encodeInto ? utf8EncodeTEencodeInto : utf8EncodeTEencode; |
| 98 | +export const utf8EncodeTE = (sharedTextEncoder.encodeInto as unknown) ? utf8EncodeTEencodeInto : utf8EncodeTEencode; |
| 99 | + |
| 100 | +export function utf8Encode(str: string, output: Uint8Array, outputOffset: number): void { |
| 101 | + if (str.length > TEXT_ENCODER_THRESHOLD) { |
| 102 | + utf8EncodeTE(str, output, outputOffset); |
| 103 | + } else { |
| 104 | + utf8EncodeJs(str, output, outputOffset); |
| 105 | + } |
| 106 | +} |
108 | 107 |
|
109 | 108 | const CHUNK_SIZE = 0x1_000;
|
110 | 109 |
|
@@ -157,14 +156,18 @@ export function utf8DecodeJs(bytes: Uint8Array, inputOffset: number, byteLength:
|
157 | 156 | return result;
|
158 | 157 | }
|
159 | 158 |
|
160 |
| -const sharedTextDecoder = TEXT_ENCODING_AVAILABLE ? new TextDecoder() : null; |
161 |
| -export const TEXT_DECODER_THRESHOLD = !TEXT_ENCODING_AVAILABLE |
162 |
| - ? UINT32_MAX |
163 |
| - : typeof process !== "undefined" && process?.env?.["TEXT_DECODER"] !== "force" |
164 |
| - ? 200 |
165 |
| - : 0; |
| 159 | +const sharedTextDecoder = new TextDecoder(); |
| 160 | +const TEXT_DECODER_THRESHOLD = 200; |
166 | 161 |
|
167 | 162 | export function utf8DecodeTD(bytes: Uint8Array, inputOffset: number, byteLength: number): string {
|
168 | 163 | const stringBytes = bytes.subarray(inputOffset, inputOffset + byteLength);
|
169 |
| - return sharedTextDecoder!.decode(stringBytes); |
| 164 | + return sharedTextDecoder.decode(stringBytes); |
| 165 | +} |
| 166 | + |
| 167 | +export function utf8Decode(bytes: Uint8Array, inputOffset: number, byteLength: number): string { |
| 168 | + if (byteLength > TEXT_DECODER_THRESHOLD) { |
| 169 | + return utf8DecodeTD(bytes, inputOffset, byteLength); |
| 170 | + } else { |
| 171 | + return utf8DecodeJs(bytes, inputOffset, byteLength); |
| 172 | + } |
170 | 173 | }
|
0 commit comments