Skip to content

Commit 796d11a

Browse files
committed
Skip string allocations
1 parent 0b8242b commit 796d11a

2 files changed

Lines changed: 17 additions & 20 deletions

File tree

src/wp-includes/html-api/class-wp-html-decoder.php

Lines changed: 10 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -179,19 +179,19 @@ public static function decode_attribute( $text, $at = 0, $length = null ) {
179179
* @return string Decoded string.
180180
*/
181181
public static function decode( $context, $text, $at = 0, $length = null ) {
182-
$decoded = '';
183-
$end = isset( $length ) ? $at + $length : strlen( $text );
184-
$was_at = $at;
182+
$decoded = '';
183+
$end = isset( $length ) ? $at + $length : strlen( $text );
184+
$started_at = $at;
185+
$was_at = $at;
185186

186187
while ( $at < $end ) {
187-
$next_character_reference_at = strpos( $text, '&', $at );
188-
if ( false === $next_character_reference_at ) {
189-
break;
188+
if ( '&' !== $text[ $at ] ) {
189+
++$at;
190+
continue;
190191
}
191192

192-
$character_reference = self::read_character_reference( $context, $text, $next_character_reference_at, $skip_bytes );
193+
$character_reference = self::read_character_reference( $context, $text, $at, $skip_bytes );
193194
if ( isset( $character_reference ) ) {
194-
$at = $next_character_reference_at;
195195
$decoded .= substr( $text, $was_at, $at - $was_at );
196196
$decoded .= $character_reference;
197197
$at += $skip_bytes;
@@ -202,8 +202,8 @@ public static function decode( $context, $text, $at = 0, $length = null ) {
202202
++$at;
203203
}
204204

205-
if ( 0 === $was_at ) {
206-
return $text;
205+
if ( $started_at === $was_at ) {
206+
return substr( $text, $started_at, $length );
207207
}
208208

209209
if ( $was_at < $end ) {

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

Lines changed: 7 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -2494,8 +2494,9 @@ private function get_enqueued_attribute_value( $comparable_name ) {
24942494
* 2. Double-quoting starts one after the equals sign.
24952495
* 3. Double-quoting ends at the last character in the update.
24962496
*/
2497-
$enqueued_value = substr( $enqueued_text, $equals_at + 2, -1 );
2498-
return WP_HTML_Decoder::decode_attribute( $enqueued_value );
2497+
$start_at = $equals_at + 2;
2498+
$end_at = strlen( $enqueued_text ) - 1;
2499+
return WP_HTML_Decoder::decode_attribute( $enqueued_text, $start_at, $end_at - $start_at );
24992500
}
25002501

25012502
/**
@@ -2566,9 +2567,7 @@ public function get_attribute( $name ) {
25662567
return true;
25672568
}
25682569

2569-
$raw_value = substr( $this->html, $attribute->value_starts_at, $attribute->value_length );
2570-
2571-
return WP_HTML_Decoder::decode_attribute( $raw_value );
2570+
return WP_HTML_Decoder::decode_attribute( $this->html, $attribute->value_starts_at, $attribute->value_length );
25722571
}
25732572

25742573
/**
@@ -2841,16 +2840,14 @@ public function get_modifiable_text() {
28412840
return '';
28422841
}
28432842

2844-
$text = substr( $this->html, $this->text_starts_at, $this->text_length );
2845-
28462843
// Comment data is not decoded.
28472844
if (
28482845
self::STATE_CDATA_NODE === $this->parser_state ||
28492846
self::STATE_COMMENT === $this->parser_state ||
28502847
self::STATE_DOCTYPE === $this->parser_state ||
28512848
self::STATE_FUNKY_COMMENT === $this->parser_state
28522849
) {
2853-
return $text;
2850+
return substr( $this->html, $this->text_starts_at, $this->text_length );
28542851
}
28552852

28562853
$tag_name = $this->get_tag();
@@ -2865,10 +2862,10 @@ public function get_modifiable_text() {
28652862
'STYLE' === $tag_name ||
28662863
'XMP' === $tag_name
28672864
) {
2868-
return $text;
2865+
return substr( $this->html, $this->text_starts_at, $this->text_length );
28692866
}
28702867

2871-
$decoded = WP_HTML_Decoder::decode_text_node( $text );
2868+
$decoded = WP_HTML_Decoder::decode_text_node( $this->html, $this->text_starts_at, $this->text_length );
28722869

28732870
/*
28742871
* TEXTAREA skips a leading newline, but this newline may appear not only as the

0 commit comments

Comments
 (0)