forked from Respect/StringFormatter
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathCompiledPattern.php
More file actions
204 lines (163 loc) · 6.29 KB
/
Copy pathCompiledPattern.php
File metadata and controls
204 lines (163 loc) · 6.29 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
<?php
/*
* SPDX-FileCopyrightText: (c) Respect Project Contributors
* SPDX-License-Identifier: ISC
* SPDX-FileContributor: Alexandre Gomes Gaigalas <alganet@gmail.com>
*/
declare(strict_types=1);
namespace Respect\StringFormatter\Internal;
use Respect\StringFormatter\InvalidFormatterException;
use function array_keys;
use function count;
use function implode;
use function mb_strtolower;
use function mb_strtoupper;
use function mb_substr;
use function preg_match;
use function preg_match_all;
use function sprintf;
use function str_starts_with;
use function strtolower;
use function substr;
use const PREG_OFFSET_CAPTURE;
final class CompiledPattern
{
private const array FILTERS = [
'#' => '.',
'0' => '\p{N}',
'A' => '\p{Lu}',
'a' => '\p{Ll}',
'C' => '\p{L}',
'W' => '\p{L}|\p{N}',
];
private const array TRANSFORM_MAP = ['l' => 'lower', 'u' => 'upper', 'i' => 'invert'];
/** @var array<string, CompiledPattern> */
private static array $compiledPatterns = [];
/** @var array<string, string> */
private static array $compiledQualifiers = [];
/** @param array<int, array{filter: string, transform: string|null}> $instructions */
private function __construct(
private(set) readonly string $pattern,
private(set) readonly string $search,
private(set) readonly string $replacement,
private(set) readonly array $instructions,
) {
}
public static function compile(string $pattern): self
{
if (isset(self::$compiledPatterns[$pattern])) {
return self::$compiledPatterns[$pattern];
}
if ($pattern === '') {
throw new InvalidFormatterException('Pattern cannot be empty');
}
$search = '';
$replacement = '';
$instructions = [];
$groupIndex = 1;
$transformState = null;
$nextTransform = null;
preg_match_all(sprintf(
'/(?:\\\\.|[%1$s]|(?:\{[^}]*\}|[*+?])|[^\\\%1$s{}+*?]+|.)/u',
implode('', array_keys(self::FILTERS)),
), $pattern, $tokens, PREG_OFFSET_CAPTURE);
$tokenList = $tokens[0];
$count = count($tokenList);
for ($i = 0; $i < $count; $i++) {
[$tokenText, $offset] = $tokenList[$i];
if (str_starts_with($tokenText, '\\')) {
if ($tokenText === '\\') {
throw new InvalidFormatterException('Incomplete escape sequence at end of pattern');
}
$char = mb_substr($tokenText, 1);
if ($char === 'd') {
$inner = '.';
$search .= sprintf('((?:.*?%s){0,1})', $inner);
$replacement .= sprintf('%%%d$', $groupIndex);
$instructions[$groupIndex] = ['filter' => sprintf('/%s/u', $inner), 'transform' => 'delete'];
$groupIndex++;
continue;
}
if ($char === 'E') {
$transformState = null;
continue;
}
if (isset(self::TRANSFORM_MAP[$char])) {
$nextTransform = self::TRANSFORM_MAP[$char];
continue;
}
$lowerChar = strtolower($char);
if (isset(self::TRANSFORM_MAP[$lowerChar]) && $char !== $lowerChar) {
$transformState = self::TRANSFORM_MAP[$lowerChar];
continue;
}
$replacement .= $char;
continue;
}
if (isset(self::FILTERS[$tokenText])) {
$filterChar = $tokenText;
$regexQuantifier = '{0,1}';
if (isset($tokenList[$i + 1]) && preg_match('/^(?:\{[^}]*\}|[*+?])$/u', $tokenList[$i + 1][0])) {
$i++;
$regexQuantifier = self::compileQualifier($tokenList[$i][0], $tokenList[$i][1]);
}
$inner = self::FILTERS[$filterChar];
$search .= sprintf('((?:.*?%s)%s)', $inner, $regexQuantifier);
$replacement .= sprintf('%%%d$', $groupIndex);
$instructions[$groupIndex] = [
'filter' => sprintf('/%s/u', $inner),
'transform' => $nextTransform ?? $transformState,
];
$groupIndex++;
$nextTransform = null;
continue;
}
if (preg_match('/^(?:\{[^}]*\}|[*+?])$/u', $tokenText)) {
throw new InvalidFormatterException(
sprintf('Quantifier "%s" must follow a filter pattern at position %d', $tokenText[0], $offset),
);
}
if (str_starts_with($tokenText, '{')) {
throw new InvalidFormatterException(
sprintf('Invalid or malformed quantifier at position %d', $offset),
);
}
$replacement .= $tokenText;
}
return self::$compiledPatterns[$pattern] = new self(
$pattern,
'/^' . $search . '/us',
$replacement,
$instructions,
);
}
public static function transform(string $val, string|null $transform): string
{
return match ($transform) {
'delete' => '',
'lower' => mb_strtolower($val),
'upper' => mb_strtoupper($val),
'invert' => mb_strtolower($val) ^ mb_strtoupper($val) ^ $val,
default => $val,
};
}
private static function compileQualifier(string $token, int $offset): string
{
if (isset(self::$compiledQualifiers[$token])) {
return self::$compiledQualifiers[$token];
}
if ($token === '*') {
return '*';
}
if ($token === '+') {
return '{1,}';
}
$content = substr($token, 1, -1);
if ($content === '' || $content === ',' || !preg_match('/^(\d+(?:,\d*)?|,\d+)$/', $content)) {
throw new InvalidFormatterException(sprintf('Invalid or malformed quantifier at position %d', $offset));
}
preg_match('/^\{(\d*)(?:,(\d*))?\}$/', $token, $m);
$max = $m[2] ?? $m[1];
return self::$compiledQualifiers[$token] = $max === '' ? '*' : sprintf('{0,%s}', $max);
}
}