Skip to content

fix: keep a leading U+FEFF when decoding long strings - #309

Open
shigma wants to merge 1 commit into
msgpack:mainfrom
shigma:fix/utf8-decode-bom
Open

shigma wants to merge 1 commit into
msgpack:mainfrom
shigma:fix/utf8-decode-bom

Conversation

@shigma

@shigma shigma commented Nov 29, 2026

Copy link
Copy Markdown

The shared TextDecoder is now created with ignoreBOM: true, so a string that begins with U+FEFF keeps it after a round trip, whatever its length.

Strings of more than 200 bytes are decoded by TextDecoder, which by default strips a leading byte order mark, while shorter strings are decoded by utf8DecodeJs(), which keeps it. The result of decoding therefore depended on the length of the string:

const short = "\uFEFF" + "a".repeat(10);
const long = "\uFEFF" + "a".repeat(300);

decode(encode(short)) === short; // true
decode(encode(long)) === long; // before: false, the first character is lost

The same applies to map keys longer than the key cache limit, which are decoded by the same function.

This complements #306, which makes utf8DecodeJs() decode invalid UTF-8 like TextDecoder and compares the two with ignoreBOM: true; the shared decoder now uses the same option.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant