1
0
Fork 0
mirror of https://github.com/denoland/deno.git synced 2025-01-03 12:58:54 -05:00

add encodeInto to TextEncoder (#2558)

This commit is contained in:
andy finch 2019-06-21 18:32:14 -04:00 committed by Ryan Dahl
parent 20f41e719d
commit eb93dc58a1
2 changed files with 65 additions and 1 deletions

View file

@ -461,6 +461,11 @@ export class TextDecoder {
} }
} }
interface TextEncoderEncodeIntoResult {
read: number;
written: number;
}
export class TextEncoder { export class TextEncoder {
/** Returns "utf-8". */ /** Returns "utf-8". */
readonly encoding = "utf-8"; readonly encoding = "utf-8";
@ -484,6 +489,36 @@ export class TextEncoder {
return new Uint8Array(output); return new Uint8Array(output);
} }
encodeInto(input: string, dest: Uint8Array): TextEncoderEncodeIntoResult {
const encoder = new UTF8Encoder();
const inputStream = new Stream(stringToCodePoints(input));
let written = 0;
let read = 0;
while (true) {
const result = encoder.handler(inputStream.read());
if (result === FINISHED) {
break;
}
read++;
if (Array.isArray(result)) {
dest.set(result, written);
written += result.length;
if (result.length > 3) {
// increment read a second time if greater than U+FFFF
read++;
}
} else {
dest[written] = result;
written++;
}
}
return {
read,
written
};
}
get [Symbol.toStringTag](): string { get [Symbol.toStringTag](): string {
return "TextEncoder"; return "TextEncoder";
} }

View file

@ -91,7 +91,7 @@ test(function textDecoderErrorEncoding(): void {
assert(didThrow); assert(didThrow);
}); });
test(function textEncoder2(): void { test(function textEncoder(): void {
const fixture = "𝓽𝓮𝔁𝓽"; const fixture = "𝓽𝓮𝔁𝓽";
const encoder = new TextEncoder(); const encoder = new TextEncoder();
// prettier-ignore // prettier-ignore
@ -103,6 +103,35 @@ test(function textEncoder2(): void {
]); ]);
}); });
test(function textEncodeInto(): void {
const fixture = "text";
const encoder = new TextEncoder();
const bytes = new Uint8Array(5);
const result = encoder.encodeInto(fixture, bytes);
assertEquals(result.read, 4);
assertEquals(result.written, 4);
// prettier-ignore
assertEquals(Array.from(bytes), [
0x74, 0x65, 0x78, 0x74, 0x00,
]);
});
test(function textEncodeInto2(): void {
const fixture = "𝓽𝓮𝔁𝓽";
const encoder = new TextEncoder();
const bytes = new Uint8Array(17);
const result = encoder.encodeInto(fixture, bytes);
assertEquals(result.read, 8);
assertEquals(result.written, 16);
// prettier-ignore
assertEquals(Array.from(bytes), [
0xf0, 0x9d, 0x93, 0xbd,
0xf0, 0x9d, 0x93, 0xae,
0xf0, 0x9d, 0x94, 0x81,
0xf0, 0x9d, 0x93, 0xbd, 0x00,
]);
});
test(function textDecoderSharedUint8Array(): void { test(function textDecoderSharedUint8Array(): void {
const ab = new SharedArrayBuffer(6); const ab = new SharedArrayBuffer(6);
const dataView = new DataView(ab); const dataView = new DataView(ab);