GSUnicode: fix UTF-16 encoding byte order and length - #122
Open
DTW-Thalion wants to merge 2 commits into
Open
Conversation
The UTF-16 branch of GSUnicodeToEncoding tested the UTF-32 encoding constants when deciding whether to swap bytes, so UTF-16BE and UTF-16LE were never byte-swapped, and it mixed byte and character counts when sizing the copy, truncating multi-character output. Convert one character at a time, swapping for the non-native byte order, and emit a byte order mark only for the endianness-neutral encoding.
DTW-Thalion
force-pushed
the
fix/gsunicode-utf16-encode
branch
from
July 24, 2026 15:43
b379000 to
dd33e22
Compare
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
The UTF-16 branch of GSUnicodeToEncoding had two defects that corrupted CFStringGetBytes and CFStringCreateExternalRepresentation output for UTF-16:
The byte-order swap was guarded by the UTF-32BE/UTF-32LE constants (a copy of the UTF-32 branch), so UTF-16BE and UTF-16LE were never byte-swapped and came out in host order.
The copy length was computed as the minimum of a byte count and a character count and then used as a byte count, so any string longer than one character was truncated.
The branch now converts one character at a time, swapping to the requested byte order, and prepends a byte order mark only for the endianness-neutral UTF-16 encoding, matching Apple.
Tests/CFString/getbytes_utf16.m covers UTF-16BE/LE for single characters, multi-character strings and a surrogate pair. The existing encodings.m assertion that the plain and external UTF-16 conversions produce the same length is corrected: the external form adds a two-byte BOM. Both were verified against Apple CoreFoundation.