Skip to content

[Finder] Add double-star matching to Glob::toRegex() #21572

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 1 commit into from
Feb 9, 2017
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 5 additions & 0 deletions src/Symfony/Component/Finder/CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,6 +1,11 @@
CHANGELOG
=========

3.3.0
-----

* added double-star matching to Glob::toRegex()

3.0.0
-----

Expand Down
18 changes: 10 additions & 8 deletions src/Symfony/Component/Finder/Glob.php
Original file line number Diff line number Diff line change
Expand Up @@ -54,16 +54,18 @@ public static function toRegex($glob, $strictLeadingDot = true, $strictWildcardS
$sizeGlob = strlen($glob);
for ($i = 0; $i < $sizeGlob; ++$i) {
$car = $glob[$i];
if ($firstByte) {
if ($strictLeadingDot && '.' !== $car) {
$regex .= '(?=[^\.])';
}

$firstByte = false;
if ($firstByte && $strictLeadingDot && '.' !== $car) {
$regex .= '(?=[^\.])';
}

if ('/' === $car) {
$firstByte = true;
$firstByte = '/' === $car;

if ($firstByte && $strictWildcardSlash && isset($glob[$i + 3]) && '**/' === $glob[$i + 1].$glob[$i + 2].$glob[$i + 3]) {
$car = $strictLeadingDot ? '/((?=[^\.])[^/]+/)*' : '/([^/]+/)*';
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I would use a possessive quantifier for [^/]++/ to avoid useless backtracking.

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

done in #21289

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

shoudln't this use non-capturing groups ?

$i += 3;
if ('/' === $delimiter) {
$car = str_replace('/', '\\/', $car);
}
}

if ($delimiter === $car || '.' === $car || '(' === $car || ')' === $car || '|' === $car || '+' === $car || '^' === $car || '$' === $car) {
Expand Down
Empty file.
Empty file.
Empty file.
35 changes: 35 additions & 0 deletions src/Symfony/Component/Finder/Tests/GlobTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@

namespace Symfony\Component\Finder\Tests;

use Symfony\Component\Finder\Finder;
use Symfony\Component\Finder\Glob;

class GlobTest extends \PHPUnit_Framework_TestCase
Expand All @@ -22,4 +23,38 @@ public function testGlobToRegexDelimiters()
$this->assertEquals('^\.[^/]*$', Glob::toRegex('.*', true, true, ''));
$this->assertEquals('/^\.[^/]*$/', Glob::toRegex('.*', true, true, '/'));
}

public function testGlobToRegexDoubleStarStrictDots()
{
$finder = new Finder();
$finder->ignoreDotFiles(false);
$regex = Glob::toRegex('/**/*.neon');

foreach ($finder->in(__DIR__) as $k => $v) {
$k = str_replace(DIRECTORY_SEPARATOR, '/', $k);
if (preg_match($regex, substr($k, strlen(__DIR__)))) {
$match[] = substr($k, 10 + strlen(__DIR__));
}
}
sort($match);

$this->assertSame(array('one/b/c.neon', 'one/b/d.neon'), $match);
}

public function testGlobToRegexDoubleStarNonStrictDots()
{
$finder = new Finder();
$finder->ignoreDotFiles(false);
$regex = Glob::toRegex('/**/*.neon', false);

foreach ($finder->in(__DIR__) as $k => $v) {
$k = str_replace(DIRECTORY_SEPARATOR, '/', $k);
if (preg_match($regex, substr($k, strlen(__DIR__)))) {
$match[] = substr($k, 10 + strlen(__DIR__));
}
}
sort($match);

$this->assertSame(array('.dot/b/c.neon', '.dot/b/d.neon', 'one/b/c.neon', 'one/b/d.neon'), $match);
}
}