Skip to content

Commit

Permalink
add BOM skip logic
Browse files Browse the repository at this point in the history
  • Loading branch information
kena0ki committed Jan 23, 2021
1 parent 93aab95 commit 3d941f2
Show file tree
Hide file tree
Showing 2 changed files with 15 additions and 2 deletions.
13 changes: 13 additions & 0 deletions src/common/input/TextDecoder.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -232,6 +232,19 @@ describe('text encodings', () => {
}
assert(decoded, 'Ä€𝄞Ö𝄞€Ü𝄞€');
});

it('BOMs (3 byte sequences) - advance by 2', () => {
const decoder = new Utf8ToUtf32();
const target = new Uint32Array(5);
const utf8Data = fromByteString('\xef\xbb\xbf\xef\xbb\xbf');
let decoded = '';
for (let i = 0; i < utf8Data.length; i += 2) {
const written = decoder.decode(utf8Data.slice(i, i + 2), target);
decoded += toString(target, written);
}
assert.equal(decoded, '');
});

it('test break after 3 bytes - issue #2495', () => {
const decoder = new Utf8ToUtf32();
const target = new Uint32Array(5);
Expand Down
4 changes: 2 additions & 2 deletions src/common/input/TextDecoder.ts
Original file line number Diff line number Diff line change
Expand Up @@ -192,8 +192,8 @@ export class Utf8ToUtf32 {
target[size++] = cp;
}
} else if (type === 3) {
if (cp < 0x0800 || (cp >= 0xD800 && cp <= 0xDFFF)) {
// illegal codepoint
if (cp < 0x0800 || (cp >= 0xD800 && cp <= 0xDFFF) || cp === 0xFEFF) {
// illegal codepoint or BOM
} else {
target[size++] = cp;
}
Expand Down

0 comments on commit 3d941f2

Please sign in to comment.