Skip to content

Commit a694c07

Browse files
committed
Fixed a potential issue in GroupSingleCharacters
The pass should ignore meta expressions that do not represent a single character
1 parent 3bf924c commit a694c07

File tree

3 files changed

+34
-1
lines changed

3 files changed

+34
-1
lines changed

src/Passes/AbstractPass.php

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,7 @@
99

1010
use const false, true;
1111
use function array_shift, array_unshift, count, end, is_array;
12+
use s9e\RegexpBuilder\Meta;
1213

1314
abstract class AbstractPass implements PassInterface
1415
{
@@ -127,7 +128,7 @@ protected function isSingleAlternationString(array $string): bool
127128
*/
128129
protected function isSingleCharString(array $string): bool
129130
{
130-
return (count($string) === 1 && !is_array($string[0]));
131+
return (count($string) === 1 && is_int($string[0]) && Meta::isChar($string[0]));
131132
}
132133

133134
/**

tests/Passes/AbstractTest.php

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@
33
namespace s9e\RegexpBuilder\Tests\Passes;
44

55
use PHPUnit\Framework\TestCase;
6+
use s9e\RegexpBuilder\Meta;
67

78
abstract class AbstractTest extends TestCase
89
{
@@ -22,4 +23,12 @@ public function getPassInstance()
2223
}
2324

2425
abstract public function getPassTests();
26+
27+
protected function getMetaValue(string $expr): int
28+
{
29+
$meta = new Meta;
30+
$meta->set('x', $expr);
31+
32+
return $meta->getInputMap()['x'];
33+
}
2534
}

tests/Passes/GroupSingleCharactersTest.php

Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -52,6 +52,29 @@ public function getPassTests()
5252
[2, 2]
5353
]
5454
],
55+
[
56+
[
57+
[1],
58+
[2, 2],
59+
[$this->getMetaValue('\\w')]
60+
],
61+
[
62+
[[[1], [$this->getMetaValue('\\w')]]],
63+
[2, 2]
64+
]
65+
],
66+
[
67+
[
68+
[1],
69+
[2, 2],
70+
[$this->getMetaValue('.*')]
71+
],
72+
[
73+
[1],
74+
[2, 2],
75+
[$this->getMetaValue('.*')]
76+
]
77+
],
5578
];
5679
}
5780
}

0 commit comments

Comments
 (0)