mirror of
https://github.com/denoland/deno.git
synced 2025-09-29 05:34:49 +00:00
Check destination length in encodeInto (#5078)
This commit is contained in:
parent
91369841ef
commit
76c77bb32c
2 changed files with 23 additions and 15 deletions
|
@ -158,6 +158,19 @@ unitTest(function textEncodeInto2(): void {
|
||||||
]);
|
]);
|
||||||
});
|
});
|
||||||
|
|
||||||
|
unitTest(function textEncodeInto3(): void {
|
||||||
|
const fixture = "𝓽𝓮𝔁𝓽";
|
||||||
|
const encoder = new TextEncoder();
|
||||||
|
const bytes = new Uint8Array(5);
|
||||||
|
const result = encoder.encodeInto(fixture, bytes);
|
||||||
|
assertEquals(result.read, 2);
|
||||||
|
assertEquals(result.written, 4);
|
||||||
|
// prettier-ignore
|
||||||
|
assertEquals(Array.from(bytes), [
|
||||||
|
0xf0, 0x9d, 0x93, 0xbd, 0x00,
|
||||||
|
]);
|
||||||
|
});
|
||||||
|
|
||||||
unitTest(function textDecoderSharedUint8Array(): void {
|
unitTest(function textDecoderSharedUint8Array(): void {
|
||||||
const ab = new SharedArrayBuffer(6);
|
const ab = new SharedArrayBuffer(6);
|
||||||
const dataView = new DataView(ab);
|
const dataView = new DataView(ab);
|
||||||
|
|
|
@ -55,13 +55,13 @@ function stringToCodePoints(input: string): number[] {
|
||||||
}
|
}
|
||||||
|
|
||||||
class UTF8Encoder implements Encoder {
|
class UTF8Encoder implements Encoder {
|
||||||
handler(codePoint: number): number | number[] {
|
handler(codePoint: number): "finished" | number[] {
|
||||||
if (codePoint === END_OF_STREAM) {
|
if (codePoint === END_OF_STREAM) {
|
||||||
return FINISHED;
|
return "finished";
|
||||||
}
|
}
|
||||||
|
|
||||||
if (inRange(codePoint, 0x00, 0x7f)) {
|
if (inRange(codePoint, 0x00, 0x7f)) {
|
||||||
return codePoint;
|
return [codePoint];
|
||||||
}
|
}
|
||||||
|
|
||||||
let count: number;
|
let count: number;
|
||||||
|
@ -144,7 +144,7 @@ interface Decoder {
|
||||||
}
|
}
|
||||||
|
|
||||||
interface Encoder {
|
interface Encoder {
|
||||||
handler(codePoint: number): number | number[];
|
handler(codePoint: number): "finished" | number[];
|
||||||
}
|
}
|
||||||
|
|
||||||
class SingleByteDecoder implements Decoder {
|
class SingleByteDecoder implements Decoder {
|
||||||
|
@ -534,14 +534,10 @@ export class TextEncoder {
|
||||||
|
|
||||||
while (true) {
|
while (true) {
|
||||||
const result = encoder.handler(inputStream.read());
|
const result = encoder.handler(inputStream.read());
|
||||||
if (result === FINISHED) {
|
if (result === "finished") {
|
||||||
break;
|
break;
|
||||||
}
|
}
|
||||||
if (Array.isArray(result)) {
|
|
||||||
output.push(...result);
|
output.push(...result);
|
||||||
} else {
|
|
||||||
output.push(result);
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
|
||||||
return new Uint8Array(output);
|
return new Uint8Array(output);
|
||||||
|
@ -554,11 +550,11 @@ export class TextEncoder {
|
||||||
let read = 0;
|
let read = 0;
|
||||||
while (true) {
|
while (true) {
|
||||||
const result = encoder.handler(inputStream.read());
|
const result = encoder.handler(inputStream.read());
|
||||||
if (result === FINISHED) {
|
if (result === "finished") {
|
||||||
break;
|
break;
|
||||||
}
|
}
|
||||||
|
if (dest.length - written >= result.length) {
|
||||||
read++;
|
read++;
|
||||||
if (Array.isArray(result)) {
|
|
||||||
dest.set(result, written);
|
dest.set(result, written);
|
||||||
written += result.length;
|
written += result.length;
|
||||||
if (result.length > 3) {
|
if (result.length > 3) {
|
||||||
|
@ -566,8 +562,7 @@ export class TextEncoder {
|
||||||
read++;
|
read++;
|
||||||
}
|
}
|
||||||
} else {
|
} else {
|
||||||
dest[written] = result;
|
break;
|
||||||
written++;
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
Loading…
Add table
Add a link
Reference in a new issue