forked from PHP-CS-Fixer/PHP-CS-Fixer
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathPreg.php
201 lines (170 loc) · 6.3 KB
/
Preg.php
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
<?php
declare(strict_types=1);
/*
* This file is part of PHP CS Fixer.
*
* (c) Fabien Potencier <[email protected]>
* Dariusz Rumiński <[email protected]>
*
* This source file is subject to the MIT license that is bundled
* with this source code in the file LICENSE.
*/
namespace PhpCsFixer;
/**
* This class replaces preg_* functions to better handling UTF8 strings,
* ensuring no matter "u" modifier is present or absent subject will be handled correctly.
*
* @author Kuba Werłos <[email protected]>
*
* @internal
*/
final class Preg
{
/**
* @param null|string[] $matches
* @param int-mask<0, 256, 512> $flags
*
* @throws PregException
*/
public static function match(string $pattern, string $subject, ?array &$matches = null, int $flags = 0, int $offset = 0): bool
{
$result = @preg_match(self::addUtf8Modifier($pattern), $subject, $matches, $flags, $offset);
if (false !== $result && PREG_NO_ERROR === preg_last_error()) {
return 1 === $result;
}
$result = @preg_match(self::removeUtf8Modifier($pattern), $subject, $matches, $flags, $offset);
if (false !== $result && PREG_NO_ERROR === preg_last_error()) {
return 1 === $result;
}
throw self::newPregException(preg_last_error(), __METHOD__, (array) $pattern);
}
/**
* @param null|string[] $matches
*
* @throws PregException
*/
public static function matchAll(string $pattern, string $subject, ?array &$matches = null, int $flags = PREG_PATTERN_ORDER, int $offset = 0): int
{
$result = @preg_match_all(self::addUtf8Modifier($pattern), $subject, $matches, $flags, $offset);
if (false !== $result && PREG_NO_ERROR === preg_last_error()) {
return $result;
}
$result = @preg_match_all(self::removeUtf8Modifier($pattern), $subject, $matches, $flags, $offset);
if (false !== $result && PREG_NO_ERROR === preg_last_error()) {
return $result;
}
throw self::newPregException(preg_last_error(), __METHOD__, (array) $pattern);
}
/**
* @param string|string[] $subject
*
* @throws PregException
*/
public static function replace(string $pattern, string $replacement, $subject, int $limit = -1, ?int &$count = null): string
{
$result = @preg_replace(self::addUtf8Modifier($pattern), $replacement, $subject, $limit, $count);
if (null !== $result && PREG_NO_ERROR === preg_last_error()) {
return $result;
}
$result = @preg_replace(self::removeUtf8Modifier($pattern), $replacement, $subject, $limit, $count);
if (null !== $result && PREG_NO_ERROR === preg_last_error()) {
return $result;
}
throw self::newPregException(preg_last_error(), __METHOD__, (array) $pattern);
}
/**
* @throws PregException
*/
public static function replaceCallback(string $pattern, callable $callback, string $subject, int $limit = -1, ?int &$count = null): string
{
$result = @preg_replace_callback(self::addUtf8Modifier($pattern), $callback, $subject, $limit, $count);
if (null !== $result && PREG_NO_ERROR === preg_last_error()) {
return $result;
}
$result = @preg_replace_callback(self::removeUtf8Modifier($pattern), $callback, $subject, $limit, $count);
if (null !== $result && PREG_NO_ERROR === preg_last_error()) {
return $result;
}
throw self::newPregException(preg_last_error(), __METHOD__, (array) $pattern);
}
/**
* @return string[]
*
* @throws PregException
*/
public static function split(string $pattern, string $subject, int $limit = -1, int $flags = 0): array
{
$result = @preg_split(self::addUtf8Modifier($pattern), $subject, $limit, $flags);
if (false !== $result && PREG_NO_ERROR === preg_last_error()) {
return $result;
}
$result = @preg_split(self::removeUtf8Modifier($pattern), $subject, $limit, $flags);
if (false !== $result && PREG_NO_ERROR === preg_last_error()) {
return $result;
}
throw self::newPregException(preg_last_error(), __METHOD__, (array) $pattern);
}
/**
* @param string|string[] $pattern
*
* @return string|string[]
*/
private static function addUtf8Modifier($pattern)
{
if (\is_array($pattern)) {
return array_map(__METHOD__, $pattern);
}
return $pattern.'u';
}
/**
* @param string|string[] $pattern
*
* @return string|string[]
*/
private static function removeUtf8Modifier($pattern)
{
if (\is_array($pattern)) {
return array_map(__METHOD__, $pattern);
}
if ('' === $pattern) {
return '';
}
$delimiter = $pattern[0];
$endDelimiterPosition = strrpos($pattern, $delimiter);
return substr($pattern, 0, $endDelimiterPosition).str_replace('u', '', substr($pattern, $endDelimiterPosition));
}
/**
* Create PregException.
*
* Create the generic PregException message and if possible due to finding
* an invalid pattern, tell more about such kind of error in the message.
*
* @param string[] $patterns
*/
private static function newPregException(int $error, string $method, array $patterns): PregException
{
foreach ($patterns as $pattern) {
$last = error_get_last();
$result = @preg_match($pattern, '');
if (false !== $result) {
continue;
}
$code = preg_last_error();
$next = error_get_last();
if ($last !== $next) {
$message = sprintf(
'(code: %d) %s',
$code,
preg_replace('~preg_[a-z_]+[()]{2}: ~', '', $next['message'])
);
} else {
$message = sprintf('(code: %d)', $code);
}
return new PregException(
sprintf('%s(): Invalid PCRE pattern "%s": %s (version: %s)', $method, $pattern, $message, PCRE_VERSION),
$code
);
}
return new PregException(sprintf('Error occurred when calling %s.', $method), $error);
}
}