Skip to content

Commit 8fdc5ae

Browse files
committed
Accept ICU's names of Unicode properties
ICU takes the long names of general categories, like "\p{Lowercase_Letter}", ignores case, spaces, hyphens, and underscores in names, and takes a "gc=" prefix, and an "Is" prefix, like Java. Its POSIX syntax takes any property, like "[:Lu:]". PCRE has only the short names of general categories, and only its own POSIX classes. Translate general categories to their short names, and drop the "Is" prefix of other properties, like scripts, which go to PCRE.
1 parent 7efa564 commit 8fdc5ae

6 files changed

Lines changed: 234 additions & 420 deletions

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

Lines changed: 98 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -135,6 +135,58 @@ public static function register_for( $pdo ): self {
135135
*/
136136
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}';
137137

138+
/**
139+
* General categories by their long names and aliases, as ICU matches them: in
140+
* lowercase, without spaces, hyphens, and underscores. PCRE has only short names.
141+
*/
142+
private const REGEXP_GENERAL_CATEGORIES = array(
143+
'other' => 'C',
144+
'control' => 'Cc',
145+
'cntrl' => 'Cc',
146+
'format' => 'Cf',
147+
'unassigned' => 'Cn',
148+
'privateuse' => 'Co',
149+
'surrogate' => 'Cs',
150+
'letter' => 'L',
151+
'casedletter' => 'L&',
152+
'lowercaseletter' => 'Ll',
153+
'modifierletter' => 'Lm',
154+
'otherletter' => 'Lo',
155+
'titlecaseletter' => 'Lt',
156+
'uppercaseletter' => 'Lu',
157+
'mark' => 'M',
158+
'combiningmark' => 'M',
159+
'spacingmark' => 'Mc',
160+
'enclosingmark' => 'Me',
161+
'nonspacingmark' => 'Mn',
162+
'number' => 'N',
163+
'decimalnumber' => 'Nd',
164+
'digit' => 'Nd',
165+
'letternumber' => 'Nl',
166+
'othernumber' => 'No',
167+
'punctuation' => 'P',
168+
'punct' => 'P',
169+
'connectorpunctuation' => 'Pc',
170+
'dashpunctuation' => 'Pd',
171+
'closepunctuation' => 'Pe',
172+
'finalpunctuation' => 'Pf',
173+
'initialpunctuation' => 'Pi',
174+
'otherpunctuation' => 'Po',
175+
'openpunctuation' => 'Ps',
176+
'symbol' => 'S',
177+
'currencysymbol' => 'Sc',
178+
'modifiersymbol' => 'Sk',
179+
'mathsymbol' => 'Sm',
180+
'othersymbol' => 'So',
181+
'separator' => 'Z',
182+
'lineseparator' => 'Zl',
183+
'paragraphseparator' => 'Zp',
184+
'spaceseparator' => 'Zs',
185+
);
186+
187+
/** The POSIX classes of PCRE. */
188+
private const REGEXP_POSIX_CLASSES = array( 'alnum', 'alpha', 'ascii', 'blank', 'cntrl', 'digit', 'graph', 'lower', 'print', 'punct', 'space', 'upper', 'word', 'xdigit' );
189+
138190
/** @var string[] Compiled PCRE patterns, keyed by byte mode, "\G" variant, match type, and pattern. */
139191
private $regexp_cache = array();
140192

