Skip to content

[4.x] Decode surrogate pairs in StringHelper::unicode_to_utf8() and unicode_to_utf16() - #79

Open
Hackwar wants to merge 1 commit into
joomla-framework:4.x-devfrom
Hackwar:4.x-unicode-surrogate-pairs
Open

Hackwar wants to merge 1 commit into
joomla-framework:4.x-devfrom
Hackwar:4.x-unicode-surrogate-pairs

Conversation

@Hackwar

@Hackwar Hackwar commented Nov 13, 2026

Copy link
Copy Markdown
Contributor

Summary of Changes

StringHelper::unicode_to_utf8() and unicode_to_utf16() convert each \uXXXX escape on its own. A surrogate pair, which is how JSON encodes characters outside the BMP, is not decoded into one character:

StringHelper::unicode_to_utf8('\uD83D\uDE00');  // "\xED\xA0\xBD\xED\xB8\x80" (CESU-8, invalid UTF-8)
StringHelper::unicode_to_utf16('\uD83D\uDE00'); // '??'

Both methods now match a high/low surrogate pair first, case-insensitively, and decode the two code units together as UTF-16BE. All other escapes are converted as before: unicode_to_utf8() uses UCS-2BE and unicode_to_utf16() UTF-16BE, so a lone surrogate keeps its current result in both.

Other changes in the same two methods:

  • The extension_loaded('mbstring') checks are removed. Without the extension, the methods silently returned their input, but symfony/polyfill-mbstring is a hard requirement and provides mb_convert_encoding().
  • unicode_to_utf16() has always returned UTF-8, as the existing tests expect, but its docblock said "UTF-16 string". The docblock now says it returns UTF-8. Deprecating the misleadingly named method instead would be a separate decision.

Testing Instructions

seedTestUnicodeToUtf8 and seedTestUnicodeToUtf16 get three new cases each: a surrogate pair, a pair in lowercase hex, and a pair between two BMP escapes. All six fail before the change and pass after it. vendor/bin/phpunit is green, phpcs is clean, and PHPStan reports no errors locally. The PHPStan job in CI fails on 4.x-dev as well (mb_trim and the other PHP 8.4 polyfill functions are not found), and #66 fixes that.

Documentation Changes Required

docs/overview.md (still "TODO" on 4.x-dev) gets a section on unicode_to_utf8() and unicode_to_utf16(). The owner's docs PR #72 does not describe these methods, but it rewrites docs/overview.md, so whichever is merged second has to fold this section into #72's structure. #73, #75, #77 and #78 also append sections to the stub, so expect small conflicts there.

🤖 Generated with Claude Code

…_to_utf16()

Each \uXXXX escape was converted on its own, so a JSON-style surrogate
pair such as \uD83D\uDE00 became six bytes of invalid UTF-8 (CESU-8) in
unicode_to_utf8() and "??" in unicode_to_utf16(). Match surrogate pairs
first and decode them together as UTF-16BE.

The mbstring checks are gone, because symfony/polyfill-mbstring is a
hard requirement; without the extension the methods silently returned
their input. unicode_to_utf16() returns UTF-8, which its docblock now
says.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
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