Skip to content

Commit

Permalink
Refactor CombinedRegexp + use FQN for functions
Browse files Browse the repository at this point in the history
  • Loading branch information
vjik committed Jul 29, 2023
1 parent 97126d5 commit b187eaf
Show file tree
Hide file tree
Showing 4 changed files with 24 additions and 13 deletions.
3 changes: 2 additions & 1 deletion CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,8 @@

## 2.2.0 under development

- Enh #102: Add `CombinedRegexp` class (@xepozz)
- New #102, #106: Add `CombinedRegexp` class (@xepozz, @vjik)
- Enh #106: Using fully-qualified function calls for improve performance (@vjik)

## 2.1.2 July 27, 2023

Expand Down
32 changes: 20 additions & 12 deletions src/CombinedRegexp.php
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,12 @@

namespace Yiisoft\Strings;

use Exception;

use InvalidArgumentException;

use function count;

/**
* `CombinedRegexp` optimizes matching of multiple regular expressions.
* Read more about the concept in
Expand All @@ -28,11 +34,11 @@ public function __construct(
array $patterns,
string $flags = ''
) {
if (count($patterns) === 0) {
throw new \InvalidArgumentException('At least one pattern should be specified.');
if (empty($patterns)) {
throw new InvalidArgumentException('At least one pattern should be specified.');
}
$this->patterns = $patterns;
$this->compiledPattern = $this->compilePatterns($patterns) . $flags;
$this->patterns = array_values($patterns);
$this->compiledPattern = $this->compilePatterns($this->patterns) . $flags;
}

/**
Expand All @@ -53,7 +59,7 @@ 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.
* @throws Exception if the string does not match any of the patterns.
*/
public function getMatchingPattern(string $string): string
{
Expand All @@ -62,13 +68,13 @@ 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.
* @throws Exception if the string does not match any of the patterns.
*/
public function getMatchingPatternPosition(string $string): int
{
$match = preg_match($this->compiledPattern, $string, $matches);
if ($match !== 1) {
throw new \Exception(
throw new Exception(
sprintf(
'Failed to match pattern "%s" with string "%s".',
$this->getCompiledPattern(),
Expand All @@ -82,6 +88,8 @@ public function getMatchingPatternPosition(string $string): int

/**
* @param string[] $patterns
*
* @psalm-param list<string> $patterns
*/
private function compilePatterns(array $patterns): string
{
Expand All @@ -92,13 +100,13 @@ private function compilePatterns(array $patterns): string
* It doesn't matter where to place `()` in the pattern:
* https://regex101.com/r/lE1Q1S/1, https://regex101.com/r/rWg7Fj/1
*/
for ($i = 0; $i < count($patterns); $i++) {
$quotedPatterns[] = $patterns[$i] . str_repeat('()', $i);
foreach ($patterns as $i => $pattern) {
$quotedPatterns[] = $pattern . str_repeat('()', $i);
}
$combinedRegexps = '(?|' . strtr(
implode('|', $quotedPatterns),
[self::REGEXP_DELIMITER => self::QUOTE_REPLACER]
) . ')';
implode('|', $quotedPatterns),
[self::REGEXP_DELIMITER => self::QUOTE_REPLACER]
) . ')';

return self::REGEXP_DELIMITER . $combinedRegexps . self::REGEXP_DELIMITER;
}
Expand Down
1 change: 1 addition & 0 deletions src/NumericHelper.php
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@

use InvalidArgumentException;

use function gettype;
use function in_array;
use function is_bool;

Expand Down
1 change: 1 addition & 0 deletions src/StringHelper.php
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@
use function mb_substr;
use function str_ends_with;
use function str_starts_with;
use function strlen;

/**
* Provides static methods to work with strings.
Expand Down

0 comments on commit b187eaf

Please sign in to comment.