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:
Fabien Potencier
2020-06-08 12:43:20 +02:00
2 changed files with 10 additions and 2 deletions
+4 -2
View File
@@ -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;
}
+6
View File
@@ -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'],