From 7b916d06855d85e7231157b10f7fc483218229ab Mon Sep 17 00:00:00 2001 From: Hannes Papenberg Date: Fri, 13 Nov 2026 01:19:21 +0100 Subject: [PATCH] Decode surrogate pairs in StringHelper::unicode_to_utf8() and unicode_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 --- Tests/StringHelperTest.php | 6 +++++ docs/overview.md | 14 +++++++++++ src/StringHelper.php | 50 ++++++++++++++++++++------------------ 3 files changed, 47 insertions(+), 23 deletions(-) diff --git a/Tests/StringHelperTest.php b/Tests/StringHelperTest.php index 8d6fd578..9eaeafdc 100644 --- a/Tests/StringHelperTest.php +++ b/Tests/StringHelperTest.php @@ -455,6 +455,9 @@ public static function seedTestUnicodeToUtf8(): array return [ ["\u0422\u0435\u0441\u0442 \u0441\u0438\u0441\u0442\u0435\u043c\u044b", "Тест системы"], ["\u00dcberpr\u00fcfung der Systemumstellung", "Überprüfung der Systemumstellung"], + 'surrogate pair' => ['\uD83D\uDE00 smile', '😀 smile'], + 'surrogate pair in lowercase' => ['\ud83d\ude00', '😀'], + 'surrogate pair next to a BMP character' => ['\u00e9\uD83D\uDE00\u00e9', 'é😀é'], ]; } @@ -470,6 +473,9 @@ public static function seedTestUnicodeToUtf16(): array return [ ["\u0422\u0435\u0441\u0442 \u0441\u0438\u0441\u0442\u0435\u043c\u044b", "Тест системы"], ["\u00dcberpr\u00fcfung der Systemumstellung", "Überprüfung der Systemumstellung"], + 'surrogate pair' => ['\uD83D\uDE00 smile', '😀 smile'], + 'surrogate pair in lowercase' => ['\ud83d\ude00', '😀'], + 'surrogate pair next to a BMP character' => ['\u00e9\uD83D\uDE00\u00e9', 'é😀é'], ]; } diff --git a/docs/overview.md b/docs/overview.md index 5c220497..659abb9e 100644 --- a/docs/overview.md +++ b/docs/overview.md @@ -1,3 +1,17 @@ # String package TODO + +## `StringHelper::unicode_to_utf8()` and `unicode_to_utf16()` + +Both methods replace `\uXXXX` escape sequences, as they appear in JSON, with the characters they +stand for. A surrogate pair such as `\uD83D\uDE00` is decoded into one character (U+1F600). Despite +its name, `unicode_to_utf16()` also returns UTF-8. The two methods differ only in how they handle a +lone surrogate (one without its partner): `unicode_to_utf8()` encodes it as three bytes that are not +valid UTF-8, and `unicode_to_utf16()` turns it into `?`. + +```php +use Joomla\String\StringHelper; + +StringHelper::unicode_to_utf8('caf\u00e9 \uD83D\uDE00'); // 'café 😀' +``` diff --git a/src/StringHelper.php b/src/StringHelper.php index 98f73e7d..7257cd38 100644 --- a/src/StringHelper.php +++ b/src/StringHelper.php @@ -906,7 +906,9 @@ public static function compliant($str) } /** - * Converts Unicode sequences to UTF-8 string. + * Converts Unicode escape sequences (\uXXXX) to a UTF-8 string. + * + * A surrogate pair such as "\uD83D\uDE00" is decoded into a single character. * * @param string $str Unicode string to convert * @@ -916,40 +918,42 @@ public static function compliant($str) */ public static function unicode_to_utf8($str) { - if (\extension_loaded('mbstring')) { - return preg_replace_callback( - '/\\\\u([0-9a-fA-F]{4})/', - static function ($match) { - return mb_convert_encoding(pack('H*', $match[1]), 'UTF-8', 'UCS-2BE'); - }, - $str - ); - } + return preg_replace_callback( + '/\\\\u(D[89AB][0-9A-F]{2})\\\\u(D[C-F][0-9A-F]{2})|\\\\u([0-9A-F]{4})/i', + static function ($match) { + if ($match[1] !== '') { + return mb_convert_encoding(pack('H*', $match[1] . $match[2]), 'UTF-8', 'UTF-16BE'); + } - return $str; + return mb_convert_encoding(pack('H*', $match[3]), 'UTF-8', 'UCS-2BE'); + }, + $str + ); } /** - * Converts Unicode sequences to UTF-16 string. + * Converts UTF-16 escape sequences (\uXXXX), as used in JSON, to a UTF-8 string. + * + * A surrogate pair such as "\uD83D\uDE00" is decoded into a single character. * * @param string $str Unicode string to convert * - * @return string UTF-16 string + * @return string UTF-8 string. Despite the method name, the result is UTF-8, not UTF-16. * * @since 1.3.0 */ public static function unicode_to_utf16($str) { - if (\extension_loaded('mbstring')) { - return preg_replace_callback( - '/\\\\u([0-9a-fA-F]{4})/', - static function ($match) { - return mb_convert_encoding(pack('H*', $match[1]), 'UTF-8', 'UTF-16BE'); - }, - $str - ); - } + return preg_replace_callback( + '/\\\\u(D[89AB][0-9A-F]{2})\\\\u(D[C-F][0-9A-F]{2})|\\\\u([0-9A-F]{4})/i', + static function ($match) { + if ($match[1] !== '') { + return mb_convert_encoding(pack('H*', $match[1] . $match[2]), 'UTF-8', 'UTF-16BE'); + } - return $str; + return mb_convert_encoding(pack('H*', $match[3]), 'UTF-8', 'UTF-16BE'); + }, + $str + ); } }