Repository navigation
Fix Query::sort ordering of non-ASCII names - #233
Merged
Merged
Conversation
The URL Standard compares names by UTF-16 code units. Compare UTF-16BE strings with strcmp instead of decimal code point strings.
nyamsprod
approved these changes
Nov 2, 2026
Member
|
@gggg5151 the PR is approved and accepted but I may change its implementation. Thanks for the fix. |
Contributor
Author
|
Sounds good. Thanks for the quick review! |
Member
|
The rewritten implementation using your improvements. public function sort(): self
{
if ([] === $this->pairs) {
return $this;
}
$toUtf16 = static fn (string $str): string => mb_check_encoding($str, 'UTF-8')
? mb_convert_encoding($str, 'UTF-16BE', 'UTF-8')
: throw new ValueError('The parameter name (hex: '.bin2hex($str).') must contain valid UTF-8 characters.')
;
$parameters = array_map(static fn (array $pair): array => ['pair' => $pair, 'name' => $toUtf16($pair[0])], $this->pairs);
usort($parameters, static fn (array $a, array $b): int => strcmp($a['name'], $b['name']));
$pairs = array_column($parameters, 'pair');
return $pairs === $this->pairs ? $this : self::fromPairs($pairs, $this->separator);
} |
Contributor
Author
|
Thanks for the rewrite, this looks cleaner! Glad I could help. |
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.
Summary
Query::sortandURLSearchParams::sortdo not always follow the order required by theURL Standard, which compares names by UTF-16 code units.
The current code turns each name into decimal code points and compares them with
strcmp,so it compares digits as text. For example,
abecomes97andあbecomes12354, andあis sorted first.The order of the names is
あ,い,a. The expected order, which Node'sURLSearchParamsalso returns, isa,あ,い.The existing
ffi&🌈test passes only by coincidence, and the WPT caseé&e�&éwas not in the test list.Changes
strcmpcompares bytes, so this matches code unit order.ValueErrorfor invalid UTF-8 withmb_check_encoding, and add a test for it.URLSearchParams::sortdocblock and the changelog.The order changes for some queries with non-ASCII names. That is the intended result of this fix.
Testing
The full test suite and PHPStan pass on PHP 8.4. php-cs-fixer reports only
interfaces/HostRecord.php, which is also reported on master and is not changed here.I used Claude Code for this investigation and for drafting the change. I reviewed the change and can answer questions about it.
🤖 Generated with Claude Code