@@ -1238,9 +1238,9 @@ private function regexp_check_match_type( $function_name, $match_type ) {
12381238 * (UTF-8) modifier is applied unless byte-oriented matching is requested.
12391239 * Case-insensitive is the default.
12401240 *
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:
1241+ * MySQL uses ICU, which differs from PCRE in some syntax, like ICU-only escapes.
1242+ * The known failures of the REGEXP test suites list these cases. Other known
1243+ * differences:
12441244 *
12451245 * - Text matching ignores case unless the `c` flag is passed, as the column
12461246 * collation isn't known here.
@@ -1358,14 +1358,14 @@ private function regexp_compile( $pattern, $match_type, $binary_mode = false, $w
13581358 /**
13591359 * Translate a MySQL regex pattern to PCRE syntax.
13601360 *
1361- * Splits the pattern into quoted text, escapes, POSIX classes, and other characters,
1362- * so that a translation applies only to a whole token.
1361+ * Splits the pattern into quoted text, sets, escapes, POSIX classes, and other
1362+ * characters, so that a translation applies only to a whole token.
13631363 *
13641364 * @param string $pattern The regex pattern.
13651365 * @param bool $case_insensitive Whether the pattern ignores case by default.
13661366 * @param bool $without_g Whether "\G" never matches.
13671367 *
1368- * @throws Exception If the pattern can't be split.
1368+ * @throws Exception If the pattern can't be split, or it has an invalid set .
13691369 * @return string Pattern safe to wrap in slash delimiters.
13701370 */
13711371 private function regexp_translate_pattern ( $ pattern , $ case_insensitive , $ without_g ) {
@@ -1410,12 +1410,24 @@ private function regexp_translate_pattern( $pattern, $case_insensitive, $without
14101410 );
14111411 }
14121412
1413- // A property name stops at a backslash, so that a long run of "\p{" doesn't
1414- // scan the rest of the pattern each time.
1413+ /*
1414+ * A set, with its nested sets, is one token, as ICU's sets differ from PCRE's.
1415+ * ICU reads a POSIX class outside of a set, as in "[:alpha:]", as a set.
1416+ *
1417+ * A property name stops at a backslash, so that a long run of "\p{" doesn't
1418+ * scan the rest of the pattern each time.
1419+ */
14151420 $ translated = preg_replace_callback (
1416- '/ \\\\Q.*?(?: \\\\E|\z)|\[:\^?\w+:\]| \\\\[pP]\{[^} \\\\]*\}| \\\\.|[\/\0]/s ' ,
1417- function ( $ parts ) use ( $ tokens ) {
1418- return $ this ->regexp_translate_token ( $ parts [0 ], $ tokens );
1421+ '/ \\\\Q.*?(?: \\\\E|\z)|(\[(?!:)\^?\]?(?: \\\\Q.*?(?: \\\\E|\z)| \\\\.|\[:\^?\w+:\]|(?1)|[^ \\\\\[\]])*\])|\[:\^?\w+:\]| \\\\[pP]\{[^} \\\\]*\}| \\\\.|[\/\0]/s ' ,
1422+ function ( $ parts ) use ( $ pattern , $ tokens ) {
1423+ $ token = $ parts [0 ];
1424+ if ( isset ( $ parts [1 ] ) ) {
1425+ return $ this ->regexp_translate_set ( $ token , $ pattern , $ tokens );
1426+ }
1427+ if ( '[ ' === $ token [0 ] ) {
1428+ return '[ ' . ( $ tokens [ $ token ] ?? $ token ) . '] ' ;
1429+ }
1430+ return $ this ->regexp_translate_token ( $ token , $ tokens );
14191431 },
14201432 $ pattern
14211433 );
@@ -1428,6 +1440,178 @@ function ( $parts ) use ( $tokens ) {
14281440 return $ translated ;
14291441 }
14301442
1443+ /**
1444+ * Translate an ICU set, like "[a-z--[aeiou]]", to PCRE.
1445+ *
1446+ * ICU sets can nest, and they have operations that PCRE classes lack. "--" and "&&"
1447+ * subtract or intersect the items up to the next of them. A single "-" or "&" between
1448+ * two sets subtracts or intersects only the next set. Elsewhere, "-" and "&" are
1449+ * literal, unless "-" makes a range. A set without operations stays a PCRE class,
1450+ * and the others become lookaheads, like "(?:(?![aeiou])[a-z])".
1451+ *
1452+ * @param string $set The set, with its brackets.
1453+ * @param string $pattern The regex pattern, for error messages.
1454+ * @param string[] $tokens Translations of tokens.
1455+ *
1456+ * @throws Exception If ICU rejects the set.
1457+ * @return string The PCRE equivalent, which matches one character.
1458+ */
1459+ private function regexp_translate_set ( $ set , $ pattern , $ tokens ) {
1460+ // An escape with its argument is one token, and so is a character, also when the
1461+ // set isn't valid UTF-8.
1462+ preg_match_all (
1463+ '/ \\\\Q.*?(?: \\\\E|\z)| \\\\(?:[pP](?:\{[^} \\\\]*\}|\w)|x(?:\{[^} \\\\]*\}|[0-9a-fA-F]{1,2})|N\{[^} \\\\]*\}|c.|.)|\[:\^?\w+:\]|\[\^?|&&|--|[\xC0-\xFF][\x80-\xBF]*|./s ' ,
1464+ $ set ,
1465+ $ parts
1466+ );
1467+ $ position = 0 ;
1468+ return $ this ->regexp_set_fragment ( $ this ->regexp_parse_set ( $ parts [0 ], $ position , $ pattern , $ tokens ) );
1469+ }
1470+
1471+ /**
1472+ * Parse an ICU set from its tokens.
1473+ *
1474+ * @param string[] $parts The tokens of the set.
1475+ * @param int $position The position of the set's "[" or "[^", moved past its "]".
1476+ * @param string $pattern The regex pattern, for error messages.
1477+ * @param string[] $tokens Translations of tokens.
1478+ *
1479+ * @throws Exception If ICU rejects the set.
1480+ * @return array The set, as array( PCRE class body or fragment, whether it's a class body ).
1481+ */
1482+ private function regexp_parse_set ( array $ parts , &$ position , $ pattern , $ tokens ) {
1483+ // An escape is translated, and another character is escaped if PCRE needs it.
1484+ $ literal = function ( $ part ) use ( $ tokens ) {
1485+ return '\\' === $ part [0 ] ? $ this ->regexp_translate_token ( $ part , $ tokens ) : ( $ tokens [ $ part ] ?? preg_quote ( $ part ) );
1486+ };
1487+
1488+ $ negated = '[^ ' === $ parts [ $ position ++ ];
1489+ $ result = null ;
1490+ $ operator = null ;
1491+ $ terms = array ();
1492+ $ types = array ();
1493+ for ( $ first = true ; ; $ first = false ) {
1494+ $ part = $ parts [ $ position ++ ] ?? null ;
1495+ $ next = $ parts [ $ position ] ?? '' ;
1496+ $ last = count ( $ terms ) - 1 ;
1497+
1498+ // ICU reads "]" right after "[" or "[^" literally.
1499+ if ( null === $ part || ( '] ' === $ part && ! $ first ) ) {
1500+ if ( null === $ part || ! $ terms ) {
1501+ throw new Exception ( "Invalid regular expression: $ pattern. " );
1502+ }
1503+ break ;
1504+ }
1505+ if ( '-- ' === $ part || '&& ' === $ part ) {
1506+ if ( ! $ terms ) {
1507+ throw new Exception ( "Invalid regular expression: $ pattern. " );
1508+ }
1509+ $ right = $ this ->regexp_set_union ( $ terms );
1510+ $ result = null === $ result ? $ right : $ this ->regexp_set_operation ( $ result , $ operator , $ right );
1511+ $ operator = $ part ;
1512+ $ terms = array ();
1513+ $ types = array ();
1514+ continue ;
1515+ }
1516+
1517+ // A single "-" or "&" between two sets subtracts or intersects only the next one.
1518+ if ( ( '- ' === $ part || '& ' === $ part ) && $ last >= 0 && 'set ' === $ types [ $ last ] && '[ ' === substr ( $ next , 0 , 1 ) ) {
1519+ if ( '[: ' === substr ( $ next , 0 , 2 ) ) {
1520+ $ right = array ( $ tokens [ $ next ] ?? $ next , true );
1521+ ++$ position ;
1522+ } else {
1523+ $ right = $ this ->regexp_parse_set ( $ parts , $ position , $ pattern , $ tokens );
1524+ }
1525+ $ terms [ $ last ] = $ this ->regexp_set_operation ( $ terms [ $ last ], '- ' === $ part ? '-- ' : '&& ' , $ right );
1526+ continue ;
1527+ }
1528+
1529+ // A "-" between two characters makes a range. Another class can't end a range.
1530+ if ( '- ' === $ part && $ last >= 0 && 'literal ' === $ types [ $ last ] && ! in_array ( $ next , array ( '' , '] ' , '-- ' , '&& ' ), true ) && '[ ' !== $ next [0 ] ) {
1531+ if ( preg_match ( '/^ \\\\[pPdDsSwWhHvV]/ ' , $ next ) ) {
1532+ throw new Exception ( "Invalid regular expression: $ pattern. " );
1533+ }
1534+ $ terms [ $ last ] = array ( $ terms [ $ last ][0 ] . '- ' . $ literal ( $ next ), true );
1535+ $ types [ $ last ] = 'range ' ;
1536+ ++$ position ;
1537+ continue ;
1538+ }
1539+
1540+ if ( '[ ' === $ part [0 ] && '[: ' !== substr ( $ part , 0 , 2 ) ) {
1541+ --$ position ;
1542+ $ terms [] = $ this ->regexp_parse_set ( $ parts , $ position , $ pattern , $ tokens );
1543+ $ types [] = 'set ' ;
1544+ } elseif ( preg_match ( '/^(?:\[:| \\\\[pP])/ ' , $ part ) ) {
1545+ $ terms [] = array ( $ tokens [ $ part ] ?? $ part , true );
1546+ $ types [] = 'set ' ;
1547+ } elseif ( preg_match ( '/^ \\\\[dDsSwWhHvV]$/ ' , $ part ) ) {
1548+ $ terms [] = array ( $ part , true );
1549+ $ types [] = 'escape ' ;
1550+ } else {
1551+ $ terms [] = array ( $ literal ( $ part ), true );
1552+ $ types [] = 'literal ' ;
1553+ }
1554+ }
1555+
1556+ $ right = $ this ->regexp_set_union ( $ terms );
1557+ $ set = null === $ result ? $ right : $ this ->regexp_set_operation ( $ result , $ operator , $ right );
1558+ if ( ! $ negated ) {
1559+ return $ set ;
1560+ }
1561+ return array ( $ set [1 ] ? '[^ ' . $ set [0 ] . '] ' : '(?:(?! ' . $ set [0 ] . ')[ \\s \\S]) ' , false );
1562+ }
1563+
1564+ /**
1565+ * Join the terms of a set into one class, or into alternatives if a term isn't a class.
1566+ *
1567+ * @param array[] $terms The terms, as returned by regexp_parse_set().
1568+ *
1569+ * @return array The union, as array( PCRE class body or fragment, whether it's a class body ).
1570+ */
1571+ private function regexp_set_union ( array $ terms ) {
1572+ $ body = '' ;
1573+ $ alternatives = array ();
1574+ foreach ( $ terms as $ term ) {
1575+ if ( $ term [1 ] ) {
1576+ $ body .= $ term [0 ];
1577+ } else {
1578+ $ alternatives [] = $ term [0 ];
1579+ }
1580+ }
1581+ if ( ! $ alternatives ) {
1582+ return array ( $ body , true );
1583+ }
1584+ if ( '' !== $ body ) {
1585+ array_unshift ( $ alternatives , "[ $ body] " );
1586+ }
1587+ return array ( '(?: ' . implode ( '| ' , $ alternatives ) . ') ' , false );
1588+ }
1589+
1590+ /**
1591+ * Subtract or intersect two sets.
1592+ *
1593+ * @param array $left The left set, as returned by regexp_parse_set().
1594+ * @param string $operator "--" or "&&".
1595+ * @param array $right The right set.
1596+ *
1597+ * @return array The result, as a PCRE fragment.
1598+ */
1599+ private function regexp_set_operation ( array $ left , $ operator , array $ right ) {
1600+ $ assertion = '-- ' === $ operator ? '(?! ' : '(?= ' ;
1601+ return array ( '(?: ' . $ assertion . $ this ->regexp_set_fragment ( $ right ) . ') ' . $ this ->regexp_set_fragment ( $ left ) . ') ' , false );
1602+ }
1603+
1604+ /**
1605+ * Get the PCRE fragment of a set.
1606+ *
1607+ * @param array $set The set, as returned by regexp_parse_set().
1608+ *
1609+ * @return string The PCRE fragment, which matches one character.
1610+ */
1611+ private function regexp_set_fragment ( array $ set ) {
1612+ return $ set [1 ] ? "[ {$ set [0 ]}] " : $ set [0 ];
1613+ }
1614+
14311615 /**
14321616 * Translate a token of a pattern.
14331617 *
0 commit comments