@@ -151,40 +151,43 @@ private function getCommonPrefix(string $prefix, string $anotherPrefix): array
151
151
$ staticLength = null ;
152
152
set_error_handler ([__CLASS__ , 'handleError ' ]);
153
153
154
- for ($ i = $ baseLength ; $ i < $ end && $ prefix [$ i ] === $ anotherPrefix [$ i ]; ++$ i ) {
155
- if ('( ' === $ prefix [$ i ]) {
156
- $ staticLength = $ staticLength ?? $ i ;
157
- for ($ j = 1 + $ i , $ n = 1 ; $ j < $ end && 0 < $ n ; ++$ j ) {
158
- if ($ prefix [$ j ] !== $ anotherPrefix [$ j ]) {
159
- break 2 ;
154
+ try {
155
+ for ($ i = $ baseLength ; $ i < $ end && $ prefix [$ i ] === $ anotherPrefix [$ i ]; ++$ i ) {
156
+ if ('( ' === $ prefix [$ i ]) {
157
+ $ staticLength = $ staticLength ?? $ i ;
158
+ for ($ j = 1 + $ i , $ n = 1 ; $ j < $ end && 0 < $ n ; ++$ j ) {
159
+ if ($ prefix [$ j ] !== $ anotherPrefix [$ j ]) {
160
+ break 2 ;
161
+ }
162
+ if ('( ' === $ prefix [$ j ]) {
163
+ ++$ n ;
164
+ } elseif (') ' === $ prefix [$ j ]) {
165
+ --$ n ;
166
+ } elseif ('\\' === $ prefix [$ j ] && (++$ j === $ end || $ prefix [$ j ] !== $ anotherPrefix [$ j ])) {
167
+ --$ j ;
168
+ break ;
169
+ }
160
170
}
161
- if ('( ' === $ prefix [$ j ]) {
162
- ++$ n ;
163
- } elseif (') ' === $ prefix [$ j ]) {
164
- --$ n ;
165
- } elseif ('\\' === $ prefix [$ j ] && (++$ j === $ end || $ prefix [$ j ] !== $ anotherPrefix [$ j ])) {
166
- --$ j ;
171
+ if (0 < $ n ) {
167
172
break ;
168
173
}
169
- }
170
- if (0 < $ n ) {
171
- break ;
172
- }
173
- if (('? ' === ($ prefix [$ j ] ?? '' ) || '? ' === ($ anotherPrefix [$ j ] ?? '' )) && ($ prefix [$ j ] ?? '' ) !== ($ anotherPrefix [$ j ] ?? '' )) {
174
- break ;
175
- }
176
- $ subPattern = substr ($ prefix , $ i , $ j - $ i );
177
- if ($ prefix !== $ anotherPrefix && !preg_match ('/^\(\[[^\]]++\]\+\+\)$/ ' , $ subPattern ) && !preg_match ('{(?<! ' .$ subPattern .')} ' , '' )) {
178
- // sub-patterns of variable length are not considered as common prefixes because their greediness would break in-order matching
174
+ if (('? ' === ($ prefix [$ j ] ?? '' ) || '? ' === ($ anotherPrefix [$ j ] ?? '' )) && ($ prefix [$ j ] ?? '' ) !== ($ anotherPrefix [$ j ] ?? '' )) {
175
+ break ;
176
+ }
177
+ $ subPattern = substr ($ prefix , $ i , $ j - $ i );
178
+ if ($ prefix !== $ anotherPrefix && !preg_match ('/^\(\[[^\]]++\]\+\+\)$/ ' , $ subPattern ) && !preg_match ('{(?<! ' .$ subPattern .')} ' , '' )) {
179
+ // sub-patterns of variable length are not considered as common prefixes because their greediness would break in-order matching
180
+ break ;
181
+ }
182
+ $ i = $ j - 1 ;
183
+ } elseif ('\\' === $ prefix [$ i ] && (++$ i === $ end || $ prefix [$ i ] !== $ anotherPrefix [$ i ])) {
184
+ --$ i ;
179
185
break ;
180
186
}
181
- $ i = $ j - 1 ;
182
- } elseif ('\\' === $ prefix [$ i ] && (++$ i === $ end || $ prefix [$ i ] !== $ anotherPrefix [$ i ])) {
183
- --$ i ;
184
- break ;
185
187
}
188
+ } finally {
189
+ restore_error_handler ();
186
190
}
187
- restore_error_handler ();
188
191
if ($ i < $ end && 0b10 === (\ord ($ prefix [$ i ]) >> 6 ) && preg_match ('//u ' , $ prefix .' ' .$ anotherPrefix )) {
189
192
do {
190
193
// Prevent cutting in the middle of an UTF-8 characters
0 commit comments