Re: Simple math request

"Konstantin A. Pelepelin" <[email protected]>
Newsgroups gmane.comp.php.smarty.devel
Organization DTF
Message-ID <[email protected]>
On Fri, 16 Apr 2004 20:51:37 +0200, Messju Mohr <[email protected]> wrote:

...
>> > {$var-5} works, but {5-$var} or {-$var) doesn't.
>> > {assign var='var2' value=$var-5} works, but value=5-$var or value=-$var
>> > doesn't (value=-$var can accidentally work because of string
>> > interpretation).
>> >
>> > Is it possible to implement at least in parameters?
...
> I have this diff against Smarty_Compiler.class.php
Well, your diff doesn't work good for my test case.
Today I tried to make my own version of that kludged regexp :)

cvs diff Smarty_Compiler.class.php
Index: Smarty_Compiler.class.php
===================================================================
RCS file: /repository/smarty/libs/Smarty_Compiler.class.php,v
retrieving revision 1.320
diff -u -r1.320 Smarty_Compiler.class.php
--- Smarty_Compiler.class.php   16 Apr 2004 10:31:43 -0000      1.320
+++ Smarty_Compiler.class.php   19 Apr 2004 11:26:36 -0000
@@ -115,7 +115,8 @@
          $this->_dvar_math_var_regexp = '[\$\w\.\+\-\*\/\%\d\>\[\]]';
          $this->_dvar_guts_regexp = '\w+(?:' . $this->_var_bracket_regexp
                  . ')*(?:\.\$?\w+(?:' . $this->_var_bracket_regexp . ')*)*(?:' . $this->_dvar_math_regexp . '(?:' . $this->_num_const_regexp . '|' . $this->_dvar_math_var_regexp . ')*)?';
-        $this->_dvar_regexp = '\$' . $this->_dvar_guts_regexp;
+        $this->_dvar_regexp = '[-+]?(?:' . $this->_num_const_regexp . $this->_dvar_math_regexp
+                . ')*\$' . $this->_dvar_guts_regexp;

          // matches config vars:
          // #foo#


This covers more cases, but still need some work. At least it doesn't work in modifier parameters.
Here is my testcase output. Add { } around the second column.

101:    4                           4
102:    4+5*5                        doesn't compile
103:    5*5+4                        doesn't compile
104:    -0.5*1.5*4                   doesn't compile

111:    $var1                       3
112:    $var1+5*5                   28
113:    5*5+$var1                   28
114:    -0.5*1.5*$var1              -2.25

121:    $testarr1.ind1              1
122:    $testarr1.ind1+5*5          26
123:    5*5+$testarr1.ind1          26
124:    -0.5*1.5*$testarr1.ind1     -0.75

131:    $testarr1[2]                2
132:    $testarr1[2]+5*5            27
133:    5*5+$testarr1[2]            27
134:    -0.5*1.5*$testarr1[2]       -1.5

141:    $testobj1->prop1            12
142:    $testobj1->prop1+5*5        37
143:    5*5+$testobj1->prop1        37
144:    -0.5*1.5*$testobj1->prop1   -9


211:    ''|cat:$var1                       3
212:    ''|cat:$var1+5*5                   28
213:    ''|cat:5*5+$var1                    doesn't compile
214:    ''|cat:-0.5*1.5*$var1               doesn't compile

221:    ''|cat:$testarr1.ind1              1
222:    ''|cat:$testarr1.ind1+5*5          26
223:    ''|cat:5*5+$testarr1.ind1           doesn't compile
224:    ''|cat:-0.5*1.5*$testarr1.ind1      doesn't compile

231:    ''|cat:$testarr1[2]                2
232:    ''|cat:$testarr1[2]+5*5            27
233:    ''|cat:5*5+$testarr1[2]             doesn't compile
234:    ''|cat:-0.5*1.5*$testarr1[2]        doesn't compile

241:    ''|cat:$testobj1->prop1            12
242:    ''|cat:$testobj1->prop1+5*5        37
243:    ''|cat:5*5+$testobj1->prop1         doesn't compile
244:    ''|cat:-0.5*1.5*$testobj1->prop1    doesn't compile


311:    cycle values=$var1                       3
312:    cycle values=$var1+5*5                   28
313:    cycle values=5*5+$var1                   28
314:    cycle values=-0.5*1.5*$var1              -2.25

321:    cycle values=$testarr1.ind1              1
322:    cycle values=$testarr1.ind1+5*5          26
323:    cycle values=5*5+$testarr1.ind1          26
324:    cycle values=-0.5*1.5*$testarr1.ind1     -0.75

331:    cycle values=$testarr1[2]                2
332:    cycle values=$testarr1[2]+5*5            27
333:    cycle values=5*5+$testarr1[2]            27
334:    cycle values=-0.5*1.5*$testarr1[2]       -1.5

341:    cycle values=$testobj1->prop1            12
342:    cycle values=$testobj1->prop1+5*5        37
343:    cycle values=5*5+$testobj1->prop1        37
344:    cycle values=-0.5*1.5*$testobj1->prop1   -9

WBR,
Konstantin A. Pelepelin

-- 
Smarty Development Mailing List (http://smarty.php.net/)
To unsubscribe, visit: http://www.php.net/unsub.php
lmpx.com only provides a reader for public news (NNTP) servers. It is not affiliated with the servers or forums shown here and is not responsible for the content of articles, which is written by their respective authors.