[ Index ]

PHP Cross Reference of phpBB-3.3.14-deutsch

title

Body

[close]

/vendor/s9e/text-formatter/src/Configurator/TemplateNormalizations/ -> FoldArithmeticConstants.php (source)

   1  <?php
   2  
   3  /**
   4  * @package   s9e\TextFormatter
   5  * @copyright Copyright (c) 2010-2022 The s9e authors
   6  * @license   http://www.opensource.org/licenses/mit-license.php The MIT License
   7  */
   8  namespace s9e\TextFormatter\Configurator\TemplateNormalizations;
   9  
  10  use s9e\TextFormatter\Configurator\Helpers\XPathHelper;
  11  use s9e\TextFormatter\Utils\XPath;
  12  
  13  class FoldArithmeticConstants extends AbstractConstantFolding
  14  {
  15      /**
  16      * {@inheritdoc}
  17      */
  18  	protected function getOptimizationPasses()
  19      {
  20          // Regular expression matching a number
  21          $n = '-?\\.[0-9]++|-?[0-9]++(?:\\.[0-9]++)?';
  22  
  23          return [
  24              '(^[0-9\\s]*[-+][-+0-9\\s]+$)'                               => 'foldOperation',
  25              '( \\+ 0(?= $| [-+\\)])|(?<![^\\(])0 \\+ )'                  => 'foldAdditiveIdentity',
  26              '(^((?>' . $n . ' [-+] )*)(' . $n . ') div (' . $n . '))'    => 'foldDivision',
  27              '(^((?>' . $n . ' [-+] )*)(' . $n . ') \\* (' . $n . '))'    => 'foldMultiplication',
  28              '(\\( (?:' . $n . ') (?>(?>[-+*]|div) (?:' . $n . ') )+\\))' => 'foldSubExpression',
  29              '((?<=[-+*\\(]|\\bdiv|^) \\( ([@$][-\\w]+|' . $n . ') \\) (?=[-+*\\)]|div|$))' => 'removeParentheses'
  30          ];
  31      }
  32  
  33      /**
  34      * {@inheritdoc}
  35      */
  36  	protected function evaluateExpression($expr)
  37      {
  38          $expr = XPathHelper::encodeStrings($expr);
  39          $expr = parent::evaluateExpression($expr);
  40          $expr = XPathHelper::decodeStrings($expr);
  41  
  42          return $expr;
  43      }
  44  
  45      /**
  46      * Remove "+ 0" additions
  47      *
  48      * @param  array  $m
  49      * @return string
  50      */
  51  	protected function foldAdditiveIdentity(array $m)
  52      {
  53          return '';
  54      }
  55  
  56      /**
  57      * Evaluate and return the result of a division
  58      *
  59      * @param  array  $m
  60      * @return string
  61      */
  62  	protected function foldDivision(array $m)
  63      {
  64          return $m[1] . XPath::export($m[2] / $m[3]);
  65      }
  66  
  67      /**
  68      * Evaluate and return the result of a multiplication
  69      *
  70      * @param  array  $m
  71      * @return string
  72      */
  73  	protected function foldMultiplication(array $m)
  74      {
  75          return $m[1] . XPath::export($m[2] * $m[3]);
  76      }
  77  
  78      /**
  79      * Evaluate and replace a constant operation
  80      *
  81      * @param  array  $m
  82      * @return string
  83      */
  84  	protected function foldOperation(array $m)
  85      {
  86          return XPath::export($this->xpath->evaluate($m[0]));
  87      }
  88  
  89      /**
  90      * Evaluate and return the result of a simple subexpression
  91      *
  92      * @param  array  $m
  93      * @return string
  94      */
  95  	protected function foldSubExpression(array $m)
  96      {
  97          return '(' . $this->evaluateExpression(trim(substr($m[0], 1, -1))) . ')';
  98      }
  99  
 100      /**
 101      * Remove the parentheses around an integer
 102      *
 103      * @param  array  $m
 104      * @return string
 105      */
 106  	protected function removeParentheses(array $m)
 107      {
 108          return ' ' . $m[1] . ' ';
 109      }
 110  }


Generated: Mon Nov 25 19:05:08 2024 Cross-referenced by PHPXref 0.7.1