Skip to content

Commit 32438c3

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 8f9dee5 commit 32438c3

2 files changed

Lines changed: 42 additions & 6 deletions

File tree

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

Lines changed: 24 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -123,14 +123,15 @@ class WP_ICU_Regex_Translator {
123123

124124
// Catch unrecognized syntax so the error can name the offending token.
125125
private const OTHER = '\\\\(?:Q\\\\E|\d+)?|\(\?(?:<[=!]|\w+|[^)])?|[\s\S]';
126-
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 . ')/';
126+
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_](?![*+?{]))?)/';
127127

128128
// Capture groups in TOKEN.
129129
private const CAPTURE_QUANTIFIER = 1;
130130
private const CAPTURE_FLAGS_ON = 2;
131131
private const CAPTURE_FLAGS_OFF = 3;
132132
private const CAPTURE_FLAG_DELIMITER = 4;
133133
private const CAPTURE_UNSUPPORTED = 5;
134+
private const CAPTURE_NEXT_WORD = 6;
134135

135136
// Cap the input size before expanding tokens such as word boundaries into longer PCRE patterns.
136137
private const MAX_LENGTH = 65535;
@@ -173,6 +174,13 @@ class WP_ICU_Regex_Translator {
173174
*/
174175
private $is_leading = true;
175176

177+
/**
178+
* Whether the previous token ended with an unquantified ASCII word character.
179+
*
180+
* @var bool
181+
*/
182+
private $is_after_word = false;
183+
176184
/**
177185
* Whether \G in the input pattern must fail.
178186
*
@@ -228,16 +236,24 @@ private function translate_token( $parts ) {
228236
$token = $parts[0];
229237
$quantifier = $parts[ self::CAPTURE_QUANTIFIER ] ?? '';
230238
$item = substr( $token, 0, strlen( $token ) - strlen( $quantifier ) );
231-
if ( isset( $parts[ self::CAPTURE_UNSUPPORTED ] ) ) {
239+
if ( '' !== ( $parts[ self::CAPTURE_UNSUPPORTED ] ?? '' ) ) {
232240
throw new WP_ICU_Regex_Exception( "Unsupported regular expression syntax: $token", self::UNSUPPORTED );
233241
}
234-
$is_leading = $this->is_leading;
235-
$this->is_leading = '|' === $token && ! $this->groups;
242+
$is_leading = $this->is_leading;
243+
$this->is_leading = '|' === $token && ! $this->groups;
244+
$is_after_word = $this->is_after_word;
245+
$this->is_after_word = false;
236246

237247
switch ( $item[0] ) {
238248
case '\\':
239249
// A position, like "\b", can't be repeated.
240250
if ( '' === $quantifier || false === strpos( 'AbBGzZ', $item[1] ) ) {
251+
// In "\bword\b", each boundary needs to test only the side facing away from the word.
252+
// Omitting the other tests keeps word lists smaller when PCRE compiles them.
253+
$is_before_word = isset( $parts[ self::CAPTURE_NEXT_WORD ] );
254+
if ( 'b' === strtolower( $item[1] ) && ! $this->bytes && ( $is_before_word || $is_after_word ) ) {
255+
return '(?' . ( $is_before_word ? '<' : '' ) . ( 'b' === $item[1] ? '!' : '=' ) . '[' . self::WORD . '])';
256+
}
241257
return $this->translate_escape( substr( $item, 1 ), false ) . $quantifier;
242258
}
243259
break;
@@ -267,8 +283,9 @@ private function translate_token( $parts ) {
267283
return '(?#)';
268284
}
269285
// LOOK_BEHIND excludes nested groups. Translate its contents with the current flags.
270-
$inside = self::replace( self::TOKEN, array( $this, 'translate_token' ), substr( $item, 4, -1 ) );
271-
$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;
272289
return substr( $item, 0, 4 ) . $inside . ')';
273290
}
274291
// A lookahead consumes no text. An atomic group keeps its first match.
@@ -322,6 +339,7 @@ private function translate_token( $parts ) {
322339
case '|':
323340
return '|';
324341
default:
342+
$this->is_after_word = '' === $quantifier && 1 === preg_match( '/[A-Za-z0-9_]$/D', $token );
325343
return $token;
326344
}
327345
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: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1050,6 +1050,24 @@ public function test_a_leading_dot_star_matches_like_any_other(): void {
10501050
$this->assertSame( '/(?:\G|(?<=[\n\x0B\f\r]))[^\n\x0B\f\r]*a/X', WP_ICU_Regex_Translator::translate( '.*a', '', false, true ) );
10511051
}
10521052

1053+
public function test_a_word_boundary_next_to_a_word_character_checks_one_side(): void {
1054+
$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}]';
1055+
$this->assertSame( "/(?<!$word)word(?!$word)/uX", WP_ICU_Regex_Translator::translate( '\bword\b' ) );
1056+
$this->assertSame( "/(?<=$word)_1(?=$word)/uX", WP_ICU_Regex_Translator::translate( '\B_1\B' ) );
1057+
$this->assertSame( "/a(?<!$word)b|c(?!$word)/uXi", WP_ICU_Regex_Translator::translate( 'a\bb|c\b', 'i' ) );
1058+
$this->assertSame( '/\bword\b/X', WP_ICU_Regex_Translator::translate( '\bword\b', '', false, true ) );
1059+
1060+
// Keep the full boundary check unless the translator recognizes an adjacent, unquantified ASCII word character.
1061+
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 ) {
1062+
$this->assertStringContainsString( "(?(?<=$word)(?!$word)|(?=$word))", WP_ICU_Regex_Translator::translate( $pattern ), $pattern );
1063+
}
1064+
1065+
$this->assertSame( '<cat> concat cat_ écat <cat>', $this->replace_matches( WP_ICU_Regex_Translator::translate( '\bcat\b' ), 'cat concat cat_ écat cat' ) );
1066+
$this->assertSame( 'cat con<cat> é<cat>', $this->replace_matches( WP_ICU_Regex_Translator::translate( '\Bcat' ), 'cat concat écat' ) );
1067+
$this->assertSame( "<k> <\u{212A}> ak", $this->replace_matches( WP_ICU_Regex_Translator::translate( '\bk\b', 'i' ), "k \u{212A} ak" ) );
1068+
$this->assertSame( 'ab', $this->replace_matches( WP_ICU_Regex_Translator::translate( 'a\bb' ), 'ab' ) );
1069+
}
1070+
10531071
public function test_unknown_flags_are_rejected(): void {
10541072
$this->expectException( WP_ICU_Regex_Exception::class );
10551073
WP_ICU_Regex_Translator::translate( 'a', 'x' );

0 commit comments

Comments
 (0)