Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 4 additions & 0 deletions ext/web/08_text_encoding.js
Original file line number Diff line number Diff line change
Expand Up @@ -119,6 +119,10 @@ class TextDecoder {
stream = options.stream;
}

if (stream && input.length === 0) {
return "";
}

try {
/** @type {ArrayBufferLike} */
let buffer = input;
Expand Down
53 changes: 53 additions & 0 deletions tests/unit/text_encoding_test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -324,3 +324,56 @@ Deno.test(
assertStrictEquals(cancelled, true);
},
);

Deno.test(
"TextDecoder should handle empty chunk in stream mode (legacy encodings)",
() => {
// big5: [0xa4, 0xa4] => "中" (U+4E2D)
{
const u8 = new Uint8Array([0xa4, 0xa4]);
const str = new TextDecoder("big5").decode(u8);
assertEquals(str, "\u4e2d");

const d = new TextDecoder("big5");
const chunks = [
d.decode(u8.subarray(0, 1), { stream: true }),
d.decode(u8.subarray(1), { stream: true }),
d.decode(new Uint8Array(), { stream: true }),
d.decode(),
];
assertEquals(chunks.join(""), str);
}

// shift_jis: [0x82, 0xa0] => "あ" (U+3042)
{
const u8 = new Uint8Array([0x82, 0xa0]);
const str = new TextDecoder("shift_jis").decode(u8);
assertEquals(str, "\u3042");

const d = new TextDecoder("shift_jis");
const chunks = [
d.decode(u8.subarray(0, 1), { stream: true }),
d.decode(u8.subarray(1), { stream: true }),
d.decode(new Uint8Array(), { stream: true }),
d.decode(),
];
assertEquals(chunks.join(""), str);
}

// euc-kr: [0xb0, 0xa1] => "가" (U+AC00)
{
const u8 = new Uint8Array([0xb0, 0xa1]);
const str = new TextDecoder("euc-kr").decode(u8);
assertEquals(str, "\uac00");

const d = new TextDecoder("euc-kr");
const chunks = [
d.decode(u8.subarray(0, 1), { stream: true }),
d.decode(u8.subarray(1), { stream: true }),
d.decode(new Uint8Array(), { stream: true }),
d.decode(),
];
assertEquals(chunks.join(""), str);
}
},
);
Loading