Skip to content

Commit

Permalink
Refactor CombinedRegexp + use FQN for functions (#106)
Browse files Browse the repository at this point in the history
  • Loading branch information
vjik committed Jul 31, 2023
1 parent 97126d5 commit 55204cd
Show file tree
Hide file tree
Showing 4 changed files with 21 additions and 10 deletions.
3 changes: 2 additions & 1 deletion CHANGELOG.md
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 to improve performance (@vjik)

## 2.1.2 July 27, 2023

Expand Down
26 changes: 17 additions & 9 deletions src/CombinedRegexp.php
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);

Check failure on line 75 in src/CombinedRegexp.php

View workflow job for this annotation

GitHub Actions / psalm / PHP 8.1-ubuntu-latest

ArgumentTypeCoercion

src/CombinedRegexp.php:75:29: ArgumentTypeCoercion: Argument 1 of preg_match expects non-empty-string, but parent type string provided (see https://psalm.dev/193)
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,8 +100,8 @@ 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),
Expand Down
1 change: 1 addition & 0 deletions src/NumericHelper.php
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
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 55204cd

Please sign in to comment.