You are here

public static function Glob::toRegex in Database Sanitize 7

Returns a regexp which is the equivalent of the glob pattern.

Parameters

string $glob The glob pattern:

bool $strictLeadingDot:

bool $strictWildcardSlash:

string $delimiter Optional delimiter:

Return value

string regex The regexp

6 calls to Glob::toRegex()
FilenameFilterIterator::toRegex in vendor/symfony/finder/Iterator/FilenameFilterIterator.php
Converts glob to regexp.
GlobTest::testGlobToRegexDelimiters in vendor/symfony/finder/Tests/GlobTest.php
GlobTest::testGlobToRegexDoubleStarNonStrictDots in vendor/symfony/finder/Tests/GlobTest.php
GlobTest::testGlobToRegexDoubleStarStrictDots in vendor/symfony/finder/Tests/GlobTest.php
GlobTest::testGlobToRegexDoubleStarWithoutLeadingSlash in vendor/symfony/finder/Tests/GlobTest.php

... See full list

File

vendor/symfony/finder/Glob.php, line 48

Class

Glob
Glob matches globbing patterns against text.

Namespace

Symfony\Component\Finder

Code

public static function toRegex($glob, $strictLeadingDot = true, $strictWildcardSlash = true, $delimiter = '#') {
  $firstByte = true;
  $escaping = false;
  $inCurlies = 0;
  $regex = '';
  $sizeGlob = \strlen($glob);
  for ($i = 0; $i < $sizeGlob; ++$i) {
    $car = $glob[$i];
    if ($firstByte && $strictLeadingDot && '.' !== $car) {
      $regex .= '(?=[^\\.])';
    }
    $firstByte = '/' === $car;
    if ($firstByte && $strictWildcardSlash && isset($glob[$i + 2]) && '**' === $glob[$i + 1] . $glob[$i + 2] && (!isset($glob[$i + 3]) || '/' === $glob[$i + 3])) {
      $car = '[^/]++/';
      if (!isset($glob[$i + 3])) {
        $car .= '?';
      }
      if ($strictLeadingDot) {
        $car = '(?=[^\\.])' . $car;
      }
      $car = '/(?:' . $car . ')*';
      $i += 2 + isset($glob[$i + 3]);
      if ('/' === $delimiter) {
        $car = str_replace('/', '\\/', $car);
      }
    }
    if ($delimiter === $car || '.' === $car || '(' === $car || ')' === $car || '|' === $car || '+' === $car || '^' === $car || '$' === $car) {
      $regex .= "\\{$car}";
    }
    elseif ('*' === $car) {
      $regex .= $escaping ? '\\*' : ($strictWildcardSlash ? '[^/]*' : '.*');
    }
    elseif ('?' === $car) {
      $regex .= $escaping ? '\\?' : ($strictWildcardSlash ? '[^/]' : '.');
    }
    elseif ('{' === $car) {
      $regex .= $escaping ? '\\{' : '(';
      if (!$escaping) {
        ++$inCurlies;
      }
    }
    elseif ('}' === $car && $inCurlies) {
      $regex .= $escaping ? '}' : ')';
      if (!$escaping) {
        --$inCurlies;
      }
    }
    elseif (',' === $car && $inCurlies) {
      $regex .= $escaping ? ',' : '|';
    }
    elseif ('\\' === $car) {
      if ($escaping) {
        $regex .= '\\\\';
        $escaping = false;
      }
      else {
        $escaping = true;
      }
      continue;
    }
    else {
      $regex .= $car;
    }
    $escaping = false;
  }
  return $delimiter . '^' . $regex . '$' . $delimiter;
}