Skip to content

Commit 11d9a64

Browse files
committed
Replaced ^ with ! for character set/range negation
1 parent 809e969 commit 11d9a64

File tree

4 files changed

+39
-35
lines changed

4 files changed

+39
-35
lines changed

README.md

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -57,8 +57,8 @@ expressions.
5757
- `**` matches zero or more characters including `/` (`\` on Windows)
5858
- `[abc]` matches a single character from the set (i.e. `a`, `b` or `c`)
5959
- `[a-c]` matches a single character in the range (i.e. `a`, `b` or `c`)
60-
- `[^abc]` matches any character not in the set (i.e. not `a`, `b` or `c`)
61-
- `[^a-c]` matches any character not in the range (i.e. not `a`, `b` or `c`)
60+
- `[!abc]` matches any character not in the set (i.e. not `a`, `b` or `c`)
61+
- `[!a-c]` matches any character not in the range (i.e. not `a`, `b` or `c`)
6262
- `{foo,bar,baz}` matches any pattern in the set (i.e. `foo`, `bar` or `baz`)
6363
- Sets may contain other matching patterns (i.e. `{foo,ba[rz]}`)
6464

src/Pattern.php

Lines changed: 9 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -49,9 +49,9 @@ public static function directorySeparator(string $separator): void
4949
public static function escape(string $string): string
5050
{
5151
return str_replace([
52-
'\\', '?', '*', '(', ')', '[', ']', '^', '{', '}', ',',
52+
'\\', '?', '*', '(', ')', '[', ']', '!', '{', '}', ',',
5353
], [
54-
'\\\\', '\\?', '\\*', '\\(', '\\)', '\\[', '\\]', '\\^', '\\{', '\\}', '\\,',
54+
'\\\\', '\\?', '\\*', '\\(', '\\)', '\\[', '\\]', '\\!', '\\{', '\\}', '\\,',
5555
], $string);
5656
}
5757

@@ -102,6 +102,11 @@ public function toRegex(int $options = self::BOTH_ANCHORS): string
102102

103103
break;
104104

105+
case '^':
106+
$pattern .= '\^';
107+
108+
break;
109+
105110
case '[':
106111
$pattern .= $char;
107112
$characterGroup = true;
@@ -117,8 +122,8 @@ public function toRegex(int $options = self::BOTH_ANCHORS): string
117122

118123
break;
119124

120-
case '^':
121-
$pattern .= $characterGroup ? $char : '\\' . $char;
125+
case '!':
126+
$pattern .= $characterGroup ? '^' : $char;
122127

123128
break;
124129

tests/GlobTest.php

Lines changed: 24 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -114,41 +114,41 @@ public function it_matches_glob_wildcards_literally_in_character_classes(): void
114114
#[Test]
115115
public function it_matches_any_character_not_in_a_set(): void
116116
{
117-
$this->assertTrue(Glob::match('[^abc]', 'x'));
118-
$this->assertTrue(Glob::match('[^abc]', 'z'));
117+
$this->assertTrue(Glob::match('[!abc]', 'x'));
118+
$this->assertTrue(Glob::match('[!abc]', 'z'));
119119

120-
$this->assertTrue(Glob::match('[^abc][^xyz]', 'za'));
121-
$this->assertTrue(Glob::match('[^abc][^xyz]', 'ya'));
120+
$this->assertTrue(Glob::match('[!abc][!xyz]', 'za'));
121+
$this->assertTrue(Glob::match('[!abc][!xyz]', 'ya'));
122122

123-
$this->assertTrue(Glob::match('[^abc]oo', 'foo'));
124-
$this->assertTrue(Glob::match('[^abc]oo', 'zoo'));
123+
$this->assertTrue(Glob::match('[!abc]oo', 'foo'));
124+
$this->assertTrue(Glob::match('[!abc]oo', 'zoo'));
125125

126-
$this->assertFalse(Glob::match('[^abc]', 'a'));
127-
$this->assertFalse(Glob::match('[^abc]', 'b'));
128-
$this->assertFalse(Glob::match('[^abc]', 'c'));
129-
$this->assertFalse(Glob::match('[^abc]oo', 'boo'));
130-
$this->assertFalse(Glob::match('[^abc][^xyz]', 'cz'));
131-
$this->assertFalse(Glob::match('[^abc][^xyz]', 'foo'));
126+
$this->assertFalse(Glob::match('[!abc]', 'a'));
127+
$this->assertFalse(Glob::match('[!abc]', 'b'));
128+
$this->assertFalse(Glob::match('[!abc]', 'c'));
129+
$this->assertFalse(Glob::match('[!abc]oo', 'boo'));
130+
$this->assertFalse(Glob::match('[!abc][!xyz]', 'cz'));
131+
$this->assertFalse(Glob::match('[!abc][!xyz]', 'foo'));
132132
}
133133

134134
#[Test]
135135
public function it_matches_any_character_not_in_a_range(): void
136136
{
137-
$this->assertTrue(Glob::match('[^a-c]', 'x'));
138-
$this->assertTrue(Glob::match('[^a-c]', 'z'));
137+
$this->assertTrue(Glob::match('[!a-c]', 'x'));
138+
$this->assertTrue(Glob::match('[!a-c]', 'z'));
139139

140-
$this->assertTrue(Glob::match('[^a-c][^x-z]', 'za'));
141-
$this->assertTrue(Glob::match('[^a-c][^x-z]', 'ya'));
140+
$this->assertTrue(Glob::match('[!a-c][!x-z]', 'za'));
141+
$this->assertTrue(Glob::match('[!a-c][!x-z]', 'ya'));
142142

143-
$this->assertTrue(Glob::match('[^a-c]oo', 'foo'));
144-
$this->assertTrue(Glob::match('[^a-c]oo', 'zoo'));
143+
$this->assertTrue(Glob::match('[!a-c]oo', 'foo'));
144+
$this->assertTrue(Glob::match('[!a-c]oo', 'zoo'));
145145

146-
$this->assertFalse(Glob::match('[^a-c]', 'a'));
147-
$this->assertFalse(Glob::match('[^a-c]', 'b'));
148-
$this->assertFalse(Glob::match('[^a-c]', 'c'));
149-
$this->assertFalse(Glob::match('[^a-c]oo', 'boo'));
150-
$this->assertFalse(Glob::match('[^a-c][^x-z]', 'cz'));
151-
$this->assertFalse(Glob::match('[^a-c][^x-z]', 'foo'));
146+
$this->assertFalse(Glob::match('[!a-c]', 'a'));
147+
$this->assertFalse(Glob::match('[!a-c]', 'b'));
148+
$this->assertFalse(Glob::match('[!a-c]', 'c'));
149+
$this->assertFalse(Glob::match('[!a-c]oo', 'boo'));
150+
$this->assertFalse(Glob::match('[!a-c][!x-z]', 'cz'));
151+
$this->assertFalse(Glob::match('[!a-c][!x-z]', 'foo'));
152152
}
153153

154154
#[Test]

tests/PatternTest.php

Lines changed: 4 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -66,11 +66,10 @@ public function it_can_convert_a_complex_glob_pattern_to_a_regular_expressions()
6666
$this->assertEquals('#^([^/]*|.*/[^/]*)\.txt$#', Pattern::make('{*,**/*}.txt')->toRegex());
6767
$this->assertEquals('#^file\.(yml|yaml)$#', Pattern::make('file.{yml,yaml}')->toRegex());
6868
$this->assertEquals('#^[fbw]oo\.txt$#', Pattern::make('[fbw]oo.txt')->toRegex());
69-
$this->assertEquals('#^[^fbw]oo\.txt$#', Pattern::make('[^fbw]oo.txt')->toRegex());
69+
$this->assertEquals('#^[^fbw]oo\.txt$#', Pattern::make('[!fbw]oo.txt')->toRegex());
7070
$this->assertEquals('#^[[?*\\\\]$#', Pattern::make('[[?*\]')->toRegex());
7171
$this->assertEquals('#^[.\\\\]$#', Pattern::make('[.\]')->toRegex());
7272
$this->assertEquals('#^foo}bar\.txt$#', Pattern::make('foo}bar.txt')->toRegex());
73-
$this->assertEquals('#^foo\^bar\.txt$#', Pattern::make('foo^bar.txt')->toRegex());
7473
$this->assertEquals('#^foo,bar\.txt$#', Pattern::make('foo,bar.txt')->toRegex());
7574
$this->assertEquals('#^foo/.*/[^/]*\.txt$#', Pattern::make('foo/**/*.txt')->toRegex());
7675
}
@@ -111,16 +110,16 @@ public function it_can_escape_a_glob_string(): void
111110
$this->assertEquals('\\*', Pattern::escape('*'));
112111
$this->assertEquals('\\[', Pattern::escape('['));
113112
$this->assertEquals('\\]', Pattern::escape(']'));
114-
$this->assertEquals('\\^', Pattern::escape('^'));
113+
$this->assertEquals('\\!', Pattern::escape('!'));
115114
$this->assertEquals('\\{', Pattern::escape('{'));
116115
$this->assertEquals('\\}', Pattern::escape('}'));
117116
$this->assertEquals('\\,', Pattern::escape(','));
118117
$this->assertEquals('\\(', Pattern::escape('('));
119118
$this->assertEquals('\\)', Pattern::escape(')'));
120119

121120
$this->assertEquals(
122-
'\\\\\\?\\*\\(\\)\\[\\]\\^\\{\\}\\,',
123-
Pattern::escape('\\?*()[]^{},')
121+
'\\\\\\?\\*\\(\\)\\[\\]\\!\\{\\}\\,',
122+
Pattern::escape('\\?*()[]!{},')
124123
);
125124
}
126125
}

0 commit comments

Comments
 (0)