-
-
Notifications
You must be signed in to change notification settings - Fork 16
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Refactor CombinedRegexp and MemoizedCombinedRegexp classes, add tests…
… and docs
- Loading branch information
Showing
9 changed files
with
473 additions
and
296 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,65 @@ | ||
<?php | ||
|
||
declare(strict_types=1); | ||
|
||
namespace Yiisoft\Strings; | ||
|
||
use Exception; | ||
|
||
/** | ||
* `CombinedRegexp` optimizes matching of multiple regular expressions. | ||
* Read more about the concept in | ||
* {@see https://nikic.github.io/2014/02/18/Fast-request-routing-using-regular-expressions.html}. | ||
*/ | ||
abstract class AbstractCombinedRegexp | ||
{ | ||
public const REGEXP_DELIMITER = '/'; | ||
public const QUOTE_REPLACER = '\\/'; | ||
|
||
/** | ||
* @return string[] Regular expressions to combine. | ||
*/ | ||
abstract public function getPatterns(): array; | ||
|
||
/** | ||
* @return string Flags to apply to all regular expressions. | ||
*/ | ||
abstract public function getFlags(): string; | ||
|
||
/** | ||
* @return string The compiled pattern. | ||
*/ | ||
abstract public function getCompiledPattern(): string; | ||
|
||
/** | ||
* Returns `true` whether the given string matches any of the patterns, `false` - otherwise. | ||
*/ | ||
abstract public function matches(string $string): bool; | ||
|
||
/** | ||
* Returns pattern that matches the given string. | ||
* @throws Exception if the string does not match any of the patterns. | ||
*/ | ||
abstract public function getMatchingPattern(string $string): string; | ||
|
||
/** | ||
* Returns position of the pattern that matches the given string. | ||
* @throws Exception if the string does not match any of the patterns. | ||
*/ | ||
abstract public function getMatchingPatternPosition(string $string): int; | ||
|
||
/** | ||
* @throws Exception | ||
* @return never-return | ||
*/ | ||
protected function throwFailedMatchException(string $string): void | ||
{ | ||
throw new Exception( | ||
sprintf( | ||
'Failed to match pattern "%s" with string "%s".', | ||
$this->getCompiledPattern(), | ||
$string, | ||
) | ||
); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.