Skip to content

Commit eb93dc5

Browse files
afinch7ry
authored andcommitted
add encodeInto to TextEncoder (#2558)
1 parent 20f41e7 commit eb93dc5

File tree

2 files changed

+65
-1
lines changed

2 files changed

+65
-1
lines changed

js/text_encoding.ts

Lines changed: 35 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -461,6 +461,11 @@ export class TextDecoder {
461461
}
462462
}
463463

464+
interface TextEncoderEncodeIntoResult {
465+
read: number;
466+
written: number;
467+
}
468+
464469
export class TextEncoder {
465470
/** Returns "utf-8". */
466471
readonly encoding = "utf-8";
@@ -484,6 +489,36 @@ export class TextEncoder {
484489

485490
return new Uint8Array(output);
486491
}
492+
encodeInto(input: string, dest: Uint8Array): TextEncoderEncodeIntoResult {
493+
const encoder = new UTF8Encoder();
494+
const inputStream = new Stream(stringToCodePoints(input));
495+
496+
let written = 0;
497+
let read = 0;
498+
while (true) {
499+
const result = encoder.handler(inputStream.read());
500+
if (result === FINISHED) {
501+
break;
502+
}
503+
read++;
504+
if (Array.isArray(result)) {
505+
dest.set(result, written);
506+
written += result.length;
507+
if (result.length > 3) {
508+
// increment read a second time if greater than U+FFFF
509+
read++;
510+
}
511+
} else {
512+
dest[written] = result;
513+
written++;
514+
}
515+
}
516+
517+
return {
518+
read,
519+
written
520+
};
521+
}
487522
get [Symbol.toStringTag](): string {
488523
return "TextEncoder";
489524
}

js/text_encoding_test.ts

Lines changed: 30 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -91,7 +91,7 @@ test(function textDecoderErrorEncoding(): void {
9191
assert(didThrow);
9292
});
9393

94-
test(function textEncoder2(): void {
94+
test(function textEncoder(): void {
9595
const fixture = "𝓽𝓮𝔁𝓽";
9696
const encoder = new TextEncoder();
9797
// prettier-ignore
@@ -103,6 +103,35 @@ test(function textEncoder2(): void {
103103
]);
104104
});
105105

106+
test(function textEncodeInto(): void {
107+
const fixture = "text";
108+
const encoder = new TextEncoder();
109+
const bytes = new Uint8Array(5);
110+
const result = encoder.encodeInto(fixture, bytes);
111+
assertEquals(result.read, 4);
112+
assertEquals(result.written, 4);
113+
// prettier-ignore
114+
assertEquals(Array.from(bytes), [
115+
0x74, 0x65, 0x78, 0x74, 0x00,
116+
]);
117+
});
118+
119+
test(function textEncodeInto2(): void {
120+
const fixture = "𝓽𝓮𝔁𝓽";
121+
const encoder = new TextEncoder();
122+
const bytes = new Uint8Array(17);
123+
const result = encoder.encodeInto(fixture, bytes);
124+
assertEquals(result.read, 8);
125+
assertEquals(result.written, 16);
126+
// prettier-ignore
127+
assertEquals(Array.from(bytes), [
128+
0xf0, 0x9d, 0x93, 0xbd,
129+
0xf0, 0x9d, 0x93, 0xae,
130+
0xf0, 0x9d, 0x94, 0x81,
131+
0xf0, 0x9d, 0x93, 0xbd, 0x00,
132+
]);
133+
});
134+
106135
test(function textDecoderSharedUint8Array(): void {
107136
const ab = new SharedArrayBuffer(6);
108137
const dataView = new DataView(ab);

0 commit comments

Comments
 (0)