@@ -1234,19 +1234,12 @@ private function regexp_check_match_type( $function_name, $match_type ) {
12341234 /**
12351235 * Compile a MySQL-style regex into a PCRE pattern string.
12361236 *
1237- * Translates MySQL match_type flags (c/i/m/n/u) to PCRE modifiers. The u
1238- * (UTF-8) modifier is applied unless byte-oriented matching is requested.
1239- * Case-insensitive is the default.
1240- *
1241- * MySQL uses ICU, which differs from PCRE in some syntax, like ICU-only escapes
1242- * and nested sets. The known failures of the REGEXP test suites list these
1243- * cases. Other known differences:
1237+ * MySQL uses ICU, so WP_ICU_Regex_Translator translates the pattern to PCRE. It
1238+ * documents the supported syntax and how the result differs from ICU. A pattern
1239+ * with other syntax is an error. Other known differences from MySQL:
12441240 *
12451241 * - Text matching ignores case unless the `c` flag is passed, as the column
12461242 * collation isn't known here.
1247- * - PCRE doesn't match "ß" with "ss" when ignoring case, `$` also matches
1248- * between CR and LF, and `(?P<name>...)` is valid.
1249- * - `\G` in a lookbehind doesn't always match where it does in ICU.
12501243 * - Data that isn't valid UTF-8 is matched byte by byte and case-sensitively,
12511244 * like MySQL binary strings. Other binary values match as text, so case
12521245 * sensitivity, and whether positions count bytes, can differ between rows.
@@ -1263,14 +1256,14 @@ private function regexp_check_match_type( $function_name, $match_type ) {
12631256 * @param bool $without_g Whether "\G" never matches, for a search that doesn't
12641257 * start where the previous match ended.
12651258 *
1266- * @throws Exception If the pattern is empty or invalid.
1259+ * @throws Exception If the pattern is empty, unsupported, or invalid.
12671260 * @return string PCRE-ready pattern with delimiter and modifiers.
12681261 */
12691262 private function regexp_compile ( $ pattern , $ match_type , $ binary_mode = false , $ without_g = false ) {
12701263 $ match_type = (string ) $ match_type ;
12711264 $ pattern = (string ) $ pattern ;
12721265 if ( '' === $ pattern ) {
1273- throw new Exception ( 'Illegal argument to a regular expression. ' );
1266+ throw new WP_ICU_Regex_Exception ( 'Illegal argument to a regular expression. ' , 3685 );
12741267 }
12751268
12761269 // Without "\G" in the pattern, both versions are the same.
@@ -1280,55 +1273,11 @@ private function regexp_compile( $pattern, $match_type, $binary_mode = false, $w
12801273 return $ this ->regexp_cache [ $ cache_key ];
12811274 }
12821275
1283- $ case_sensitive = false ;
1284- $ multiline = false ;
1285- $ dotall = false ;
1286- $ unix_lines = false ;
1287- $ len = strlen ( $ match_type );
1288- for ( $ i = 0 ; $ i < $ len ; $ i ++ ) {
1289- $ flag = $ match_type [ $ i ];
1290- if ( 'c ' === $ flag ) {
1291- $ case_sensitive = true ;
1292- } elseif ( 'i ' === $ flag ) {
1293- $ case_sensitive = false ;
1294- } elseif ( 'm ' === $ flag ) {
1295- $ multiline = true ;
1296- } elseif ( 'n ' === $ flag ) {
1297- $ dotall = true ;
1298- } elseif ( 'u ' === $ flag ) {
1299- $ unix_lines = true ;
1300- }
1301- }
1302-
1303- /*
1304- * PHP 7 can read an unknown escape as literal text. For "\y", that matches ICU,
1305- * but "\u0041" matches "u0041", where ICU matches "A". The "X" modifier makes
1306- * all of them errors, like in PHP 8.
1307- */
1308- $ modifiers = $ binary_mode ? 'X ' : 'uX ' ;
1309- if ( ! $ case_sensitive ) {
1310- $ modifiers .= 'i ' ;
1311- }
1312- if ( $ multiline ) {
1313- $ modifiers .= 'm ' ;
1314- }
1315- if ( $ dotall ) {
1316- $ modifiers .= 's ' ;
1317- }
1318-
1319- /*
1320- * In binary data, MySQL ends lines at CR, LF, VT and FF. No PCRE setting
1321- * matches that. (*ANY) also ends lines at the byte 0x85, which is a part of
1322- * characters like "ą" (0xC4 0x85). (*ANYCRLF) misses only the rare VT and FF.
1323- */
1324- if ( $ unix_lines ) {
1325- $ newline = '(*LF) ' ;
1326- } elseif ( $ binary_mode ) {
1327- $ newline = '(*ANYCRLF) ' ;
1328- } else {
1329- $ newline = '(*ANY) ' ;
1330- }
1331- $ compiled = '/ ' . $ newline . $ this ->regexp_escape_pattern ( $ pattern , $ without_g ) . '/ ' . $ modifiers ;
1276+ // The last "c" or "i" wins, and without one, the match ignores case. The letters
1277+ // "n" and "u" are ICU's flags "s" and "d".
1278+ $ flags = 'c ' === substr ( rtrim ( $ match_type , 'mnu ' ), -1 ) ? '' : 'i ' ;
1279+ $ flags .= strtr ( str_replace ( array ( 'c ' , 'i ' ), '' , $ match_type ), 'nu ' , 'sd ' );
1280+ $ compiled = WP_ICU_Regex_Translator ::translate ( $ pattern , $ flags , $ without_g , $ binary_mode );
13321281
13331282 /*
13341283 * Compile the pattern by matching it against an empty string. Only a compile
@@ -1352,71 +1301,6 @@ private function regexp_compile( $pattern, $match_type, $binary_mode = false, $w
13521301 return $ compiled ;
13531302 }
13541303
1355- /**
1356- * Escape pattern delimiters and NUL bytes that are not already escaped, read
1357- * "\K" as "K", and, if asked, make "\G" never match.
1358- *
1359- * PHP before 8.2 rejects NUL bytes in patterns, so they become "\x00".
1360- *
1361- * @param string $pattern The regex pattern.
1362- * @param bool $without_g Optional. Whether "\G" never matches.
1363- *
1364- * @return string Pattern safe to wrap in slash delimiters.
1365- */
1366- private function regexp_escape_pattern ( $ pattern , $ without_g = false ) {
1367- $ special = array (
1368- '/ ' => '\\/ ' ,
1369- "\0" => '\\x00 ' ,
1370- );
1371-
1372- /*
1373- * ICU reads "\K" as "K". In PCRE, "\K" moves the start of the match, even to
1374- * before the place where the search started, like in "(?<=\Ka)".
1375- */
1376- $ escapes = $ special + array ( 'K ' => 'K ' );
1377- if ( $ without_g ) {
1378- // A position can't be both a word boundary and not one, so "\b\B" never
1379- // matches. Unlike "(?!)", it has no ")" to end a "(?#...)" comment.
1380- $ escapes ['G ' ] = '\\b \\B ' ;
1381- }
1382-
1383- $ escaped = '' ;
1384- $ quoted = false ;
1385- $ length = strlen ( $ pattern );
1386- for ( $ i = 0 ; $ i < $ length ; ++$ i ) {
1387- $ character = $ pattern [ $ i ];
1388- if ( $ quoted ) {
1389- if ( '\\' === $ character && $ i + 1 < $ length && 'E ' === $ pattern [ $ i + 1 ] ) {
1390- $ escaped .= '\\E ' ;
1391- $ quoted = false ;
1392- ++$ i ;
1393- } elseif ( isset ( $ special [ $ character ] ) ) {
1394- $ escaped .= '\\E ' . $ special [ $ character ] . '\\Q ' ;
1395- } else {
1396- $ escaped .= $ character ;
1397- }
1398- continue ;
1399- }
1400- if ( '\\' === $ character && $ i + 1 < $ length ) {
1401- $ next = $ pattern [ $ i + 1 ];
1402- $ escaped .= isset ( $ escapes [ $ next ] ) ? $ escapes [ $ next ] : $ character . $ next ;
1403- $ quoted = 'Q ' === $ next ;
1404- ++$ i ;
1405- } elseif ( isset ( $ special [ $ character ] ) ) {
1406- $ escaped .= $ special [ $ character ];
1407- } else {
1408- $ escaped .= $ character ;
1409- }
1410- }
1411-
1412- // Close quoted text that runs to the end, so that a trailing backslash
1413- // can't escape the delimiter.
1414- if ( $ quoted ) {
1415- $ escaped .= '\\E ' ;
1416- }
1417- return $ escaped ;
1418- }
1419-
14201304 /**
14211305 * Check whether a string is valid UTF-8.
14221306 *
@@ -1453,7 +1337,7 @@ private function regexp_char_to_byte_offset( $subject, $position, $allow_past_en
14531337 if ( $ allow_past_end && $ chars + 1 === $ position ) {
14541338 return $ length ;
14551339 }
1456- throw new Exception ( 'Index out of bounds in regular expression search. ' );
1340+ throw new WP_ICU_Regex_Exception ( 'Index out of bounds in regular expression search. ' , 3686 );
14571341 }
14581342
14591343 /**
@@ -1492,15 +1376,15 @@ static function ( $parts ) use ( $match_data, $group_keys ) {
14921376 return $ match_data [ $ name ][0 ] ?? '' ;
14931377 }
14941378 if ( '' === $ digits ) {
1495- throw new Exception ( 'A capture group has an invalid name. ' );
1379+ throw new WP_ICU_Regex_Exception ( 'A capture group has an invalid name. ' , 3887 );
14961380 }
14971381 for ( $ length = strlen ( $ digits ); $ length > 0 ; --$ length ) {
14981382 $ group = (int ) substr ( $ digits , 0 , $ length );
14991383 if ( isset ( $ group_keys [ $ group ] ) ) {
15001384 return ( $ match_data [ $ group ][0 ] ?? '' ) . substr ( $ digits , $ length );
15011385 }
15021386 }
1503- throw new Exception ( 'Index out of bounds in regular expression search. ' );
1387+ throw new WP_ICU_Regex_Exception ( 'Index out of bounds in regular expression search. ' , 3686 );
15041388 },
15051389 $ replacement
15061390 );
@@ -1555,15 +1439,6 @@ private function regexp_matches( $pattern, $match_type, $byte_mode, $subject, $o
15551439 if ( ! $ this ->regexp_match ( $ search , $ pattern , $ subject , $ match , PREG_OFFSET_CAPTURE , $ offset ) ) {
15561440 return ;
15571441 }
1558-
1559- /*
1560- * Only PCRE's "\K" can start a match before the search, and then the next
1561- * search would find the same match forever. The escaper reads "\K" as "K",
1562- * but not after a "\Q" in a comment, which it reads as quoted text.
1563- */
1564- if ( $ match [0 ][1 ] < $ offset ) {
1565- throw new Exception ( 'Invalid regular expression: ' . $ pattern . '. ' );
1566- }
15671442 yield $ match ;
15681443
15691444 $ offset = $ match [0 ][1 ] + strlen ( $ match [0 ][0 ] );
@@ -1627,8 +1502,10 @@ private function regexp_fail( $pattern ) {
16271502 || PREG_RECURSION_LIMIT_ERROR === $ err
16281503 || PREG_JIT_STACKLIMIT_ERROR === $ err
16291504 ) {
1630- throw new Exception ( 'Regular expression evaluation exceeded internal limits. ' );
1505+ // Like in MySQL, a time limit and a stack limit are different errors.
1506+ $ code = PREG_BACKTRACK_LIMIT_ERROR === $ err ? 3699 : 3698 ;
1507+ throw new WP_ICU_Regex_Exception ( 'Regular expression evaluation exceeded internal limits. ' , $ code );
16311508 }
1632- throw new Exception ( 'Invalid regular expression: ' . $ pattern . '. ' );
1509+ throw new WP_ICU_Regex_Exception ( 'Invalid regular expression: ' . $ pattern . '. ' , 3688 );
16331510 }
16341511}
0 commit comments