@@ -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 );
0 commit comments