Skip to content

Commit 55204cd

Browse files
authored
Refactor CombinedRegexp + use FQN for functions (#106)
1 parent 97126d5 commit 55204cd

File tree

4 files changed

+21
-10
lines changed

4 files changed

+21
-10
lines changed

CHANGELOG.md

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,8 @@
22

33
## 2.2.0 under development
44

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

78
## 2.1.2 July 27, 2023
89

src/CombinedRegexp.php

Lines changed: 17 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,12 @@
44

55
namespace Yiisoft\Strings;
66

7+
use Exception;
8+
9+
use InvalidArgumentException;
10+
11+
use function count;
12+
713
/**
814
* `CombinedRegexp` optimizes matching of multiple regular expressions.
915
* Read more about the concept in
@@ -28,11 +34,11 @@ public function __construct(
2834
array $patterns,
2935
string $flags = ''
3036
) {
31-
if (count($patterns) === 0) {
32-
throw new \InvalidArgumentException('At least one pattern should be specified.');
37+
if (empty($patterns)) {
38+
throw new InvalidArgumentException('At least one pattern should be specified.');
3339
}
34-
$this->patterns = $patterns;
35-
$this->compiledPattern = $this->compilePatterns($patterns) . $flags;
40+
$this->patterns = array_values($patterns);
41+
$this->compiledPattern = $this->compilePatterns($this->patterns) . $flags;
3642
}
3743

3844
/**
@@ -53,7 +59,7 @@ public function matches(string $string): bool
5359

5460
/**
5561
* Returns pattern that matches the given string.
56-
* @throws \Exception if the string does not match any of the patterns.
62+
* @throws Exception if the string does not match any of the patterns.
5763
*/
5864
public function getMatchingPattern(string $string): string
5965
{
@@ -62,13 +68,13 @@ public function getMatchingPattern(string $string): string
6268

6369
/**
6470
* Returns position of the pattern that matches the given string.
65-
* @throws \Exception if the string does not match any of the patterns.
71+
* @throws Exception if the string does not match any of the patterns.
6672
*/
6773
public function getMatchingPatternPosition(string $string): int
6874
{
6975
$match = preg_match($this->compiledPattern, $string, $matches);
7076
if ($match !== 1) {
71-
throw new \Exception(
77+
throw new Exception(
7278
sprintf(
7379
'Failed to match pattern "%s" with string "%s".',
7480
$this->getCompiledPattern(),
@@ -82,6 +88,8 @@ public function getMatchingPatternPosition(string $string): int
8288

8389
/**
8490
* @param string[] $patterns
91+
*
92+
* @psalm-param list<string> $patterns
8593
*/
8694
private function compilePatterns(array $patterns): string
8795
{
@@ -92,8 +100,8 @@ private function compilePatterns(array $patterns): string
92100
* It doesn't matter where to place `()` in the pattern:
93101
* https://regex101.com/r/lE1Q1S/1, https://regex101.com/r/rWg7Fj/1
94102
*/
95-
for ($i = 0; $i < count($patterns); $i++) {
96-
$quotedPatterns[] = $patterns[$i] . str_repeat('()', $i);
103+
foreach ($patterns as $i => $pattern) {
104+
$quotedPatterns[] = $pattern . str_repeat('()', $i);
97105
}
98106
$combinedRegexps = '(?|' . strtr(
99107
implode('|', $quotedPatterns),

src/NumericHelper.php

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@
66

77
use InvalidArgumentException;
88

9+
use function gettype;
910
use function in_array;
1011
use function is_bool;
1112

src/StringHelper.php

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,7 @@
1616
use function mb_substr;
1717
use function str_ends_with;
1818
use function str_starts_with;
19+
use function strlen;
1920

2021
/**
2122
* Provides static methods to work with strings.

0 commit comments

Comments
 (0)