Skip to content

Commit a350967

Browse files
committed
Shorten a word boundary next to a word character
A "\b" becomes a condition with three word classes, because PCRE has other word characters than ICU. Next to a literal ASCII letter, digit, or "_" that is not repeated, one side of the boundary is known, so one class is enough: "\bword\b" is now "(?<![…])word(?![…])". The same holds for "\B". Measured on PHP 8.5 with PCRE 10.49, on 5.9 MB of text that does not match: - "\bneedle\b|\bhaystack\b|\bthread\b" takes 7.9 ms, and 54.7 ms before. Without JIT, it takes 40.7 ms, and 685 ms before. - "\bneedle\b" takes 1.7 ms, and 3.1 ms before. - A list of "\bword\b" alternatives compiles with up to 426 words, and with up to 148 before. A boundary next to a group, a set, or a repeated character is unchanged.
1 parent 66cd964 commit a350967

2 files changed

Lines changed: 46 additions & 7 deletions

File tree

‎packages/mysql-on-sqlite/src/icu/class-wp-icu-regex-translator.php‎

Lines changed: 24 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -134,7 +134,8 @@ class WP_ICU_Regex_Translator {
134134
/*
135135
* The tokens of a pattern. An item may have a quantifier, and a position may not.
136136
* TOKEN captures the quantifier (1), the flags that an inline flag group turns on (2)
137-
* and off (3) with its end (4), and text that is no supported token (5).
137+
* and off (3) with its end (4), text that is no supported token (5), and an ASCII
138+
* word character that follows the token and isn't repeated (6).
138139
*/
139140
private const QUOTE = '\\\\Q(?!\\\\E)(?:[^\\\\]++|\\\\(?!E))*+';
140141
private const ESCAPE = '\\\\(?:[pP]\{[^}]*\}|x\{[0-9A-Fa-f]{1,6}\}|x[0-9A-Fa-f]{1,2}|u[0-9A-Fa-f]{4}|U[0-9A-Fa-f]{8}|0[0-3][0-7]{2}|0[0-7]{1,2}|c[\x00-\x7F]|k<[A-Za-z][A-Za-z0-9]*>|[1-9](?!\d)|[^Q0-9])';
@@ -154,7 +155,7 @@ class WP_ICU_Regex_Translator {
154155

155156
// Text that is no supported token: the start of an escape or a group, or a character.
156157
private const OTHER = '\\\\(?:Q\\\\E|\d+)?|\(\?(?:<[=!]|\w+|[^)])?|[\s\S]';
157-
private const TOKEN = '/(?:[^\\\\\[(){}|^$.*+?\/\x00]++|' . self::QUOTE . '(?:\\\\E|\z)|' . self::ESCAPE . '|' . self::POSIX . '|' . self::SET . '|[.)\/\x00])' . self::QUANTIFIER . '|' . self::LOOK_BEHIND . '|' . self::GROUP . '|' . self::BRACE . '|\[\[:[<>]:\]\]|[|^$]|(' . self::OTHER . ')/';
158+
private const TOKEN = '/(?:(?:[^\\\\\[(){}|^$.*+?\/\x00]++|' . self::QUOTE . '(?:\\\\E|\z)|' . self::ESCAPE . '|' . self::POSIX . '|' . self::SET . '|[.)\/\x00])' . self::QUANTIFIER . '|' . self::LOOK_BEHIND . '|' . self::GROUP . '|' . self::BRACE . '|\[\[:[<>]:\]\]|[|^$]|(' . self::OTHER . '))(?=([A-Za-z0-9_](?![*+?{]))?)/';
158159

159160
// The longest pattern to translate. A translation can be many times as long as its
160161
// pattern, and PCRE can compile only few longer patterns, like a long set.
@@ -181,6 +182,9 @@ class WP_ICU_Regex_Translator {
181182
// group that does.
182183
private $is_leading = true;
183184

185+
// Whether the previous token ends with an ASCII word character that isn't repeated.
186+
private $is_after_word = false;
187+
184188
// Whether "\G" never matches.
185189
private $without_g;
186190

@@ -230,16 +234,27 @@ private function translate_token( $parts ) {
230234
$token = $parts[0];
231235
$quantifier = $parts[1] ?? '';
232236
$item = substr( $token, 0, strlen( $token ) - strlen( $quantifier ) );
233-
if ( isset( $parts[5] ) ) {
237+
if ( '' !== ( $parts[5] ?? '' ) ) {
234238
throw new WP_ICU_Regex_Exception( "Unsupported regular expression syntax: $token", self::UNSUPPORTED );
235239
}
236-
$is_leading = $this->is_leading;
237-
$this->is_leading = '|' === $token && ! $this->groups;
240+
$is_leading = $this->is_leading;
241+
$this->is_leading = '|' === $token && ! $this->groups;
242+
$is_after_word = $this->is_after_word;
243+
$this->is_after_word = false;
238244

239245
switch ( $item[0] ) {
240246
case '\\':
241247
// A position, like "\b", can't be repeated.
242248
if ( '' === $quantifier || false === strpos( 'AbBGzZ', $item[1] ) ) {
249+
/*
250+
* A word boundary next to a word character only checks its other side.
251+
* So "\bword\b" needs two word classes and not six, and PCRE can search
252+
* for the first letter of the word.
253+
*/
254+
$is_before_word = isset( $parts[6] );
255+
if ( 'b' === strtolower( $item[1] ) && ! $this->bytes && ( $is_before_word || $is_after_word ) ) {
256+
return '(?' . ( $is_before_word ? '<' : '' ) . ( 'b' === $item[1] ? '!' : '=' ) . '[' . self::WORD . '])';
257+
}
243258
return $this->translate_escape( substr( $item, 1 ), false ) . $quantifier;
244259
}
245260
break;
@@ -268,8 +283,9 @@ private function translate_token( $parts ) {
268283
return '(?#)';
269284
}
270285
// A look-behind has no groups, so its tokens are translated in place.
271-
$inside = self::replace( self::TOKEN, array( $this, 'translate_token' ), substr( $item, 4, -1 ) );
272-
$this->is_leading = false;
286+
$inside = self::replace( self::TOKEN, array( $this, 'translate_token' ), substr( $item, 4, -1 ) );
287+
$this->is_leading = false;
288+
$this->is_after_word = false;
273289
return substr( $item, 0, 4 ) . $inside . ')';
274290
}
275291
/*
@@ -331,6 +347,7 @@ private function translate_token( $parts ) {
331347
case '|':
332348
return '|';
333349
default:
350+
$this->is_after_word = '' === $quantifier && 1 === preg_match( '/[A-Za-z0-9_]$/D', $token );
334351
return $token;
335352
}
336353
throw new WP_ICU_Regex_Exception( "Unsupported regular expression syntax: $token", self::UNSUPPORTED );

‎packages/mysql-on-sqlite/tests/icu/WP_ICU_Regex_Translator_Tests.php‎

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1075,6 +1075,28 @@ public function test_a_leading_dot_star_matches_like_any_other(): void {
10751075
$this->assertSame( '/(?:\G|(?<=[\n\x0B\f\r]))[^\n\x0B\f\r]*a/X', WP_ICU_Regex_Translator::translate( '.*a', '', false, true ) );
10761076
}
10771077

1078+
/**
1079+
* A word boundary next to a word character only checks its other side. The pattern
1080+
* gets shorter, and PCRE can search for the first letter of the word.
1081+
*/
1082+
public function test_a_word_boundary_next_to_a_word_character_checks_one_side(): void {
1083+
$word = '[\p{L}\p{M}\p{Nd}\p{Nl}\p{Pc}\x{200C}\x{200D}\x{24B6}-\x{24E9}\x{1F130}-\x{1F149}\x{1F150}-\x{1F169}\x{1F170}-\x{1F189}]';
1084+
$this->assertSame( "/(?<!$word)word(?!$word)/uX", WP_ICU_Regex_Translator::translate( '\bword\b' ) );
1085+
$this->assertSame( "/(?<=$word)_1(?=$word)/uX", WP_ICU_Regex_Translator::translate( '\B_1\B' ) );
1086+
$this->assertSame( "/a(?<!$word)b|c(?!$word)/uXi", WP_ICU_Regex_Translator::translate( 'a\bb|c\b', 'i' ) );
1087+
$this->assertSame( '/\bword\b/X', WP_ICU_Regex_Translator::translate( '\bword\b', '', false, true ) );
1088+
1089+
// Other neighbors can match a character that is no word character, or nothing.
1090+
foreach ( array( '\b', '\b-', '\bé', '\ba*', '\ba?', '\ba{2}', '\b(a)', '\b[a]', '\b\w', '-\b', 'é\b', 'a*\b', 'a+\b', '(a)\b', '[a]\b', '\x61\b', '(?<=a)\b' ) as $pattern ) {
1091+
$this->assertStringContainsString( "(?(?<=$word)(?!$word)|(?=$word))", WP_ICU_Regex_Translator::translate( $pattern ), $pattern );
1092+
}
1093+
1094+
$this->assertSame( '<cat> concat cat_ écat <cat>', $this->replace_matches( WP_ICU_Regex_Translator::translate( '\bcat\b' ), 'cat concat cat_ écat cat' ) );
1095+
$this->assertSame( 'cat con<cat> é<cat>', $this->replace_matches( WP_ICU_Regex_Translator::translate( '\Bcat' ), 'cat concat écat' ) );
1096+
$this->assertSame( "<k> <\u{212A}> ak", $this->replace_matches( WP_ICU_Regex_Translator::translate( '\bk\b', 'i' ), "k \u{212A} ak" ) );
1097+
$this->assertSame( 'ab', $this->replace_matches( WP_ICU_Regex_Translator::translate( 'a\bb' ), 'ab' ) );
1098+
}
1099+
10781100
public function test_unknown_flags_are_rejected(): void {
10791101
$this->expectException( WP_ICU_Regex_Exception::class );
10801102
WP_ICU_Regex_Translator::translate( 'a', 'x' );

0 commit comments

Comments
 (0)