fix precedence of simple-math-operators before modifiers.

thanks dominik!
This commit is contained in:
messju
2003-10-11 09:32:24 +00:00
parent f8db1a7ce8
commit bd0440b546

View File

@@ -124,7 +124,7 @@ class Smarty_Compiler extends Smarty {
// $foo[5][blah] // $foo[5][blah]
// $foo[5].bar[$foobar][4] // $foo[5].bar[$foobar][4]
$this->_dvar_math_regexp = '[\+\-\*\/\%]'; $this->_dvar_math_regexp = '[\+\-\*\/\%]';
$this->_dvar_math_var_regexp = '[\$\w\.\+\-\*\/\%\d\|\>\[\]]'; $this->_dvar_math_var_regexp = '[\$\w\.\+\-\*\/\%\d\>\[\]]';
$this->_dvar_num_var_regexp = '\-?\d+(?:\.\d+)?' . $this->_dvar_math_var_regexp; $this->_dvar_num_var_regexp = '\-?\d+(?:\.\d+)?' . $this->_dvar_math_var_regexp;
$this->_dvar_guts_regexp = '\w+(?:' . $this->_var_bracket_regexp $this->_dvar_guts_regexp = '\w+(?:' . $this->_var_bracket_regexp
. ')*(?:\.\$?\w+(?:' . $this->_var_bracket_regexp . ')*)*(?:' . $this->_dvar_math_regexp . '(?:\-?\d+(?:\.\d+)?|' . $this->_dvar_math_var_regexp . '*))?'; . ')*(?:\.\$?\w+(?:' . $this->_var_bracket_regexp . ')*)*(?:' . $this->_dvar_math_regexp . '(?:\-?\d+(?:\.\d+)?|' . $this->_dvar_math_var_regexp . '*))?';
@@ -1580,37 +1580,43 @@ class Smarty_Compiler extends Smarty {
* parse variable expression into PHP code or static value * parse variable expression into PHP code or static value
* *
* @param string $var_expr * @param string $var_expr
* @param string $output
* @return string * @return string
*/ */
function _parse_var($var_expr) function _parse_var($var_expr, $in_math = false)
{ {
// inform the calling expression the return type (php, static) // inform the calling expression the return type (php, static)
$this->_output_type = 'php'; $this->_output_type = 'php';
$_has_math = false;
$_math_vars = preg_split('!('.$this->_dvar_math_regexp.'|'.$this->_qstr_regexp.')!', $var_expr, -1, PREG_SPLIT_DELIM_CAPTURE); $_math_vars = preg_split('!('.$this->_dvar_math_regexp.'|'.$this->_qstr_regexp.')!', $var_expr, -1, PREG_SPLIT_DELIM_CAPTURE);
if(count($_math_vars) > 1) if(count($_math_vars) > 1)
{ {
$_output = ""; $_first_var = "";
$_complete_var = ""; $_complete_var = "";
// simple check if there is any math, to stop recursion (due to modifiers with "xx % yy" as parameter) // simple check if there is any math, to stop recursion (due to modifiers with "xx % yy" as parameter)
$_has_math = false;
foreach($_math_vars as $_k => $_math_var) foreach($_math_vars as $_k => $_math_var)
{ {
$_math_var = $_math_vars[$_k]; $_math_var = $_math_vars[$_k];
if(!empty($_math_var))
if(!empty($_math_var) || is_numeric($_math_var))
{ {
// hit a math operator, so process the stuff which came before it // hit a math operator, so process the stuff which came before it
if(preg_match('!^' . $this->_dvar_math_regexp . '$!', $_math_var)) if(preg_match('!^' . $this->_dvar_math_regexp . '$!', $_math_var))
{ {
$_has_math = true; $_has_math = true;
if(!empty($_complete_var)) if(!empty($_complete_var) || is_numeric($_complete_var))
{ {
$_output .= $this->_parse_var($_complete_var); $_output .= $this->_parse_var($_complete_var, true);
} }
// just output the math operator to php // just output the math operator to php
$_output .= $_math_var; $_output .= $_math_var;
if(empty($_first_var))
$_first_var = $_complete_var;
$_complete_var = ""; $_complete_var = "";
} }
else else
@@ -1633,14 +1639,14 @@ class Smarty_Compiler extends Smarty {
} }
if($_has_math) if($_has_math)
{ {
if(!empty($_complete_var)) if(!empty($_complete_var) || is_numeric($_complete_var))
$_output .= $this->_parse_var($_complete_var); $_output .= $this->_parse_var($_complete_var, true);
return $_output; // get the modifiers working (only the last var from math + modifier is left)
$var_expr = $_complete_var;
} }
} }
preg_match('!(' . $this->_dvar_num_var_regexp . '*|' . $this->_obj_call_regexp . '|' . $this->_var_regexp . ')(' . $this->_mod_regexp . '*)$!', $var_expr, $match); preg_match('!(' . $this->_dvar_num_var_regexp . '*|' . $this->_obj_call_regexp . '|' . $this->_var_regexp . ')(' . $this->_mod_regexp . '*)$!', $var_expr, $match);
// prevent cutting of first digit in the number (we _definitly_ got a number if the first char is a digit) // prevent cutting of first digit in the number (we _definitly_ got a number if the first char is a digit)
@@ -1657,6 +1663,8 @@ class Smarty_Compiler extends Smarty {
$modifiers = empty($modifiers) ? $_default_mod_string : $_default_mod_string . '|' . $modifiers; $modifiers = empty($modifiers) ? $_default_mod_string : $_default_mod_string . '|' . $modifiers;
} }
if(!$_has_math)
{
// get [foo] and .foo and ->foo and (...) pieces // get [foo] and .foo and ->foo and (...) pieces
preg_match_all('!(?:^\w+)|' . $this->_obj_params_regexp . '|(?:' . $this->_var_bracket_regexp . ')|->\$?\w+|\.\$?\w+|\S+!', $_var_ref, $match); preg_match_all('!(?:^\w+)|' . $this->_obj_params_regexp . '|(?:' . $this->_var_bracket_regexp . ')|->\$?\w+|\.\$?\w+|\S+!', $_var_ref, $match);
@@ -1727,8 +1735,13 @@ class Smarty_Compiler extends Smarty {
$_output .= $_index; $_output .= $_index;
} }
} }
}
// If called recursive (because of math var splitting) don't do modifiers
if(!$in_math)
{
$this->_parse_modifiers($_output, $modifiers); $this->_parse_modifiers($_output, $modifiers);
}
return $_output; return $_output;
} }