From 2c93400e77d4bc972ed20c23e57421a76207d718 Mon Sep 17 00:00:00 2001 From: Chris Kankiewicz Date: Mon, 27 Mar 2023 09:33:13 -0700 Subject: [PATCH] Improved character groups to support backslashes --- src/Pattern.php | 12 ++++++------ tests/GlobTest.php | 8 ++++---- tests/PatternTest.php | 7 +------ 3 files changed, 11 insertions(+), 16 deletions(-) diff --git a/src/Pattern.php b/src/Pattern.php index 25afffa..032fa7b 100644 --- a/src/Pattern.php +++ b/src/Pattern.php @@ -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; diff --git a/tests/GlobTest.php b/tests/GlobTest.php index 65bff8b..ff63e1d 100644 --- a/tests/GlobTest.php +++ b/tests/GlobTest.php @@ -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 diff --git a/tests/PatternTest.php b/tests/PatternTest.php index c5c2e73..4fa31d2 100644 --- a/tests/PatternTest.php +++ b/tests/PatternTest.php @@ -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()); @@ -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());