Skip to content

Commit

Permalink
Fixed some characters being improperly escaped within character groups
Browse files Browse the repository at this point in the history
  • Loading branch information
PHLAK committed Mar 28, 2023
1 parent 2c93400 commit e74fcfe
Show file tree
Hide file tree
Showing 2 changed files with 16 additions and 21 deletions.
36 changes: 15 additions & 21 deletions src/Pattern.php
Original file line number Diff line number Diff line change
Expand Up @@ -73,11 +73,7 @@ public function toRegex(int $options = self::BOTH_ANCHORS): string

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

break;

Expand All @@ -89,13 +85,15 @@ public function toRegex(int $options = self::BOTH_ANCHORS): string
case '*':
if ($characterGroup) {
$pattern .= $char;

break;
}

if (isset($this->pattern[$i + 1]) && $this->pattern[$i + 1] === '*') {
$pattern .= '.*';
++$i;
} else {
if (isset($this->pattern[$i + 1]) && $this->pattern[$i + 1] === '*') {
$pattern .= '.*';
++$i;
} else {
$pattern .= sprintf('[^%s]*', addslashes(static::$directorySeparator));
}
$pattern .= sprintf('[^%s]*', addslashes(static::$directorySeparator));
}

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

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

break;

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

case '}':
if ($patternGroup > 0) {
$pattern .= ')';
$pattern .= $characterGroup ? $char : ')';
--$patternGroup;
} else {
$pattern .= $char;
Expand All @@ -147,7 +141,7 @@ public function toRegex(int $options = self::BOTH_ANCHORS): string

case ',':
if ($patternGroup > 0) {
$pattern .= '|';
$pattern .= $characterGroup ? $char : '|';
} else {
$pattern .= $char;
}
Expand All @@ -159,7 +153,7 @@ public function toRegex(int $options = self::BOTH_ANCHORS): string
$pattern .= sprintf('(?%s', $this->pattern[++$i]);
++$lookaheadGroup;
} else {
$pattern .= '\\' . $char;
$pattern .= $characterGroup ? $char : '\\' . $char;
}

break;
Expand All @@ -169,14 +163,14 @@ public function toRegex(int $options = self::BOTH_ANCHORS): string
--$lookaheadGroup;
$pattern .= $char;
} else {
$pattern .= '\\' . $char;
$pattern .= $characterGroup ? $char : '\\' . $char;
}

break;

default:
if (in_array($char, ['.', '|', '+', '$'])) {
$pattern .= '\\' . $char;
$pattern .= $characterGroup ? $char : '\\' . $char;
} else {
$pattern .= $char;
}
Expand Down
1 change: 1 addition & 0 deletions tests/PatternTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -54,6 +54,7 @@ public function test_it_can_convert_a_complex_glob_pattern_to_a_regular_expressi
$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('#^[.\\\\]$#', 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 e74fcfe

Please sign in to comment.