Skip to content

Commit

Permalink
Improved character groups to support backslashes
Browse files Browse the repository at this point in the history
  • Loading branch information
PHLAK committed Mar 27, 2023
1 parent 29c72b7 commit 2c93400
Show file tree
Hide file tree
Showing 3 changed files with 11 additions and 16 deletions.
12 changes: 6 additions & 6 deletions src/Pattern.php
Original file line number Diff line number Diff line change
Expand Up @@ -73,16 +73,16 @@ public function toRegex(int $options = self::BOTH_ANCHORS): string

switch ($char) {
case '\\':
$pattern .= '\\' . $this->pattern[++$i];
if ($characterGroup) {
$pattern .= '\\\\';
} else {
$pattern .= '\\' . $this->pattern[++$i];
}

break;

case '?':
if ($characterGroup) {
$pattern .= $char;
} else {
$pattern .= '.';
}
$pattern .= $characterGroup ? $char : '.';

break;

Expand Down
8 changes: 4 additions & 4 deletions tests/GlobTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -96,10 +96,10 @@ public function test_it_matches_a_single_character_in_a_range(): void

public function test_it_matches_glob_wildcards_literally_in_character_classes(): void
{
$this->assertTrue(Glob::match('[*?**]', '*'));
$this->assertTrue(Glob::match('[*?**]', '?'));
$this->assertFalse(Glob::match('[*?**]', '.'));
$this->assertFalse(Glob::match('[*?**]', 'x'));
$this->assertTrue(Glob::match('[[?*\]', '?'));
$this->assertTrue(Glob::match('[[?*\]', '*'));
$this->assertTrue(Glob::match('[[?*\]', '\\'));
$this->assertFalse(Glob::match('[[?*\]', 'x'));
}

public function test_it_matches_any_character_not_in_a_set(): void
Expand Down
7 changes: 1 addition & 6 deletions tests/PatternTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -42,12 +42,6 @@ public function test_it_can_escape_glob_patterns_when_converting_to_regular_expr
$this->assertEquals('#^\\#$#', Pattern::make('\#')->toRegex());
}
public function test_it_does_not_replace_glob_wildcards_in_character_classes(): void
{
$this->assertEquals('#^[\*\?\*\*]$#', Pattern::make('[\*\?\*\*]')->toRegex());
$this->assertEquals('#^[*?**]$#', Pattern::make('[*?**]')->toRegex());
}

public function test_it_can_convert_a_complex_glob_pattern_to_a_regular_expressions(): void
{
$this->assertEquals('#^foo\.txt$#', Pattern::make('foo.txt')->toRegex());
Expand All @@ -59,6 +53,7 @@ public function test_it_can_convert_a_complex_glob_pattern_to_a_regular_expressi
$this->assertEquals('#^file\.(yml|yaml)$#', Pattern::make('file.{yml,yaml}')->toRegex());
$this->assertEquals('#^[fbw]oo\.txt$#', Pattern::make('[fbw]oo.txt')->toRegex());
$this->assertEquals('#^[^fbw]oo\.txt$#', Pattern::make('[^fbw]oo.txt')->toRegex());
$this->assertEquals('#^[[?*\\\\]$#', Pattern::make('[[?*\]')->toRegex());
$this->assertEquals('#^foo}bar\.txt$#', Pattern::make('foo}bar.txt')->toRegex());
$this->assertEquals('#^foo\^bar\.txt$#', Pattern::make('foo^bar.txt')->toRegex());
$this->assertEquals('#^foo,bar\.txt$#', Pattern::make('foo,bar.txt')->toRegex());
Expand Down

0 comments on commit 2c93400

Please sign in to comment.