Skip to content

Commit cc62c68

Browse files
committed
assume TextEncoder.prototype.encodeInto() is always available
1 parent 91d180d commit cc62c68

File tree

2 files changed

+8
-8
lines changed

2 files changed

+8
-8
lines changed

.vscode/settings.json

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@
66
"source.fixAll.eslint": true
77
},
88
"cSpell.words": [
9-
"tsdoc"
9+
"tsdoc",
10+
"whatwg"
1011
]
1112
}

src/utils/utf8.ts

Lines changed: 6 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -84,19 +84,18 @@ export function utf8EncodeJs(str: string, output: Uint8Array, outputOffset: numb
8484
}
8585
}
8686

87+
// TextEncoder and TextDecoder are standardized in whatwg encoding:
88+
// https://encoding.spec.whatwg.org/
89+
// and available in all the modern browsers:
90+
// https://caniuse.com/textencoder
91+
8792
const sharedTextEncoder = new TextEncoder();
8893
const TEXT_ENCODER_THRESHOLD = 200;
8994

90-
function utf8EncodeTEencode(str: string, output: Uint8Array, outputOffset: number): void {
91-
output.set(sharedTextEncoder.encode(str), outputOffset);
92-
}
93-
94-
function utf8EncodeTEencodeInto(str: string, output: Uint8Array, outputOffset: number): void {
95+
export function utf8EncodeTE(str: string, output: Uint8Array, outputOffset: number): void {
9596
sharedTextEncoder.encodeInto(str, output.subarray(outputOffset));
9697
}
9798

98-
export const utf8EncodeTE = (sharedTextEncoder.encodeInto as unknown) ? utf8EncodeTEencodeInto : utf8EncodeTEencode;
99-
10099
export function utf8Encode(str: string, output: Uint8Array, outputOffset: number): void {
101100
if (str.length > TEXT_ENCODER_THRESHOLD) {
102101
utf8EncodeTE(str, output, outputOffset);

0 commit comments

Comments
 (0)