diff --git a/lib/Twig/Node/Expression/Binary/EndsWith.php b/lib/Twig/Node/Expression/Binary/EndsWith.php index 5de6c72d2..93b3b96f0 100644 --- a/lib/Twig/Node/Expression/Binary/EndsWith.php +++ b/lib/Twig/Node/Expression/Binary/EndsWith.php @@ -12,14 +12,14 @@ class Twig_Node_Expression_Binary_EndsWith extends Twig_Node_Expression_Binary { public function compile(Twig_Compiler $compiler) { + $left = $compiler->getVarName(); + $right = $compiler->getVarName(); $compiler - ->raw('(0 === substr_compare(') + ->raw(sprintf('(is_string($%s = ', $left)) ->subcompile($this->getNode('left')) - ->raw(', ') + ->raw(sprintf(') && is_string($%s = ', $right)) ->subcompile($this->getNode('right')) - ->raw(', -strlen(') - ->subcompile($this->getNode('right')) - ->raw(')))') + ->raw(sprintf(') && (\'\' === $%2$s || $%2$s === substr($%1$s, -strlen($%2$s))))', $left, $right)) ; } diff --git a/lib/Twig/Node/Expression/Binary/StartsWith.php b/lib/Twig/Node/Expression/Binary/StartsWith.php index b7c30b3c4..d2e30d66e 100644 --- a/lib/Twig/Node/Expression/Binary/StartsWith.php +++ b/lib/Twig/Node/Expression/Binary/StartsWith.php @@ -12,13 +12,14 @@ class Twig_Node_Expression_Binary_StartsWith extends Twig_Node_Expression_Binary { public function compile(Twig_Compiler $compiler) { - $varName = $compiler->getVarName(); + $left = $compiler->getVarName(); + $right = $compiler->getVarName(); $compiler - ->raw(sprintf("(('' === (\$%s = ", $varName)) - ->subcompile($this->getNode('right')) - ->raw(')) ? true : 0 === strpos(') + ->raw(sprintf('(is_string($%s = ', $left)) ->subcompile($this->getNode('left')) - ->raw(sprintf(', $%s))', $varName)) + ->raw(sprintf(') && is_string($%s = ', $right)) + ->subcompile($this->getNode('right')) + ->raw(sprintf(') && (\'\' === $%2$s || 0 === strpos($%1$s, $%2$s)))', $left, $right)) ; } diff --git a/test/Twig/Tests/Fixtures/expressions/ends_with.test b/test/Twig/Tests/Fixtures/expressions/ends_with.test index d259d1189..9ad5e5e8e 100644 --- a/test/Twig/Tests/Fixtures/expressions/ends_with.test +++ b/test/Twig/Tests/Fixtures/expressions/ends_with.test @@ -4,9 +4,23 @@ Twig supports the "ends with" operator {{ 'foo' ends with 'o' ? 'OK' : 'KO' }} {{ not ('foo' ends with 'f') ? 'OK' : 'KO' }} {{ not ('foo' ends with 'foowaytoolong') ? 'OK' : 'KO' }} +{{ 'foo' ends with '' ? 'OK' : 'KO' }} +{{ '1' ends with true ? 'OK' : 'KO' }} +{{ 1 ends with true ? 'OK' : 'KO' }} +{{ 0 ends with false ? 'OK' : 'KO' }} +{{ '' ends with false ? 'OK' : 'KO' }} +{{ false ends with false ? 'OK' : 'KO' }} +{{ false ends with '' ? 'OK' : 'KO' }} --DATA-- return array() --EXPECT-- OK OK OK +OK +KO +KO +KO +KO +KO +KO diff --git a/test/Twig/Tests/Fixtures/expressions/starts_with.test b/test/Twig/Tests/Fixtures/expressions/starts_with.test index b960c8b5e..75d331e90 100644 --- a/test/Twig/Tests/Fixtures/expressions/starts_with.test +++ b/test/Twig/Tests/Fixtures/expressions/starts_with.test @@ -11,6 +11,7 @@ with 'f' ? 'OK' : 'KO' }} {{ '1' starts with true ? 'OK' : 'KO' }} {{ '' starts with false ? 'OK' : 'KO' }} {{ 'a' starts with false ? 'OK' : 'KO' }} +{{ false starts with '' ? 'OK' : 'KO' }} --DATA-- return array() --EXPECT-- @@ -23,3 +24,4 @@ OK KO KO KO +KO