Skip to content

Commit fdb6e46

Browse files
committed
Read escaped letters literally where ICU does
ICU reads an escaped letter that it gives no meaning, like "\y", as the letter. PCRE rejects most of these, and gives others a meaning: "\C" matches one byte of a character, and "[\b]" matches a backspace. Translate these escapes to their letters, which replaces the special case for "\K". The lists of letters, outside of a set and in a set, come from MySQL 8.4. In a set, "\G" is now a letter too. Before, it failed to compile in a search that didn't start where the previous match ended.
1 parent e8aaacb commit fdb6e46

6 files changed

Lines changed: 58 additions & 73 deletions

‎packages/mysql-on-sqlite/src/sqlite/class-wp-sqlite-pdo-user-defined-functions.php‎

Lines changed: 26 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -115,6 +115,19 @@ public static function register_for( $pdo ): self {
115115
*/
116116
private const REGEXP_CODE_POINT_ESCAPE = '\\\\u[dD][89abAB][0-9a-fA-F]{2}\\\\u[dD][c-fC-F][0-9a-fA-F]{2}|\\\\u[0-9a-fA-F]{4}|\\\\U[0-9a-fA-F]{8}|\\\\0(?:[0-3][0-7]{0,2}|[4-7][0-7]?)';
117117

118+
/**
119+
* Letters that ICU reads literally after a backslash, outside of a set. PCRE rejects
120+
* most of them, and reads some differently, like "\K", which moves the start of a
121+
* match, even to before the place where the search started.
122+
*/
123+
private const REGEXP_LITERAL_ESCAPES = 'gijlmoqyCEFIJKLMOTY';
124+
125+
/**
126+
* Letters that ICU reads literally after a backslash, in a set. PCRE rejects most of
127+
* them, and reads some differently, like "\b".
128+
*/
129+
private const REGEXP_LITERAL_SET_ESCAPES = 'bgijklmoqyzABCEFGIJKLMORTXYZ';
130+
118131
/** @var string[] Compiled PCRE patterns, keyed by byte mode, "\G" variant, match type, and pattern. */
119132
private $regexp_cache = array();
120133

@@ -1245,9 +1258,9 @@ private function regexp_check_match_type( $function_name, $match_type ) {
12451258
* (UTF-8) modifier is applied unless byte-oriented matching is requested.
12461259
* Case-insensitive is the default.
12471260
*
1248-
* MySQL uses ICU, which differs from PCRE in some syntax, like ICU-only escapes.
1249-
* The known failures of the REGEXP test suites list these cases. Other known
1250-
* differences:
1261+
* MySQL uses ICU, which differs from PCRE in some syntax, like `\N{...}` and
1262+
* property names. The known failures of the REGEXP test suites list these cases.
1263+
* Other known differences:
12511264
*
12521265
* - Text matching ignores case unless the `c` flag is passed, as the column
12531266
* collation isn't known here.
@@ -1311,9 +1324,9 @@ private function regexp_compile( $pattern, $match_type, $binary_mode = false, $w
13111324
}
13121325

13131326
/*
1314-
* PHP 7 can read an unknown escape as literal text. For "\y", that matches ICU,
1315-
* but ICU rejects "\u004", which has too few digits. The "X" modifier makes all
1316-
* of them errors, like in PHP 8.
1327+
* PHP 7 can read an unknown escape as literal text. ICU rejects the ones that are
1328+
* left after the translation, like "\u004", which has too few digits. The "X"
1329+
* modifier makes them errors, like in PHP 8.
13171330
*/
13181331
$modifiers = $binary_mode ? 'X' : 'uX';
13191332
if ( ! $case_sensitive ) {
@@ -1385,12 +1398,6 @@ private function regexp_translate_pattern( $pattern, $case_insensitive, $without
13851398
// PCRE's [:punct:] also holds ASCII symbols like "+", where ICU's doesn't.
13861399
'[:punct:]' => '\\p{P}',
13871400
'[:^punct:]' => '\\P{P}',
1388-
1389-
/*
1390-
* ICU reads "\K" as "K". In PCRE, "\K" moves the start of the match, even to
1391-
* before the place where the search started, like in "(?<=\Ka)".
1392-
*/
1393-
'\\K' => 'K',
13941401
);
13951402
if ( $without_g ) {
13961403
// A position can't be both a word boundary and not one, so "\b\B" never
@@ -1489,7 +1496,7 @@ private function regexp_translate_set( $set, $pattern, $tokens ) {
14891496
private function regexp_parse_set( array $parts, &$position, $pattern, $tokens ) {
14901497
// An escape is translated, and another character is escaped if PCRE needs it.
14911498
$literal = function ( $part ) use ( $tokens ) {
1492-
return '\\' === $part[0] ? $this->regexp_translate_token( $part, $tokens ) : ( $tokens[ $part ] ?? preg_quote( $part ) );
1499+
return '\\' === $part[0] ? $this->regexp_translate_token( $part, $tokens, true ) : ( $tokens[ $part ] ?? preg_quote( $part ) );
14931500
};
14941501

14951502
$negated = '[^' === $parts[ $position++ ];
@@ -1624,10 +1631,11 @@ private function regexp_set_fragment( array $set ) {
16241631
*
16251632
* @param string $token The token.
16261633
* @param string[] $tokens Translations of tokens.
1634+
* @param bool $in_set Whether the token is in a set.
16271635
*
16281636
* @return string The PCRE equivalent.
16291637
*/
1630-
private function regexp_translate_token( $token, $tokens ) {
1638+
private function regexp_translate_token( $token, $tokens, $in_set = false ) {
16311639
if ( '\\Q' === substr( $token, 0, 2 ) ) {
16321640
// Quoting can't hold the delimiter or NUL bytes, so they are escaped between
16331641
// two quotes. Quoted text that runs to the end gets an "\E", so that a trailing
@@ -1641,6 +1649,10 @@ private function regexp_translate_token( $token, $tokens ) {
16411649
);
16421650
return '\\E' === substr( $token, -2 ) ? $quoted : $quoted . '\\E';
16431651
}
1652+
$letters = $in_set ? self::REGEXP_LITERAL_SET_ESCAPES : self::REGEXP_LITERAL_ESCAPES;
1653+
if ( 2 === strlen( $token ) && '\\' === $token[0] && false !== strpos( $letters, $token[1] ) ) {
1654+
return $token[1];
1655+
}
16441656
if ( isset( $tokens[ $token ] ) ) {
16451657
return $tokens[ $token ];
16461658
}

‎packages/mysql-on-sqlite/tests/WP_MySQL_On_SQLite_Tests.php‎

Lines changed: 32 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -381,18 +381,29 @@ public function testRegexpLikeErrors( $sql, $message ) {
381381
public static function regexpLikeErrors() {
382382
return array(
383383
'invalid pattern' => array( "REGEXP_LIKE('abc', '(abc')", 'Invalid regular expression: (abc.' ),
384-
'unknown escape' => array( "REGEXP_LIKE('y', '\\\\y')", 'Invalid regular expression: \\y.' ),
385384
'short escape' => array( "REGEXP_LIKE('u004', '\\\\u004')", 'Invalid regular expression: \\u004.' ),
386385
'bad property' => array( "REGEXP_LIKE('a', '\\\\p{Foo}')", 'Invalid regular expression: \\p{Foo}.' ),
387-
'operator escape' => array( "'y' REGEXP '\\\\y'", 'Invalid regular expression: \\y.' ),
388-
'binary escape' => array( "X'FF' REGEXP '\\\\y'", 'Invalid regular expression: \\y.' ),
389386
'extra argument' => array(
390387
"REGEXP_LIKE('abc', 'abc', 'c', 'extra')",
391388
'SQLSTATE[HY000]: General error: 1 wrong number of arguments to function REGEXP_LIKE()',
392389
),
393390
);
394391
}
395392

393+
public function testRegexpOperatorLiteralEscape() {
394+
// The REGEXP operator reads "\y" literally too, also when it matches bytes.
395+
$this->assertQuery( "SELECT 'y' REGEXP '\\\\y' AS text_result, X'FF79' REGEXP '\\\\y' AS binary_result" );
396+
$this->assertEquals(
397+
array(
398+
(object) array(
399+
'text_result' => '1',
400+
'binary_result' => '1',
401+
),
402+
),
403+
$this->last_result
404+
);
405+
}
406+
396407
/**
397408
* @dataProvider regexpMatchTypeFlagCases
398409
*/
@@ -1184,6 +1195,7 @@ public static function regexpMatchEndAnchorCases() {
11841195
'REGEXP_REPLACE, second occurrence' => array( "REGEXP_REPLACE('aab', '\\\\Ga', '-', 1, 2)", 'a-b' ),
11851196
'REGEXP_REPLACE, in a comment' => array( "REGEXP_REPLACE('aa', '(?#\\\\G)a', 'x', 2)", 'ax' ),
11861197
'REGEXP_REPLACE, escaped backslash' => array( "REGEXP_REPLACE('a\\\\Gb', '\\\\\\\\G', '-', 2)", 'a-b' ),
1198+
'REGEXP_REPLACE, letter in a set' => array( "REGEXP_REPLACE('aGb', '[\\\\G]', 'x', 2)", 'axb' ),
11871199

11881200
'REGEXP_SUBSTR, after empty match' => array( "REGEXP_SUBSTR('abc', '\\\\G', 1, 2)", null ),
11891201
'REGEXP_SUBSTR, pos' => array( "REGEXP_SUBSTR('abc', '\\\\G.', 2)", null ),
@@ -1584,6 +1596,7 @@ public function testRegexpBigIntegerColumn() {
15841596
/**
15851597
* @dataProvider regexpSetCases
15861598
* @dataProvider regexpCodePointEscapeCases
1599+
* @dataProvider regexpLiteralEscapeCases
15871600
*/
15881601
public function testRegexpIcuSyntax( $pattern, $subject, $expected ) {
15891602
$this->assertQuery(
@@ -1685,6 +1698,22 @@ public static function regexpCodePointEscapeCases() {
16851698
);
16861699
}
16871700

1701+
public static function regexpLiteralEscapeCases() {
1702+
return array(
1703+
'"\\y"' => array( '\\y', 'xy', 'x<y>' ),
1704+
'"\\j" in a set' => array( '[\\j]+', 'jjx', '<jj>x' ),
1705+
'"\\K"' => array( 'a\\Kb', 'abaKb', 'ab<aKb>' ),
1706+
'"\\K" in a lookbehind' => array( '(?<=\\Ka)', 'Kaa', 'Ka<>a' ),
1707+
'"\\C"' => array( '\\C', 'aC', 'a<C>' ),
1708+
'"\\E" outside of quotes' => array( 'a\\Eb', 'abaEb', 'ab<aEb>' ),
1709+
'"\\b" in a set' => array( '[\\b]', 'ab', 'a<b>' ),
1710+
'"\\z" in a set' => array( '[\\z]+', 'azz', 'a<zz>' ),
1711+
'"\\X" in a set' => array( '[\\X]', 'aX', 'a<X>' ),
1712+
'"\\b" outside of a set' => array( '\\bb', 'ab b', 'ab <b>' ),
1713+
'"\\z" outside of a set' => array( 'a\\z', 'aa', 'a<a>' ),
1714+
);
1715+
}
1716+
16881717
/**
16891718
* @dataProvider regexpInvalidSetCases
16901719
*/

‎packages/mysql-on-sqlite/tests/regexp/WP_MySQL_On_SQLite_Regexp_Instr_Tests.php‎

Lines changed: 0 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -158,8 +158,6 @@ protected function get_known_failures(): array {
158158
'icu-regextst:149',
159159
'icu-regextst:165',
160160
'icu-regextst:166',
161-
'icu-regextst:209',
162-
'icu-regextst:210',
163161
'icu-regextst:219',
164162
'icu-regextst:220',
165163
'icu-regextst:221',
@@ -174,10 +172,6 @@ protected function get_known_failures(): array {
174172
'icu-regextst:232',
175173
'icu-regextst:233',
176174
'icu-regextst:234',
177-
'icu-regextst:365',
178-
'icu-regextst:366',
179-
'icu-regextst:367',
180-
'icu-regextst:368',
181175
'icu-regextst:383',
182176
'icu-regextst:384',
183177
'icu-regextst:385',
@@ -218,10 +212,6 @@ protected function get_known_failures(): array {
218212
'icu-regextst:714',
219213
'icu-regextst:719',
220214
'icu-regextst:720',
221-
'icu-regextst:725',
222-
'icu-regextst:726',
223-
'icu-regextst:727',
224-
'icu-regextst:728',
225215
'icu-regextst:843',
226216
'icu-regextst:844',
227217
'icu-regextst:941',
@@ -304,10 +294,6 @@ protected function get_known_failures(): array {
304294
'icu-regextst:1132',
305295
'icu-regextst:1135',
306296
'icu-regextst:1136',
307-
'icu-regextst:1137',
308-
'icu-regextst:1138',
309-
'icu-regextst:1139',
310-
'icu-regextst:1140',
311297
'icu-regextst:1141',
312298
'icu-regextst:1142',
313299
'icu-regextst:1179',

‎packages/mysql-on-sqlite/tests/regexp/WP_MySQL_On_SQLite_Regexp_Like_Tests.php‎

Lines changed: 0 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -156,8 +156,6 @@ protected function get_known_failures(): array {
156156
'icu-regextst:149',
157157
'icu-regextst:165',
158158
'icu-regextst:166',
159-
'icu-regextst:209',
160-
'icu-regextst:210',
161159
'icu-regextst:219',
162160
'icu-regextst:220',
163161
'icu-regextst:221',
@@ -172,10 +170,6 @@ protected function get_known_failures(): array {
172170
'icu-regextst:232',
173171
'icu-regextst:233',
174172
'icu-regextst:234',
175-
'icu-regextst:365',
176-
'icu-regextst:366',
177-
'icu-regextst:367',
178-
'icu-regextst:368',
179173
'icu-regextst:383',
180174
'icu-regextst:384',
181175
'icu-regextst:385',
@@ -216,10 +210,6 @@ protected function get_known_failures(): array {
216210
'icu-regextst:714',
217211
'icu-regextst:719',
218212
'icu-regextst:720',
219-
'icu-regextst:725',
220-
'icu-regextst:726',
221-
'icu-regextst:727',
222-
'icu-regextst:728',
223213
'icu-regextst:843',
224214
'icu-regextst:844',
225215
'icu-regextst:941',
@@ -300,10 +290,6 @@ protected function get_known_failures(): array {
300290
'icu-regextst:1132',
301291
'icu-regextst:1135',
302292
'icu-regextst:1136',
303-
'icu-regextst:1137',
304-
'icu-regextst:1138',
305-
'icu-regextst:1139',
306-
'icu-regextst:1140',
307293
'icu-regextst:1141',
308294
'icu-regextst:1142',
309295
'icu-regextst:1179',

‎packages/mysql-on-sqlite/tests/regexp/WP_MySQL_On_SQLite_Regexp_Replace_Tests.php‎

Lines changed: 0 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -170,8 +170,6 @@ protected function get_known_failures(): array {
170170
'icu-regextst:149',
171171
'icu-regextst:165',
172172
'icu-regextst:166',
173-
'icu-regextst:209',
174-
'icu-regextst:210',
175173
'icu-regextst:217',
176174
'icu-regextst:218',
177175
'icu-regextst:219',
@@ -188,10 +186,6 @@ protected function get_known_failures(): array {
188186
'icu-regextst:232',
189187
'icu-regextst:233',
190188
'icu-regextst:234',
191-
'icu-regextst:365',
192-
'icu-regextst:366',
193-
'icu-regextst:367',
194-
'icu-regextst:368',
195189
'icu-regextst:383',
196190
'icu-regextst:384',
197191
'icu-regextst:385',
@@ -232,10 +226,6 @@ protected function get_known_failures(): array {
232226
'icu-regextst:714',
233227
'icu-regextst:719',
234228
'icu-regextst:720',
235-
'icu-regextst:725',
236-
'icu-regextst:726',
237-
'icu-regextst:727',
238-
'icu-regextst:728',
239229
'icu-regextst:843',
240230
'icu-regextst:844',
241231
'icu-regextst:879',
@@ -325,10 +315,6 @@ protected function get_known_failures(): array {
325315
'icu-regextst:1132',
326316
'icu-regextst:1135',
327317
'icu-regextst:1136',
328-
'icu-regextst:1137',
329-
'icu-regextst:1138',
330-
'icu-regextst:1139',
331-
'icu-regextst:1140',
332318
'icu-regextst:1141',
333319
'icu-regextst:1142',
334320
'icu-regextst:1170',

‎packages/mysql-on-sqlite/tests/regexp/WP_MySQL_On_SQLite_Regexp_Substr_Tests.php‎

Lines changed: 0 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -158,8 +158,6 @@ protected function get_known_failures(): array {
158158
'icu-regextst:149',
159159
'icu-regextst:165',
160160
'icu-regextst:166',
161-
'icu-regextst:209',
162-
'icu-regextst:210',
163161
'icu-regextst:219',
164162
'icu-regextst:220',
165163
'icu-regextst:221',
@@ -174,10 +172,6 @@ protected function get_known_failures(): array {
174172
'icu-regextst:232',
175173
'icu-regextst:233',
176174
'icu-regextst:234',
177-
'icu-regextst:365',
178-
'icu-regextst:366',
179-
'icu-regextst:367',
180-
'icu-regextst:368',
181175
'icu-regextst:383',
182176
'icu-regextst:384',
183177
'icu-regextst:385',
@@ -218,10 +212,6 @@ protected function get_known_failures(): array {
218212
'icu-regextst:714',
219213
'icu-regextst:719',
220214
'icu-regextst:720',
221-
'icu-regextst:725',
222-
'icu-regextst:726',
223-
'icu-regextst:727',
224-
'icu-regextst:728',
225215
'icu-regextst:843',
226216
'icu-regextst:844',
227217
'icu-regextst:941',
@@ -304,10 +294,6 @@ protected function get_known_failures(): array {
304294
'icu-regextst:1132',
305295
'icu-regextst:1135',
306296
'icu-regextst:1136',
307-
'icu-regextst:1137',
308-
'icu-regextst:1138',
309-
'icu-regextst:1139',
310-
'icu-regextst:1140',
311297
'icu-regextst:1141',
312298
'icu-regextst:1142',
313299
'icu-regextst:1179',

0 commit comments

Comments
 (0)