mirror of
https://github.com/twigphp/Twig.git
synced 2026-09-14 19:36:43 +00:00
bug #3256 Fix twig_compare float-string trailing whitespace + refactor (guilliamxavier)
This PR was squashed before being merged into the 3.x branch.
Discussion
----------
Fix twig_compare float-string trailing whitespace + refactor
Fixes #3345
Refs #2966 (and #3186)
(feel free to discard the "Factorize" commit if you favor performance over maintainability)
Commits
-------
d6822aeb Fix twig_compare float-string trailing whitespace + refactor
This commit is contained in:
@@ -971,16 +971,18 @@ function twig_compare($a, $b)
|
||||
if (is_nan($a)) {
|
||||
return 1;
|
||||
}
|
||||
$b = trim($b);
|
||||
if (!is_numeric($b)) {
|
||||
return (string) $a <=> $b;
|
||||
}
|
||||
|
||||
return (float) $a <=> $b;
|
||||
return $a <=> (float) $b;
|
||||
}
|
||||
if (\is_float($b) && \is_string($a)) {
|
||||
if (\is_string($a) && \is_float($b)) {
|
||||
if (is_nan($b)) {
|
||||
return 1;
|
||||
}
|
||||
$a = trim($a);
|
||||
if (!is_numeric($a)) {
|
||||
return $a <=> (string) $b;
|
||||
}
|
||||
|
||||
@@ -306,6 +306,12 @@ class CoreTest extends TestCase
|
||||
[-1, 42, 'abc42'],
|
||||
[-1, 0, 'abc42'],
|
||||
|
||||
[0, 42.0, ' 42.0'],
|
||||
[0, 42.0, '42.0 '],
|
||||
[-1, 42.0, '42.0abc'],
|
||||
[-1, 42.0, 'abc42.0'],
|
||||
[-1, 0.0, 'abc42.0'],
|
||||
|
||||
[0, INF, 'INF'],
|
||||
[0, -INF, '-INF'],
|
||||
[0, INF, '1e1000'],
|
||||
|
||||
Reference in New Issue
Block a user