Skip to content

Commit ef0ebb8

Browse files
committed
[Finder] Fixed gnu find regex name filtering problems.
1 parent 5fc01f2 commit ef0ebb8

File tree

4 files changed

+83
-12
lines changed

4 files changed

+83
-12
lines changed

src/Symfony/Component/Finder/Adapter/GnuFindAdapter.php

Lines changed: 9 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -152,13 +152,17 @@ private function buildNamesFiltering(Command $command, array $names, $not = fals
152152
foreach ($names as $i => $name) {
153153
$expr = Expression::create($name);
154154

155-
// Fixes 'not search' regex problem.
155+
// Fixes 'not search' and 'fuls path matching' regex problems.
156+
// - Jokers '.' are replaced by [^/].
157+
// - We add '[^/]*' before and after regex (if no ^|$ flags are present).
156158
if ($expr->isRegex()) {
157159
$regex = $expr->getRegex();
158-
$regex->prepend($regex->hasStartFlag() ? '/' : '/.*')->setStartFlag(false)->setStartJoker(true);
159-
160-
if (!$regex->hasEndFlag()) {
161-
$regex->setEndJoker(true);
160+
$regex->prepend($regex->hasStartFlag() ? '/' : '/[^/]*')
161+
->setStartFlag(false)
162+
->setStartJoker(true)
163+
->replaceJokers('[^/]');
164+
if (!$regex->hasEndFlag() || $regex->hasEndJoker()) {
165+
$regex->setEndJoker(false)->append('[^/]*');
162166
}
163167
}
164168

src/Symfony/Component/Finder/Expression/Regex.php

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -270,6 +270,25 @@ public function append($expr)
270270
return $this;
271271
}
272272

273+
/**
274+
* @param array $replacements
275+
*
276+
* @return Regex
277+
*/
278+
public function replaceJokers($replacement)
279+
{
280+
$replace = function ($subject) use ($replacement) {
281+
$subject = $subject[0];
282+
$replace = 0 === substr_count($subject, '\\') % 2;
283+
284+
return $replace ? str_replace('.', $replacement, $subject) : $subject;
285+
};
286+
287+
$this->pattern = preg_replace_callback('~[\\\\]*\\.~', $replace, $this->pattern);
288+
289+
return $this;
290+
}
291+
273292
/**
274293
* @param string $pattern
275294
*/

src/Symfony/Component/Finder/Tests/Expression/RegexTest.php

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -78,6 +78,17 @@ public function testMixFlagsAndJokers()
7878
$this->assertEquals('~^.*abc.*$~is', $expr->render());
7979
}
8080

81+
/**
82+
* @dataProvider getReplaceJokersTestData
83+
*/
84+
public function testReplaceJokers($regex, $expected)
85+
{
86+
$expr = new Expression($regex);
87+
$expr = $expr->getRegex()->replaceJokers('@');
88+
89+
$this->assertEquals($expected, $expr->renderPattern());
90+
}
91+
8192
public function getHasFlagsData()
8293
{
8394
return array(
@@ -119,4 +130,14 @@ public function getSetJokersData()
119130
array('~abc~', true, true, '~.*abc.*~'),
120131
);
121132
}
133+
134+
public function getReplaceJokersTestData()
135+
{
136+
return array(
137+
array('~.abc~', '@abc'),
138+
array('~\\.abc~', '\\.abc'),
139+
array('~\\\\.abc~', '\\\\@abc'),
140+
array('~\\\\\\.abc~', '\\\\\\.abc'),
141+
);
142+
}
122143
}

src/Symfony/Component/Finder/Tests/FinderTest.php

Lines changed: 34 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -133,6 +133,18 @@ public function testNotName($adapter)
133133
$this->assertIterator(array(), $finder->in(self::$tmpDir)->getIterator());
134134
}
135135

136+
/**
137+
* @dataProvider getRegexNameTestData
138+
*
139+
* @group regexName
140+
*/
141+
public function testRegexName($adapter, $regex)
142+
{
143+
$finder = $this->buildFinder($adapter);
144+
$finder->name($regex);
145+
$this->assertIterator($this->toAbsolute(array('test.py', 'test.php')), $finder->in(self::$tmpDir)->getIterator());
146+
}
147+
136148
/**
137149
* @dataProvider getAdaptersTestData
138150
*/
@@ -595,14 +607,17 @@ public function getContainsTestData()
595607
array('ipsum dolor sit amet', '/^IPSUM/m', array('lorem.txt')),
596608
);
597609

598-
$data = array();
599-
foreach ($this->getValidAdapters() as $adapter) {
600-
foreach ($tests as $test) {
601-
$data[] = array_merge(array($adapter), $test);
602-
}
603-
}
610+
return $this->buildTestData($tests);
611+
}
604612

605-
return $data;
613+
public function getRegexNameTestData()
614+
{
615+
$tests = array(
616+
array('~.+\\.p.+~i'),
617+
array('~t.*s~i'),
618+
);
619+
620+
return $this->buildTestData($tests);
606621
}
607622

608623
private function buildFinder(Adapter\AdapterInterface $adapter)
@@ -619,4 +634,16 @@ private function getValidAdapters()
619634
function (Adapter\AdapterInterface $adapter) { return $adapter->isSupported(); }
620635
);
621636
}
637+
638+
private function buildTestData(array $tests)
639+
{
640+
$data = array();
641+
foreach ($this->getValidAdapters() as $adapter) {
642+
foreach ($tests as $test) {
643+
$data[] = array_merge(array($adapter), $test);
644+
}
645+
}
646+
647+
return $data;
648+
}
622649
}

0 commit comments

Comments
 (0)
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