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