Skip to content

Commit 2c93400

Browse files
committed
Improved character groups to support backslashes
1 parent 29c72b7 commit 2c93400

File tree

3 files changed

+11
-16
lines changed

3 files changed

+11
-16
lines changed

src/Pattern.php

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -73,16 +73,16 @@ public function toRegex(int $options = self::BOTH_ANCHORS): string
7373

7474
switch ($char) {
7575
case '\\':
76-
$pattern .= '\\' . $this->pattern[++$i];
76+
if ($characterGroup) {
77+
$pattern .= '\\\\';
78+
} else {
79+
$pattern .= '\\' . $this->pattern[++$i];
80+
}
7781

7882
break;
7983

8084
case '?':
81-
if ($characterGroup) {
82-
$pattern .= $char;
83-
} else {
84-
$pattern .= '.';
85-
}
85+
$pattern .= $characterGroup ? $char : '.';
8686

8787
break;
8888

tests/GlobTest.php

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -96,10 +96,10 @@ public function test_it_matches_a_single_character_in_a_range(): void
9696

9797
public function test_it_matches_glob_wildcards_literally_in_character_classes(): void
9898
{
99-
$this->assertTrue(Glob::match('[*?**]', '*'));
100-
$this->assertTrue(Glob::match('[*?**]', '?'));
101-
$this->assertFalse(Glob::match('[*?**]', '.'));
102-
$this->assertFalse(Glob::match('[*?**]', 'x'));
99+
$this->assertTrue(Glob::match('[[?*\]', '?'));
100+
$this->assertTrue(Glob::match('[[?*\]', '*'));
101+
$this->assertTrue(Glob::match('[[?*\]', '\\'));
102+
$this->assertFalse(Glob::match('[[?*\]', 'x'));
103103
}
104104

105105
public function test_it_matches_any_character_not_in_a_set(): void

tests/PatternTest.php

Lines changed: 1 addition & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -42,12 +42,6 @@ public function test_it_can_escape_glob_patterns_when_converting_to_regular_expr
4242
$this->assertEquals('#^\\#$#', Pattern::make('\#')->toRegex());
4343
}
4444

45-
public function test_it_does_not_replace_glob_wildcards_in_character_classes(): void
46-
{
47-
$this->assertEquals('#^[\*\?\*\*]$#', Pattern::make('[\*\?\*\*]')->toRegex());
48-
$this->assertEquals('#^[*?**]$#', Pattern::make('[*?**]')->toRegex());
49-
}
50-
5145
public function test_it_can_convert_a_complex_glob_pattern_to_a_regular_expressions(): void
5246
{
5347
$this->assertEquals('#^foo\.txt$#', Pattern::make('foo.txt')->toRegex());
@@ -59,6 +53,7 @@ public function test_it_can_convert_a_complex_glob_pattern_to_a_regular_expressi
5953
$this->assertEquals('#^file\.(yml|yaml)$#', Pattern::make('file.{yml,yaml}')->toRegex());
6054
$this->assertEquals('#^[fbw]oo\.txt$#', Pattern::make('[fbw]oo.txt')->toRegex());
6155
$this->assertEquals('#^[^fbw]oo\.txt$#', Pattern::make('[^fbw]oo.txt')->toRegex());
56+
$this->assertEquals('#^[[?*\\\\]$#', Pattern::make('[[?*\]')->toRegex());
6257
$this->assertEquals('#^foo}bar\.txt$#', Pattern::make('foo}bar.txt')->toRegex());
6358
$this->assertEquals('#^foo\^bar\.txt$#', Pattern::make('foo^bar.txt')->toRegex());
6459
$this->assertEquals('#^foo,bar\.txt$#', Pattern::make('foo,bar.txt')->toRegex());

0 commit comments

Comments
 (0)