Skip to content

Commit 7efa564

Browse files
committed
Match ICU's word characters in \w and \b
ICU's word characters are alphabetic ones, all marks, decimal digits, connector punctuation, and the zero-width joiners. PCRE's "\w" misses spacing marks, and before PCRE2 10.43, all marks, so "\w+" split words in Hindi, Thai, or Tamil, and "\b" found boundaries inside them. It also matches other numbers, like "²". Translate "\w", "\W", "\b", and "\B" to ICU's class, which works the same with every PCRE version. Matching bytes keeps PCRE's ASCII class.
1 parent fdb6e46 commit 7efa564

6 files changed

Lines changed: 49 additions & 117 deletions

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

Lines changed: 26 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -128,6 +128,13 @@ public static function register_for( $pdo ): self {
128128
*/
129129
private const REGEXP_LITERAL_SET_ESCAPES = 'bgijklmoqyzABCEFGIJKLMORTXYZ';
130130

131+
/**
132+
* ICU's word characters: alphabetic ones, marks, decimal digits, connector punctuation,
133+
* and the zero-width joiners. Alphabetic characters are letters, letter numbers, and
134+
* letter symbols, like "Ⓐ". PCRE's "\w" misses some marks, and has other numbers.
135+
*/
136+
private const REGEXP_WORD_CLASS = '\p{L}\p{Nl}\p{M}\p{Nd}\p{Pc}\x{200C}\x{200D}\x{24B6}-\x{24E9}\x{1F130}-\x{1F149}\x{1F150}-\x{1F169}\x{1F170}-\x{1F189}';
137+
131138
/** @var string[] Compiled PCRE patterns, keyed by byte mode, "\G" variant, match type, and pattern. */
132139
private $regexp_cache = array();
133140

@@ -1269,6 +1276,8 @@ private function regexp_check_match_type( $function_name, $match_type ) {
12691276
* - Inline case flags don't reach `[:upper:]` and `[:lower:]`, or `\p{Lu}` and
12701277
* `\p{Ll}` on PCRE 10.44 and older. They keep their case in `(?i)[[:upper:]]`
12711278
* with the `c` flag, and in a whole pattern with `(?-i)`.
1279+
* - ICU attaches a combining mark to the character before it, so a mark after a
1280+
* space doesn't start a word for `\b`. Here, it does.
12721281
* - `\G` in a lookbehind doesn't always match where it does in ICU.
12731282
* - Data that isn't valid UTF-8 is matched byte by byte and case-sensitively,
12741283
* like MySQL binary strings. Other binary values match as text, so case
@@ -1351,7 +1360,7 @@ private function regexp_compile( $pattern, $match_type, $binary_mode = false, $w
13511360
} else {
13521361
$newline = '(*ANY)';
13531362
}
1354-
$compiled = '/' . $newline . $this->regexp_translate_pattern( $pattern, ! $case_sensitive, $without_g ) . '/' . $modifiers;
1363+
$compiled = '/' . $newline . $this->regexp_translate_pattern( $pattern, ! $case_sensitive, $binary_mode, $without_g ) . '/' . $modifiers;
13551364

13561365
/*
13571366
* Compile the pattern by matching it against an empty string. Only a compile
@@ -1383,12 +1392,13 @@ private function regexp_compile( $pattern, $match_type, $binary_mode = false, $w
13831392
*
13841393
* @param string $pattern The regex pattern.
13851394
* @param bool $case_insensitive Whether the pattern ignores case by default.
1395+
* @param bool $binary_mode Whether the pattern matches bytes instead of UTF-8 characters.
13861396
* @param bool $without_g Whether "\G" never matches.
13871397
*
13881398
* @throws Exception If the pattern can't be split, or it has an invalid set.
13891399
* @return string Pattern safe to wrap in slash delimiters.
13901400
*/
1391-
private function regexp_translate_pattern( $pattern, $case_insensitive, $without_g ) {
1401+
private function regexp_translate_pattern( $pattern, $case_insensitive, $binary_mode, $without_g ) {
13921402
$tokens = array(
13931403
// The delimiter, and NUL bytes, which PHP before 8.2 rejects in patterns.
13941404
'/' => '\\/',
@@ -1405,6 +1415,17 @@ private function regexp_translate_pattern( $pattern, $case_insensitive, $without
14051415
$tokens['\\G'] = '\\b\\B';
14061416
}
14071417

1418+
// Word boundaries follow ICU's word characters. Bytes keep PCRE's ASCII ones.
1419+
if ( ! $binary_mode ) {
1420+
$word = self::REGEXP_WORD_CLASS;
1421+
$tokens += array(
1422+
'\\w' => "[$word]",
1423+
'\\W' => "[^$word]",
1424+
'\\b' => "(?:(?<=[$word])(?![$word])|(?<![$word])(?=[$word]))",
1425+
'\\B' => "(?:(?<=[$word])(?=[$word])|(?<![$word])(?![$word]))",
1426+
);
1427+
}
1428+
14081429
/*
14091430
* With the "u" modifier, PCRE reads [:upper:] and [:lower:] case-sensitively
14101431
* even when the pattern ignores case, where ICU matches both cases. PCRE 10.44
@@ -1559,7 +1580,9 @@ private function regexp_parse_set( array $parts, &$position, $pattern, $tokens )
15591580
$terms[] = array( $tokens[ $part ] ?? $part, true );
15601581
$types[] = 'set';
15611582
} elseif ( preg_match( '/^\\\\[dDsSwWhHvV]$/', $part ) ) {
1562-
$terms[] = array( $part, true );
1583+
// A translated class, like "[\p{L}...]", joins the set, and a negated one is an alternative.
1584+
$class = $tokens[ $part ] ?? $part;
1585+
$terms[] = '[^' === substr( $class, 0, 2 ) ? array( $class, false ) : array( trim( $class, '[]' ), true );
15631586
$types[] = 'escape';
15641587
} else {
15651588
$terms[] = array( $literal( $part ), true );

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

Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -453,6 +453,8 @@ public static function regexpBinaryDataCases() {
453453
'REGEXP_REPLACE, pos one past end' => array( "X'C485FF'", "REGEXP_REPLACE(b, '\$', 'x', 4)", "\xC4\x85\xFFx" ),
454454
'REGEXP_REPLACE, occurrence' => array( "X'C485FF'", "REGEXP_REPLACE(b, '(.)', '[\$1]', 2, 1)", "\xC4[\x85]\xFF" ),
455455
'REGEXP_REPLACE, empty matches' => array( "X'C485FF'", "REGEXP_REPLACE(b, 'x*', '-')", "-\xC4-\x85-\xFF-" ),
456+
'REGEXP_REPLACE, word characters' => array( "X'61812D62'", "REGEXP_REPLACE(b, '\\\\w', 'x')", "x\x81-x" ),
457+
'REGEXP_REPLACE, word boundaries' => array( "X'61812D62'", "REGEXP_REPLACE(b, '\\\\b', '|')", "|a|\x81-|b|" ),
456458

457459
'REGEXP_SUBSTR, any byte' => array( "X'61FF62'", "REGEXP_SUBSTR(b, 'a.b')", "a\xFFb" ),
458460
'REGEXP_SUBSTR, other case' => array( "X'61FF41'", "REGEXP_SUBSTR(b, 'a.a')", null ),
@@ -1597,6 +1599,7 @@ public function testRegexpBigIntegerColumn() {
15971599
* @dataProvider regexpSetCases
15981600
* @dataProvider regexpCodePointEscapeCases
15991601
* @dataProvider regexpLiteralEscapeCases
1602+
* @dataProvider regexpWordCases
16001603
*/
16011604
public function testRegexpIcuSyntax( $pattern, $subject, $expected ) {
16021605
$this->assertQuery(
@@ -1714,6 +1717,26 @@ public static function regexpLiteralEscapeCases() {
17141717
);
17151718
}
17161719

1720+
public static function regexpWordCases() {
1721+
return array(
1722+
'"\\w" and marks' => array( '\\w+', 'नमस्ते สวัสดี தமிழ்', '<नमस्ते> <สวัสดี> <தமிழ்>' ),
1723+
'"\\b" and marks' => array( '\\bहिन्दी\\b', 'हिन्दी है', '<हिन्दी> है' ),
1724+
'"\\b" and a combining accent' => array( "\\bcafe\u{301}\\b", "a cafe\u{301} b", "a <cafe\u{301}> b" ),
1725+
'"\\w" and other numbers' => array( '\\w', 'x²Ⅻ', '<x>²<Ⅻ>' ),
1726+
'"\\w" and letter symbols' => array( '\\w', 'Ⓐ🄰', '<Ⓐ><🄰>' ),
1727+
'"\\w" and joiners' => array( '\\w+', "ab\u{200C}cd\u{200D}ef", "<ab\u{200C}cd\u{200D}ef>" ),
1728+
'"\\w" and connectors' => array( '\\w', '_‿', '<_><‿>' ),
1729+
'"\\W"' => array( '\\W+', "ab\u{200C}cd ²", "ab\u{200C}cd< ²>" ),
1730+
'"\\b"' => array( '\\b', 'ab cd', '<>ab<> <>cd<>' ),
1731+
'"\\B"' => array( '\\B', 'ab cd', 'a<>b c<>d' ),
1732+
'"\\w" in a set' => array( '[\\w]+', 'ab²', '<ab>²' ),
1733+
'"\\W" in a set' => array( '[\\W]+', 'ab²', 'ab<²>' ),
1734+
'"\\W" in a negated set' => array( '[^\\W]+', 'ab² c', '<ab>² <c>' ),
1735+
'"\\w" in an intersection' => array( '[\\w&&[^a]]+', 'ab²c', 'a<b>²<c>' ),
1736+
'"\\d"' => array( '\\d+', '١٢٣ ²', '<١٢٣> ²' ),
1737+
);
1738+
}
1739+
17171740
/**
17181741
* @dataProvider regexpInvalidSetCases
17191742
*/

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

Lines changed: 0 additions & 29 deletions
Original file line numberDiff line numberDiff line change
@@ -14,8 +14,6 @@ protected function get_known_failures(): array {
1414

1515
// Cases that fail in every environment.
1616
$failures = array(
17-
'mysql-server-suite:7923',
18-
'mysql-server-suite:7924',
1917
'mysql-server-suite:12325',
2018
'mysql-server-suite:12326',
2119
'mysql-server-suite:12327',
@@ -330,12 +328,6 @@ protected function get_known_failures(): array {
330328
'icu-regextst:1262',
331329
'icu-regextst:1263',
332330
'icu-regextst:1264',
333-
'icu-regextst:1267',
334-
'icu-regextst:1268',
335-
'icu-regextst:1269',
336-
'icu-regextst:1270',
337-
'icu-regextst:1271',
338-
'icu-regextst:1272',
339331
'icu-regextst:1301',
340332
'icu-regextst:1302',
341333
'icu-regextst:1330',
@@ -601,27 +593,6 @@ protected function get_known_failures(): array {
601593
);
602594
}
603595

604-
// Before PCRE2 10.43, \w didn't match combining marks, like the vowel signs of "नमस्ते", which ICU's \w does.
605-
if ( version_compare( $pcre_version, '10.43', '<' ) ) {
606-
array_push(
607-
$failures,
608-
'mysql-server-suite:1533',
609-
'mysql-server-suite:1534',
610-
'mysql-server-suite:1553',
611-
'mysql-server-suite:1554',
612-
'mysql-server-suite:1557',
613-
'mysql-server-suite:1558',
614-
'mysql-server-suite:1701',
615-
'mysql-server-suite:1702',
616-
'mysql-server-suite:1721',
617-
'mysql-server-suite:1722',
618-
'mysql-server-suite:1725',
619-
'mysql-server-suite:1726',
620-
'mysql-server-suite:7919',
621-
'mysql-server-suite:7920'
622-
);
623-
}
624-
625596
return $failures;
626597
}
627598
}

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

Lines changed: 0 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -576,19 +576,6 @@ protected function get_known_failures(): array {
576576
);
577577
}
578578

579-
// Before PCRE2 10.43, \w didn't match combining marks, like the vowel signs of "नमस्ते", which ICU's \w does.
580-
if ( version_compare( $pcre_version, '10.43', '<' ) ) {
581-
array_push(
582-
$failures,
583-
'mysql-server-suite:1533',
584-
'mysql-server-suite:1534',
585-
'mysql-server-suite:1701',
586-
'mysql-server-suite:1702',
587-
'mysql-server-suite:7923',
588-
'mysql-server-suite:7924'
589-
);
590-
}
591-
592579
return $failures;
593580
}
594581
}

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

Lines changed: 0 additions & 43 deletions
Original file line numberDiff line numberDiff line change
@@ -14,16 +14,6 @@ protected function get_known_failures(): array {
1414

1515
// Cases that fail in every environment.
1616
$failures = array(
17-
'mysql-server-suite:1529',
18-
'mysql-server-suite:1530',
19-
'mysql-server-suite:1557',
20-
'mysql-server-suite:1558',
21-
'mysql-server-suite:1697',
22-
'mysql-server-suite:1698',
23-
'mysql-server-suite:1725',
24-
'mysql-server-suite:1726',
25-
'mysql-server-suite:7923',
26-
'mysql-server-suite:7924',
2717
'mysql-server-suite:8349',
2818
'mysql-server-suite:8350',
2919
'mysql-server-suite:12325',
@@ -317,8 +307,6 @@ protected function get_known_failures(): array {
317307
'icu-regextst:1136',
318308
'icu-regextst:1141',
319309
'icu-regextst:1142',
320-
'icu-regextst:1170',
321-
'icu-regextst:1171',
322310
'icu-regextst:1179',
323311
'icu-regextst:1180',
324312
'icu-regextst:1181',
@@ -353,12 +341,6 @@ protected function get_known_failures(): array {
353341
'icu-regextst:1262',
354342
'icu-regextst:1263',
355343
'icu-regextst:1264',
356-
'icu-regextst:1267',
357-
'icu-regextst:1268',
358-
'icu-regextst:1269',
359-
'icu-regextst:1270',
360-
'icu-regextst:1271',
361-
'icu-regextst:1272',
362344
'icu-regextst:1301',
363345
'icu-regextst:1302',
364346
'icu-regextst:1330',
@@ -630,31 +612,6 @@ protected function get_known_failures(): array {
630612
);
631613
}
632614

633-
// Before PCRE2 10.43, \w didn't match combining marks, like the vowel signs of "नमस्ते", which ICU's \w does.
634-
if ( version_compare( $pcre_version, '10.43', '<' ) ) {
635-
array_push(
636-
$failures,
637-
'mysql-server-suite:1525',
638-
'mysql-server-suite:1526',
639-
'mysql-server-suite:1533',
640-
'mysql-server-suite:1534',
641-
'mysql-server-suite:1553',
642-
'mysql-server-suite:1554',
643-
'mysql-server-suite:1561',
644-
'mysql-server-suite:1562',
645-
'mysql-server-suite:1693',
646-
'mysql-server-suite:1694',
647-
'mysql-server-suite:1701',
648-
'mysql-server-suite:1702',
649-
'mysql-server-suite:1721',
650-
'mysql-server-suite:1722',
651-
'mysql-server-suite:1729',
652-
'mysql-server-suite:1730',
653-
'mysql-server-suite:7919',
654-
'mysql-server-suite:7920'
655-
);
656-
}
657-
658615
return $failures;
659616
}
660617
}

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

Lines changed: 0 additions & 29 deletions
Original file line numberDiff line numberDiff line change
@@ -14,8 +14,6 @@ protected function get_known_failures(): array {
1414

1515
// Cases that fail in every environment.
1616
$failures = array(
17-
'mysql-server-suite:7923',
18-
'mysql-server-suite:7924',
1917
'mysql-server-suite:12325',
2018
'mysql-server-suite:12326',
2119
'mysql-server-suite:12327',
@@ -330,12 +328,6 @@ protected function get_known_failures(): array {
330328
'icu-regextst:1262',
331329
'icu-regextst:1263',
332330
'icu-regextst:1264',
333-
'icu-regextst:1267',
334-
'icu-regextst:1268',
335-
'icu-regextst:1269',
336-
'icu-regextst:1270',
337-
'icu-regextst:1271',
338-
'icu-regextst:1272',
339331
'icu-regextst:1301',
340332
'icu-regextst:1302',
341333
'icu-regextst:1330',
@@ -601,27 +593,6 @@ protected function get_known_failures(): array {
601593
);
602594
}
603595

604-
// Before PCRE2 10.43, \w didn't match combining marks, like the vowel signs of "नमस्ते", which ICU's \w does.
605-
if ( version_compare( $pcre_version, '10.43', '<' ) ) {
606-
array_push(
607-
$failures,
608-
'mysql-server-suite:1533',
609-
'mysql-server-suite:1534',
610-
'mysql-server-suite:1553',
611-
'mysql-server-suite:1554',
612-
'mysql-server-suite:1557',
613-
'mysql-server-suite:1558',
614-
'mysql-server-suite:1701',
615-
'mysql-server-suite:1702',
616-
'mysql-server-suite:1721',
617-
'mysql-server-suite:1722',
618-
'mysql-server-suite:1725',
619-
'mysql-server-suite:1726',
620-
'mysql-server-suite:7919',
621-
'mysql-server-suite:7920'
622-
);
623-
}
624-
625596
return $failures;
626597
}
627598
}

0 commit comments

Comments
 (0)