Skip to content

Commit 1ca3e9b

Browse files
committed
Perf: Use ctype_alpha() for ASCII letter checks
Replace inline range comparisons with ctype_alpha() which dispatches to a single C-level function call. Applies to all four sites: fast path tag check, fast path text validation, parse_next_tag() tag check, and parse_next_tag() text validation.
1 parent a89e78d commit 1ca3e9b

File tree

1 file changed

+4
-4
lines changed

1 file changed

+4
-4
lines changed

src/wp-includes/html-api/class-wp-html-tag-processor.php

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1042,7 +1042,7 @@ private function base_class_next_token(): bool {
10421042
$next_byte = $html[ $at + 1 ] ?? '';
10431043
if (
10441044
'!' !== $next_byte && '/' !== $next_byte && '?' !== $next_byte &&
1045-
( $next_byte < 'A' || ( $next_byte > 'Z' && $next_byte < 'a' ) || $next_byte > 'z' )
1045+
! ctype_alpha( $next_byte )
10461046
) {
10471047
/*
10481048
* The '<' doesn't start a valid token. Fall through to
@@ -1067,7 +1067,7 @@ private function base_class_next_token(): bool {
10671067
$first_char = $html[ $at + 2 ] ?? '';
10681068
}
10691069

1070-
if ( ( $first_char >= 'a' && $first_char <= 'z' ) || ( $first_char >= 'A' && $first_char <= 'Z' ) ) {
1070+
if ( ctype_alpha( $first_char ) ) {
10711071
$tag_at = $at + 1 + ( $is_closer ? 1 : 0 );
10721072
$tag_length = strcspn( $html, " \t\f\r\n/>", $tag_at );
10731073
$after_name = $tag_at + $tag_length;
@@ -1872,7 +1872,7 @@ private function parse_next_tag(): bool {
18721872
$next_byte = $html[ $at + 1 ] ?? '';
18731873
if (
18741874
'!' !== $next_byte && '/' !== $next_byte && '?' !== $next_byte &&
1875-
( $next_byte < 'A' || ( $next_byte > 'Z' && $next_byte < 'a' ) || $next_byte > 'z' )
1875+
! ctype_alpha( $next_byte )
18761876
) {
18771877
++$at;
18781878
continue;
@@ -1910,7 +1910,7 @@ private function parse_next_tag(): bool {
19101910
* * https://html.spec.whatwg.org/multipage/parsing.html#tag-open-state
19111911
*/
19121912
$first_char = $html[ $at + 1 ] ?? '';
1913-
if ( ( $first_char >= 'a' && $first_char <= 'z' ) || ( $first_char >= 'A' && $first_char <= 'Z' ) ) {
1913+
if ( ctype_alpha( $first_char ) ) {
19141914
++$at;
19151915
$this->parser_state = self::STATE_MATCHED_TAG;
19161916
$this->tag_name_starts_at = $at;

0 commit comments

Comments
 (0)