Skip to content

Commit e74fcfe

Browse files
committed
Fixed some characters being improperly escaped within character groups
1 parent 2c93400 commit e74fcfe

File tree

2 files changed

+16
-21
lines changed

2 files changed

+16
-21
lines changed

src/Pattern.php

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

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

8278
break;
8379

@@ -89,13 +85,15 @@ public function toRegex(int $options = self::BOTH_ANCHORS): string
8985
case '*':
9086
if ($characterGroup) {
9187
$pattern .= $char;
88+
89+
break;
90+
}
91+
92+
if (isset($this->pattern[$i + 1]) && $this->pattern[$i + 1] === '*') {
93+
$pattern .= '.*';
94+
++$i;
9295
} else {
93-
if (isset($this->pattern[$i + 1]) && $this->pattern[$i + 1] === '*') {
94-
$pattern .= '.*';
95-
++$i;
96-
} else {
97-
$pattern .= sprintf('[^%s]*', addslashes(static::$directorySeparator));
98-
}
96+
$pattern .= sprintf('[^%s]*', addslashes(static::$directorySeparator));
9997
}
10098

10199
break;
@@ -121,11 +119,7 @@ public function toRegex(int $options = self::BOTH_ANCHORS): string
121119
break;
122120

123121
case '^':
124-
if ($characterGroup) {
125-
$pattern .= $char;
126-
} else {
127-
$pattern .= '\\' . $char;
128-
}
122+
$pattern .= $characterGroup ? $char : '\\' . $char;
129123

130124
break;
131125

@@ -137,7 +131,7 @@ public function toRegex(int $options = self::BOTH_ANCHORS): string
137131

138132
case '}':
139133
if ($patternGroup > 0) {
140-
$pattern .= ')';
134+
$pattern .= $characterGroup ? $char : ')';
141135
--$patternGroup;
142136
} else {
143137
$pattern .= $char;
@@ -147,7 +141,7 @@ public function toRegex(int $options = self::BOTH_ANCHORS): string
147141

148142
case ',':
149143
if ($patternGroup > 0) {
150-
$pattern .= '|';
144+
$pattern .= $characterGroup ? $char : '|';
151145
} else {
152146
$pattern .= $char;
153147
}
@@ -159,7 +153,7 @@ public function toRegex(int $options = self::BOTH_ANCHORS): string
159153
$pattern .= sprintf('(?%s', $this->pattern[++$i]);
160154
++$lookaheadGroup;
161155
} else {
162-
$pattern .= '\\' . $char;
156+
$pattern .= $characterGroup ? $char : '\\' . $char;
163157
}
164158

165159
break;
@@ -169,14 +163,14 @@ public function toRegex(int $options = self::BOTH_ANCHORS): string
169163
--$lookaheadGroup;
170164
$pattern .= $char;
171165
} else {
172-
$pattern .= '\\' . $char;
166+
$pattern .= $characterGroup ? $char : '\\' . $char;
173167
}
174168

175169
break;
176170

177171
default:
178172
if (in_array($char, ['.', '|', '+', '$'])) {
179-
$pattern .= '\\' . $char;
173+
$pattern .= $characterGroup ? $char : '\\' . $char;
180174
} else {
181175
$pattern .= $char;
182176
}

tests/PatternTest.php

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -54,6 +54,7 @@ public function test_it_can_convert_a_complex_glob_pattern_to_a_regular_expressi
5454
$this->assertEquals('#^[fbw]oo\.txt$#', Pattern::make('[fbw]oo.txt')->toRegex());
5555
$this->assertEquals('#^[^fbw]oo\.txt$#', Pattern::make('[^fbw]oo.txt')->toRegex());
5656
$this->assertEquals('#^[[?*\\\\]$#', Pattern::make('[[?*\]')->toRegex());
57+
$this->assertEquals('#^[.\\\\]$#', Pattern::make('[.\]')->toRegex());
5758
$this->assertEquals('#^foo}bar\.txt$#', Pattern::make('foo}bar.txt')->toRegex());
5859
$this->assertEquals('#^foo\^bar\.txt$#', Pattern::make('foo^bar.txt')->toRegex());
5960
$this->assertEquals('#^foo,bar\.txt$#', Pattern::make('foo,bar.txt')->toRegex());

0 commit comments

Comments
 (0)