Skip to content

Commit

Permalink
thanks therealgaxbo
Browse files Browse the repository at this point in the history
  • Loading branch information
rodber committed Feb 10, 2024
1 parent a3b1991 commit bd637ee
Show file tree
Hide file tree
Showing 2 changed files with 34 additions and 1 deletion.
5 changes: 4 additions & 1 deletion src/Regex.php
Expand Up @@ -33,7 +33,10 @@ public function __construct(
) {
$this->assertPattern();
$delimiter = $this->pattern[0];
$this->noDelimiters = trim($this->pattern, $delimiter);
$this->noDelimiters = substr($this->pattern, 1);
/** @var int $tailPos */
$tailPos = strrpos($this->noDelimiters, $delimiter);
$this->noDelimiters = substr($this->noDelimiters, 0, $tailPos);
$this->noDelimitersNoAnchors = strval(
preg_replace('#^\^(.*)\$$#', '$1', $this->noDelimiters)
);
Expand Down
30 changes: 30 additions & 0 deletions tests/RegexTest.php
Expand Up @@ -37,6 +37,36 @@ public function testConstruct(): void
$this->assertSame($pattern, $regex->noDelimitersNoAnchors());
}

public function delimiterConflictDataProvider(): array
{
return [
['abc\/', '/'],
['\/abc\/', '/'],
['abc\/i', '/'],
['\/abc\/i', '/'],
['\/ab🐞c\/i', '/'],
];
}

/**
* @dataProvider delimiterConflictDataProvider
*/
public function testDelimiterConflict(string $pattern, string $delimiter): void
{
$patternAnchors = "^{$pattern}$";
$patternDelimitersAnchors = "{$delimiter}{$patternAnchors}{$delimiter}";
$regex = new Regex($patternDelimitersAnchors);
$this->assertSame($patternDelimitersAnchors, $regex->__toString());
$this->assertSame($patternAnchors, $regex->noDelimiters());
$this->assertSame($pattern, $regex->noDelimitersNoAnchors());
$patternNoAnchors = "{$pattern}";
$patternDelimitersAnchors = "{$delimiter}{$patternNoAnchors}{$delimiter}";
$regex = new Regex($patternDelimitersAnchors);
$this->assertSame($patternDelimitersAnchors, $regex->__toString());
$this->assertSame($patternNoAnchors, $regex->noDelimiters());
$this->assertSame($pattern, $regex->noDelimitersNoAnchors());
}

public function testMatch(): void
{
$test = 'Hello World!';
Expand Down

0 comments on commit bd637ee

Please sign in to comment.