From c59f36637c85c2b3ba4b252a56851733b8571029 Mon Sep 17 00:00:00 2001 From: Nicolas Grekas Date: Thu, 17 Sep 2015 14:13:49 +0200 Subject: [PATCH] [Finder] Merge regexps for performance --- .../Component/Finder/Expression/Regex.php | 2 +- .../Iterator/MultiplePcreFilterIterator.php | 36 ++++++++++++++----- .../MultiplePcreFilterIteratorTest.php | 2 +- 3 files changed, 29 insertions(+), 11 deletions(-) diff --git a/src/Symfony/Component/Finder/Expression/Regex.php b/src/Symfony/Component/Finder/Expression/Regex.php index a249fc2546dac..cd232f9b2bf82 100644 --- a/src/Symfony/Component/Finder/Expression/Regex.php +++ b/src/Symfony/Component/Finder/Expression/Regex.php @@ -61,7 +61,7 @@ class Regex implements ValueInterface */ public static function create($expr) { - if (preg_match('/^(.{3,}?)([imsxuADU]*)$/', $expr, $m)) { + if (preg_match('/^(.{2,}?)([imsxuADU]*)$/', $expr, $m)) { $start = substr($m[1], 0, 1); $end = substr($m[1], -1); diff --git a/src/Symfony/Component/Finder/Iterator/MultiplePcreFilterIterator.php b/src/Symfony/Component/Finder/Iterator/MultiplePcreFilterIterator.php index d21380d446f93..6ebcd479e0232 100644 --- a/src/Symfony/Component/Finder/Iterator/MultiplePcreFilterIterator.php +++ b/src/Symfony/Component/Finder/Iterator/MultiplePcreFilterIterator.php @@ -12,6 +12,7 @@ namespace Symfony\Component\Finder\Iterator; use Symfony\Component\Finder\Expression\Expression; +use Symfony\Component\Finder\Expression\Regex; /** * MultiplePcreFilterIterator filters files using patterns (regexps, globs or strings). @@ -32,15 +33,8 @@ abstract class MultiplePcreFilterIterator extends FilterIterator */ public function __construct(\Iterator $iterator, array $matchPatterns, array $noMatchPatterns) { - $this->matchRegexps = array(); - foreach ($matchPatterns as $pattern) { - $this->matchRegexps[] = $this->toRegex($pattern); - } - - $this->noMatchRegexps = array(); - foreach ($noMatchPatterns as $pattern) { - $this->noMatchRegexps[] = $this->toRegex($pattern); - } + $this->matchRegexps = $this->buildRegexps($matchPatterns); + $this->noMatchRegexps = $this->buildRegexps($noMatchPatterns); parent::__construct($iterator); } @@ -65,4 +59,28 @@ protected function isRegex($str) * @return string regexp corresponding to a given string */ abstract protected function toRegex($str); + + private function buildRegexps($patterns) + { + $rxs = array(); + $regexps = array(); + + foreach ($patterns as $pattern) { + $regex = Regex::create($this->toRegex($pattern))->render(); + + if (preg_match('/\W([imsxUXJ]*)$/', $regex, $match) && !preg_match('/\\\\[1-9]|\(\?P/', $regex)) { + $rxs[] = '(?'.$match[1].':'.substr($regex, 1, -strlen($match[0])).')'; + } else { + $regexps[] = $regex; + } + } + if (1 < count($rxs)) { + $regexps[] = Regex::BOUNDARY.implode('|', $rxs).Regex::BOUNDARY; + } elseif ($rxs) { + $rxs = explode(':', substr($rxs[0], 2, -1), 2); + $regexps[] = Regex::BOUNDARY.$rxs[1].Regex::BOUNDARY.$rxs[0]; + } + + return $regexps; + } } diff --git a/src/Symfony/Component/Finder/Tests/Iterator/MultiplePcreFilterIteratorTest.php b/src/Symfony/Component/Finder/Tests/Iterator/MultiplePcreFilterIteratorTest.php index 89d8edb0093b0..3b7a9f03b7315 100644 --- a/src/Symfony/Component/Finder/Tests/Iterator/MultiplePcreFilterIteratorTest.php +++ b/src/Symfony/Component/Finder/Tests/Iterator/MultiplePcreFilterIteratorTest.php @@ -31,7 +31,7 @@ public function getIsRegexFixtures() array(' foo ', false, '" " is not a valid delimiter'), array('\\foo\\', false, '"\\" is not a valid delimiter'), array('afooa', false, '"a" is not a valid delimiter'), - array('//', false, 'the pattern should contain at least 1 character'), + array('//', true, 'valid empty regex'), array('/a/', true, 'valid regex'), array('/foo/', true, 'valid regex'), array('/foo/i', true, 'valid regex with a single modifier'), pFad - Phonifier reborn

Pfad - The Proxy pFad of © 2024 Garber Painting. All rights reserved.

Note: This service is not intended for secure transactions such as banking, social media, email, or purchasing. Use at your own risk. We assume no liability whatsoever for broken pages.


Alternative Proxies:

Alternative Proxy

pFad Proxy

pFad v3 Proxy

pFad v4 Proxy