Skip to main content
Drupal API
User account menu
  • Log in

Breadcrumb

  1. Drupal Core 11.1.x
  2. Lexer.php

function Lexer::getOperatorRegex

1 call to Lexer::getOperatorRegex()
Lexer::initialize in vendor/twig/twig/src/Lexer.php

File

vendor/twig/twig/src/Lexer.php, line 541

Class

Lexer
@author Fabien Potencier <fabien@symfony.com>

Namespace

Twig

Code

private function getOperatorRegex() : string {
    $operators = array_merge([
        '=',
    ], array_keys($this->env
        ->getUnaryOperators()), array_keys($this->env
        ->getBinaryOperators()));
    $operators = array_combine($operators, array_map('strlen', $operators));
    arsort($operators);
    $regex = [];
    foreach ($operators as $operator => $length) {
        // an operator that ends with a character must be followed by
        // a whitespace, a parenthesis, an opening map [ or sequence {
        $r = preg_quote($operator, '/');
        if (ctype_alpha($operator[$length - 1])) {
            $r .= '(?=[\\s()\\[{])';
        }
        // an operator that begins with a character must not have a dot or pipe before
        if (ctype_alpha($operator[0])) {
            $r = '(?<![\\.\\|])' . $r;
        }
        // an operator with a space can be any amount of whitespaces
        $r = preg_replace('/\\s+/', '\\s+', $r);
        $regex[] = $r;
    }
    return '/' . implode('|', $regex) . '/A';
}
RSS feed
Powered by Drupal