@@ -1265,9 +1317,9 @@ private function regexp_check_match_type( $function_name, $match_type ) {
12651317
* (UTF-8) modifier is applied unless byte-oriented matching is requested.
12661318
* Case-insensitive is the default.
12671319
*
1268-
* MySQL uses ICU, which differs from PCRE in some syntax, like `\N{...}` and
1269-
* property names. The known failures of the REGEXP test suites list these cases.
1270-
* Other known differences:
1320+
* MySQL uses ICU, which differs from PCRE in some syntax, like `\N{...}`, Unicode
1321+
* blocks, and Java properties. The known failures of the REGEXP test suites list
1322+
* these cases. Other known differences:
12711323
*
12721324
* - Text matching ignores case unless the `c` flag is passed, as the column
12731325
* collation isn't known here.
@@ -1460,7 +1512,7 @@ function ( $parts ) use ( $pattern, $tokens ) {
14601512
return $this->regexp_translate_set( $token, $pattern, $tokens );
14611513
}
14621514
if ( '[' === $token[0] ) {
1463-
return '[' . ( $tokens[ $token ] ?? $token ) . ']';
1515+
return '[' . $this->regexp_translate_property( $token, $tokens ) . ']';
14641516
}
14651517
return $this->regexp_translate_token( $token, $tokens );
14661518
},
@@ -1552,7 +1604,7 @@ private function regexp_parse_set( array $parts, &$position, $pattern, $tokens )
15521604
// A single "-" or "&" between two sets subtracts or intersects only the next one.
15531605
if ( ( '-' === $part || '&' === $part ) && $last >= 0 && 'set' === $types[ $last ] && '[' === substr( $next, 0, 1 ) ) {
15541606
if ( '[:' === substr( $next, 0, 2 ) ) {
1555-
$right = array( $tokens[ $next ] ?? $next, true );
1607+
$right = array( $this->regexp_translate_property( $next, $tokens ), true );
15561608
++$position;
15571609
} else {
15581610
$right = $this->regexp_parse_set( $parts, $position, $pattern, $tokens );
@@ -1577,7 +1629,7 @@ private function regexp_parse_set( array $parts, &$position, $pattern, $tokens )
15771629
$terms[] = $this->regexp_parse_set( $parts, $position, $pattern, $tokens );
15781630
$types[] = 'set';
15791631
} elseif ( preg_match( '/^(?:\[:|\\\\[pP])/', $part ) ) {
1580-
$terms[] = array( $tokens[ $part ] ?? $part, true );
1632+
$terms[] = array( $this->regexp_translate_property( $part, $tokens ), true );
15811633
$types[] = 'set';
15821634
} elseif ( preg_match( '/^\\\\[dDsSwWhHvV]$/', $part ) ) {
15831635
// A translated class, like "[\p{L}...]", joins the set, and a negated one is an alternative.
@@ -1676,6 +1728,9 @@ private function regexp_translate_token( $token, $tokens, $in_set = false ) {
16761728
if ( 2 === strlen( $token ) && '\\' === $token[0] && false !== strpos( $letters, $token[1] ) ) {
16771729
return $token[1];
16781730
}
1731+
if ( preg_match( '/^\\\\[pP]\{/', $token ) ) {
1732+
return $this->regexp_translate_property( $token, $tokens );
1733+
}
16791734
if ( isset( $tokens[ $token ] ) ) {
16801735
return $tokens[ $token ];
16811736
}
@@ -1693,6 +1748,43 @@ private function regexp_translate_token( $token, $tokens, $in_set = false ) {
16931748
return sprintf( '\\x{%X}', $code );
16941749
}
16951750

1751+
/**
1752+
* Translate a property, like "\p{Lowercase_Letter}" or "[:Lu:]", to PCRE.
1753+
*
1754+
* ICU takes the long names of general categories, ignores case, spaces, hyphens, and
1755+
* underscores in names, and takes a "gc=" prefix, and an "Is" prefix, like Java. It
1756+
* takes any property in the POSIX syntax. Other names, like scripts, go to PCRE.
1757+
*
1758+
* @param string $token The property, like "\p{...}", "\P{...}", or "[:...:]".
1759+
* @param string[] $tokens Translations of tokens.
1760+
*
1761+
* @return string The PCRE equivalent, for a set.
1762+
*/
1763+
private function regexp_translate_property( $token, $tokens ) {
1764+
if ( ! preg_match( '/^(?:\\\\([pP])\{(.*)\}|\[:(\^?)(.*):\])$/s', $token, $parts ) ) {
1765+
return $token;
1766+
}
1767+
$negated = 'P' === $parts[1] || '^' === ( $parts[3] ?? '' );
1768+
$name = '' === $parts[1] ? $parts[4] : $parts[2];
1769+
$loose = preg_replace( '/^(?:is|gc=|generalcategory=)(?=.)/', '', strtolower( str_replace( array( ' ', '-', '_' ), '', $name ) ) );
1770+
1771+
if ( '[' === $token[0] && in_array( $loose, self::REGEXP_POSIX_CLASSES, true ) ) {
1772+
$class = '[:' . ( $negated ? '^' : '' ) . $loose . ':]';
1773+
} else {
1774+
if ( isset( self::REGEXP_GENERAL_CATEGORIES[ $loose ] ) ) {
1775+
$name = self::REGEXP_GENERAL_CATEGORIES[ $loose ];
1776+
} elseif ( in_array( ucfirst( $loose ), self::REGEXP_GENERAL_CATEGORIES, true ) ) {
1777+
$name = ucfirst( $loose );
1778+
} else {
1779+
$name = preg_replace( '/^is(?=.)/i', '', $name );
1780+
}
1781+
$class = ( $negated ? '\\P{' : '\\p{' ) . $name . '}';
1782+
}
1783+
1784+
// The translations of [:upper:] and similar classes apply to their other names too.
1785+
return $tokens[ $class ] ?? $class;
1786+
}
1787+
16961788
/**
16971789
* Check whether a string is valid UTF-8.
16981790
*

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

Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -383,6 +383,7 @@ public static function regexpLikeErrors() {
383383
'invalid pattern' => array( "REGEXP_LIKE('abc', '(abc')", 'Invalid regular expression: (abc.' ),
384384
'short escape' => array( "REGEXP_LIKE('u004', '\\\\u004')", 'Invalid regular expression: \\u004.' ),
385385
'bad property' => array( "REGEXP_LIKE('a', '\\\\p{Foo}')", 'Invalid regular expression: \\p{Foo}.' ),
386+
'property with /' => array( "REGEXP_LIKE('a', '\\\\p{a/i}')", 'Invalid regular expression: \\p{a/i}.' ),
386387
'extra argument' => array(
387388
"REGEXP_LIKE('abc', 'abc', 'c', 'extra')",
388389
'SQLSTATE[HY000]: General error: 1 wrong number of arguments to function REGEXP_LIKE()',
@@ -1600,6 +1601,7 @@ public function testRegexpBigIntegerColumn() {
16001601
* @dataProvider regexpCodePointEscapeCases
16011602
* @dataProvider regexpLiteralEscapeCases
16021603
* @dataProvider regexpWordCases
1604+
* @dataProvider regexpPropertyCases
16031605
*/
16041606
public function testRegexpIcuSyntax( $pattern, $subject, $expected ) {
16051607
$this->assertQuery(
@@ -1737,6 +1739,31 @@ public static function regexpWordCases() {
17371739
);
17381740
}
17391741

1742+
public static function regexpPropertyCases() {
1743+
return array(
1744+
'long name' => array( '\\p{Lowercase_Letter}+', 'aBc', '<a>B<c>' ),
1745+
'long name with spaces' => array( '\\p{lowercase letter}+', 'aBc', '<a>B<c>' ),
1746+
'long name in capitals' => array( '\\p{LOWERCASELETTER}+', 'aBc', '<a>B<c>' ),
1747+
'long name with hyphens' => array( '\\p{lowercase-letter}+', 'aBc', '<a>B<c>' ),
1748+
'negated long name' => array( '\\P{Uppercase_Letter}+', 'aBc', '<a>B<c>' ),
1749+
'short name in lowercase' => array( '\\p{lu}+', 'aBc', 'a<B>c' ),
1750+
'"Is" prefix' => array( '\\p{IsLetter}+', 'a1b', '<a>1<b>' ),
1751+
'"Is" and a short name' => array( '\\p{IsLu}+', 'aBc', 'a<B>c' ),
1752+
'"gc=" prefix' => array( '\\p{gc=Lu}+', 'aBc', 'a<B>c' ),
1753+
'"General_Category="' => array( '\\p{General_Category=Lu}+', 'aBc', 'a<B>c' ),
1754+
'alias "punct"' => array( '\\p{punct}+', 'a.,b+', 'a<.,>b+' ),
1755+
'alias "digit"' => array( '\\p{IsDigit}+', 'a12²', 'a<12>²' ),
1756+
'cased letters' => array( '\\p{Cased_Letter}+', 'aB1', '<aB>1' ),
1757+
'property in POSIX syntax' => array( '[:Lu:]+', 'aBc', 'a<B>c' ),
1758+
'long name in POSIX' => array( '[:Punctuation:]+', 'a.b', 'a<.>b' ),
1759+
'negated POSIX property' => array( '[:^Lu:]+', 'aBc', '<a>B<c>' ),
1760+
'POSIX property in a set' => array( '[[:Lu:]x]+', 'aBxc', 'a<Bx>c' ),
1761+
'POSIX class in capitals' => array( '[:Alpha:]+', 'a1b', '<a>1<b>' ),
1762+
'script' => array( '\\p{Greek}+', 'aαβ', 'a<αβ>' ),
1763+
'"Is" and a script' => array( '\\p{IsGreek}+', 'aαβ', 'a<αβ>' ),
1764+
);
1765+
}
1766+
17401767
/**
17411768
* @dataProvider regexpInvalidSetCases
17421769
*/
@@ -1799,6 +1826,8 @@ public static function regexpPosixCaseClassCases() {
17991826
'\\p{Lu} in a set' => array( "REGEXP_LIKE('a', '[\\\\p{Lu}]')", '1' ),
18001827
'\\p{Lu} in a negated set' => array( "REGEXP_LIKE('a', '[^\\\\p{Lu}]')", '0' ),
18011828
'\\p{Lu}, intersection' => array( "REGEXP_LIKE('a', '^[\\\\p{Lu}&&[a-c]]\$')", '1' ),
1829+
'long name, lowercase' => array( "REGEXP_LIKE('a', '\\\\p{Uppercase_Letter}')", '1' ),
1830+
'capitals, lowercase' => array( "REGEXP_LIKE('a', '[[:Upper:]]')", '1' ),
18021831
'\\p{Lu}, REGEXP_REPLACE' => array( "REGEXP_REPLACE('aB1', '\\\\p{Lu}', 'x')", 'xx1' ),
18031832
'\\p{Ll}, operator' => array( "'A' REGEXP '\\\\p{Ll}'", '1' ),
18041833
'\\p{Lu}, c flag' => array( "REGEXP_LIKE('a', '\\\\p{Lu}', 'c')", '0' ),
@@ -1849,6 +1878,7 @@ public static function regexpPosixPunctCases() {
18491878
'negated class, symbol' => array( "REGEXP_LIKE('+', '[[:^punct:]]')", '1' ),
18501879
'with other set members' => array( "REGEXP_LIKE('+', '^[[:punct:]+]\$')", '1' ),
18511880
'outside of a set' => array( "REGEXP_LIKE('.', '[:punct:]')", '1' ),
1881+
'capitals, symbol' => array( "REGEXP_LIKE('+', '[[:PUNCT:]]')", '0' ),
18521882
'REGEXP_REPLACE' => array( "REGEXP_REPLACE('a+b.c\$d,e=f!g', '[[:punct:]]', '')", 'a+bc$de=fg' ),
18531883
'operator, punctuation' => array( "'.' REGEXP '[[:punct:]]'", '1' ),
18541884
'operator, symbol' => array( "'+' REGEXP '[[:punct:]]'", '0' ),

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

Lines changed: 26 additions & 103 deletions
Original file line numberDiff line numberDiff line change
@@ -66,62 +66,7 @@ protected function get_known_failures(): array {
6666
'mysql-server-suite:12374',
6767
'mysql-server-suite:12375',
6868
'mysql-server-suite:12376',
69-
'mysql-server-suite:12455',
70-
'mysql-server-suite:12456',
71-
'mysql-server-suite:12457',
72-
'mysql-server-suite:12458',
73-
'mysql-server-suite:12459',
74-
'mysql-server-suite:12460',
75-
'mysql-server-suite:12461',
76-
'mysql-server-suite:12462',
77-
'mysql-server-suite:12463',
78-
'mysql-server-suite:12464',
79-
'mysql-server-suite:12465',
80-
'mysql-server-suite:12466',
81-
'mysql-server-suite:12467',
82-
'mysql-server-suite:12468',
83-
'mysql-server-suite:12469',
84-
'mysql-server-suite:12470',
85-
'mysql-server-suite:12471',
86-
'mysql-server-suite:12472',
87-
'mysql-server-suite:12473',
88-
'mysql-server-suite:12474',
89-
'mysql-server-suite:12475',
90-
'mysql-server-suite:12476',
91-
'mysql-server-suite:12477',
92-
'mysql-server-suite:12478',
9369
'mysql-server-suite:12479',
94-
'mysql-server-suite:12480',
95-
'mysql-server-suite:12481',
96-
'mysql-server-suite:12482',
97-
'mysql-server-suite:12483',
98-
'mysql-server-suite:12484',
99-
'mysql-server-suite:12485',
100-
'mysql-server-suite:12486',
101-
'mysql-server-suite:12487',
102-
'mysql-server-suite:12488',
103-
'mysql-server-suite:12489',
104-
'mysql-server-suite:12490',
105-
'mysql-server-suite:12491',
106-
'mysql-server-suite:12492',
107-
'mysql-server-suite:12493',
108-
'mysql-server-suite:12494',
109-
'mysql-server-suite:12495',
110-
'mysql-server-suite:12496',
111-
'mysql-server-suite:12497',
112-
'mysql-server-suite:12498',
113-
'mysql-server-suite:12499',
114-
'mysql-server-suite:12500',
115-
'mysql-server-suite:12501',
116-
'mysql-server-suite:12502',
117-
'mysql-server-suite:12503',
118-
'mysql-server-suite:12504',
119-
'mysql-server-suite:12505',
120-
'mysql-server-suite:12506',
121-
'mysql-server-suite:12507',
122-
'mysql-server-suite:12508',
123-
'mysql-server-suite:12509',
124-
'mysql-server-suite:12510',
12570
'mysql-server-suite:12589',
12671
'mysql-server-suite:12590',
12772
'mysql-server-suite:12591',
@@ -154,8 +99,6 @@ protected function get_known_failures(): array {
15499
'icu-regextst:147',
155100
'icu-regextst:148',
156101
'icu-regextst:149',
157-
'icu-regextst:165',
158-
'icu-regextst:166',
159102
'icu-regextst:219',
160103
'icu-regextst:220',
161104
'icu-regextst:221',
@@ -212,8 +155,6 @@ protected function get_known_failures(): array {
212155
'icu-regextst:720',
213156
'icu-regextst:843',
214157
'icu-regextst:844',
215-
'icu-regextst:941',
216-
'icu-regextst:942',
217158
'icu-regextst:949',
218159
'icu-regextst:950',
219160
'icu-regextst:951',
@@ -340,58 +281,14 @@ protected function get_known_failures(): array {
340281
'icu-regextst:1337',
341282
'icu-regextst:1338',
342283
'icu-regextst:1339',
343-
'icu-regextst:1370',
344-
'icu-regextst:1371',
345-
'icu-regextst:1372',
346-
'icu-regextst:1373',
347-
'icu-regextst:1374',
348-
'icu-regextst:1375',
349-
'icu-regextst:1376',
350-
'icu-regextst:1377',
351-
'icu-regextst:1378',
352-
'icu-regextst:1379',
353-
'icu-regextst:1380',
354-
'icu-regextst:1381',
355-
'icu-regextst:1382',
356-
'icu-regextst:1383',
357284
'icu-regextst:1384',
358-
'icu-regextst:1385',
359285
'icu-regextst:1386',
360-
'icu-regextst:1387',
361286
'icu-regextst:1388',
362-
'icu-regextst:1389',
363287
'icu-regextst:1390',
364-
'icu-regextst:1391',
365288
'icu-regextst:1392',
366289
'icu-regextst:1393',
367290
'icu-regextst:1394',
368291
'icu-regextst:1395',
369-
'icu-regextst:1396',
370-
'icu-regextst:1397',
371-
'icu-regextst:1398',
372-
'icu-regextst:1399',
373-
'icu-regextst:1400',
374-
'icu-regextst:1401',
375-
'icu-regextst:1402',
376-
'icu-regextst:1403',
377-
'icu-regextst:1404',
378-
'icu-regextst:1405',
379-
'icu-regextst:1406',
380-
'icu-regextst:1407',
381-
'icu-regextst:1408',
382-
'icu-regextst:1409',
383-
'icu-regextst:1410',
384-
'icu-regextst:1411',
385-
'icu-regextst:1412',
386-
'icu-regextst:1413',
387-
'icu-regextst:1414',
388-
'icu-regextst:1415',
389-
'icu-regextst:1416',
390-
'icu-regextst:1417',
391-
'icu-regextst:1418',
392-
'icu-regextst:1419',
393-
'icu-regextst:1420',
394-
'icu-regextst:1421',
395292
'icu-regextst:1422',
396293
'icu-regextst:1423',
397294
'icu-regextst:1424',
@@ -549,6 +446,32 @@ protected function get_known_failures(): array {
549446
'mysql-server-suite:12452',
550447
'mysql-server-suite:12453',
551448
'mysql-server-suite:12454',
449+
'icu-regextst:1370',
450+
'icu-regextst:1371',
451+
'icu-regextst:1372',
452+
'icu-regextst:1373',
453+
'icu-regextst:1374',
454+
'icu-regextst:1375',
455+
'icu-regextst:1376',
456+
'icu-regextst:1377',
457+
'icu-regextst:1385',
458+
'icu-regextst:1387',
459+
'icu-regextst:1389',
460+
'icu-regextst:1391',
461+
'icu-regextst:1404',
462+
'icu-regextst:1405',
463+
'icu-regextst:1406',
464+
'icu-regextst:1407',
465+
'icu-regextst:1412',
466+
'icu-regextst:1413',
467+
'icu-regextst:1414',
468+
'icu-regextst:1415',
469+
'icu-regextst:1416',
470+
'icu-regextst:1417',
471+
'icu-regextst:1418',
472+
'icu-regextst:1419',
473+
'icu-regextst:1420',
474+
'icu-regextst:1421',
552475
'icu-regextst:1452',
553476
'icu-regextst:1453'
554477
);

0 commit comments

Comments
 (0)