Skip to content

Commit 1c10cb9

Browse files
committed
Exclude ASCII symbols from [:punct:]
PCRE's [:punct:] also holds ASCII symbols such as + $ = ~, where ICU's holds Unicode punctuation only. REGEXP_REPLACE('a+b.c$d', '[[:punct:]]', '') removed all of them, where MySQL keeps + and $. Replace the class with \p{P}. It also applies to byte-oriented matching, where MySQL reads bytes as Latin-1, so 0xAB counts as punctuation.
1 parent 2a436b6 commit 1c10cb9

2 files changed

Lines changed: 46 additions & 4 deletions

File tree

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

Lines changed: 8 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1371,15 +1371,19 @@ private function regexp_compile( $pattern, $match_type, $binary_mode = false, $w
13711371
private function regexp_translate_pattern( $pattern, $case_insensitive, $without_g ) {
13721372
$tokens = array(
13731373
// The delimiter, and NUL bytes, which PHP before 8.2 rejects in patterns.
1374-
'/' => '\\/',
1375-
"\0" => '\\x00',
1376-
"\\\0" => '\\x00',
1374+
'/' => '\\/',
1375+
"\0" => '\\x00',
1376+
"\\\0" => '\\x00',
1377+
1378+
// PCRE's [:punct:] also holds ASCII symbols like "+", where ICU's doesn't.
1379+
'[:punct:]' => '\\p{P}',
1380+
'[:^punct:]' => '\\P{P}',
13771381

13781382
/*
13791383
* ICU reads "\K" as "K". In PCRE, "\K" moves the start of the match, even to
13801384
* before the place where the search started, like in "(?<=\Ka)".
13811385
*/
1382-
'\\K' => 'K',
1386+
'\\K' => 'K',
13831387
);
13841388
if ( $without_g ) {
13851389
// A position can't be both a word boundary and not one, so "\b\B" never

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

Lines changed: 38 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1628,6 +1628,44 @@ public static function regexpPosixCaseClassCases() {
16281628
);
16291629
}
16301630

1631+
/**
1632+
* @dataProvider regexpPosixPunctCases
1633+
*/
1634+
public function testRegexpPosixPunct( $sql, $expected ) {
1635+
$this->assertQuery( "SELECT $sql AS r" );
1636+
$this->assertSame( $expected, $this->last_result[0]->r );
1637+
}
1638+
1639+
public static function regexpPosixPunctCases() {
1640+
return array(
1641+
// ICU's class holds Unicode punctuation only, without ASCII symbols.
1642+
'ASCII punctuation' => array( "REGEXP_LIKE('.', '[[:punct:]]')", '1' ),
1643+
'plus' => array( "REGEXP_LIKE('+', '[[:punct:]]')", '0' ),
1644+
'dollar' => array( "REGEXP_LIKE('\$', '[[:punct:]]')", '0' ),
1645+
'equals' => array( "REGEXP_LIKE('=', '[[:punct:]]')", '0' ),
1646+
'tilde' => array( "REGEXP_LIKE('~', '[[:punct:]]')", '0' ),
1647+
'non-ASCII punctuation' => array( "REGEXP_LIKE('«', '[[:punct:]]')", '1' ),
1648+
'non-ASCII symbol' => array( "REGEXP_LIKE('€', '[[:punct:]]')", '0' ),
1649+
'c flag' => array( "REGEXP_LIKE('+', '[[:punct:]]', 'c')", '0' ),
1650+
'negated class' => array( "REGEXP_LIKE('.', '[[:^punct:]]')", '0' ),
1651+
'negated class, symbol' => array( "REGEXP_LIKE('+', '[[:^punct:]]')", '1' ),
1652+
'with other set members' => array( "REGEXP_LIKE('+', '^[[:punct:]+]\$')", '1' ),
1653+
'outside of a set' => array( "REGEXP_LIKE('.', '[:punct:]')", '1' ),
1654+
'REGEXP_REPLACE' => array( "REGEXP_REPLACE('a+b.c\$d,e=f!g', '[[:punct:]]', '')", 'a+bc$de=fg' ),
1655+
'operator, punctuation' => array( "'.' REGEXP '[[:punct:]]'", '1' ),
1656+
'operator, symbol' => array( "'+' REGEXP '[[:punct:]]'", '0' ),
1657+
'quoted class' => array( "REGEXP_LIKE('.', '\\\\Q[:punct:]\\\\E')", '0' ),
1658+
);
1659+
}
1660+
1661+
public function testRegexpPosixPunctOnBinaryData() {
1662+
// Bytes that are not valid UTF-8 are read as Latin-1, where 0xAB is "«".
1663+
$this->assertQuery( 'CREATE TABLE blobs (id INT PRIMARY KEY, b BLOB)' );
1664+
$this->assertQuery( "INSERT INTO blobs VALUES (1, X'2BFF'), (2, X'2EFF'), (3, X'AB')" );
1665+
$this->assertQuery( "SELECT b REGEXP '[[:punct:]]' AS r FROM blobs ORDER BY id" );
1666+
$this->assertSame( array( '0', '1', '1' ), array_column( $this->last_result, 'r' ) );
1667+
}
1668+
16311669
public function testInsertDateNow() {
16321670
$this->assertQuery(
16331671
"INSERT INTO _dates (option_name, option_value) VALUES ('first', now());"

0 commit comments

Comments
 (0)