Skip to content

Commit a2432e5

Browse files
committed
Optimize consuming whitespaces
Places consuming whitespaces don't care about the matched substring. They either need its length, or nothing. Returning only the length directly avoids computing the substring.
1 parent 1fd419b commit a2432e5

2 files changed

Lines changed: 14 additions & 4 deletions

File tree

src/HTML5/Parser/Scanner.php

Lines changed: 11 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -223,10 +223,20 @@ public function getNumeric()
223223
* Consume whitespace.
224224
*
225225
* Whitespace in HTML5 is: formfeed, tab, newline, space.
226+
*
227+
* @return int The length of the matched whitespaces
226228
*/
227229
public function whitespace()
228230
{
229-
return $this->doCharsWhile("\n\t\f ");
231+
if ($this->char >= $this->EOF) {
232+
return false;
233+
}
234+
235+
$len = strspn($this->data, "\n\t\f ", $this->char);
236+
237+
$this->char += $len;
238+
239+
return $len;
230240
}
231241

232242
/**

src/HTML5/Parser/Tokenizer.php

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -279,7 +279,7 @@ protected function rcdata($tok)
279279
}
280280
$len = strlen($sequence);
281281
$this->scanner->consume($len);
282-
$len += strlen($this->scanner->whitespace());
282+
$len += $this->scanner->whitespace();
283283
if ($this->scanner->current() !== '>') {
284284
$this->parseError("Unclosed RCDATA end tag");
285285
}
@@ -779,7 +779,7 @@ protected function doctype()
779779
$this->scanner->whitespace();
780780

781781
$pub = strtoupper($this->scanner->getAsciiAlpha());
782-
$white = strlen($this->scanner->whitespace());
782+
$white = $this->scanner->whitespace();
783783

784784
// Get ID, and flag it as pub or system.
785785
if (($pub == 'PUBLIC' || $pub == 'SYSTEM') && $white > 0) {
@@ -909,7 +909,7 @@ protected function processingInstruction()
909909

910910
$tok = $this->scanner->next();
911911
$procName = $this->scanner->getAsciiAlpha();
912-
$white = strlen($this->scanner->whitespace());
912+
$white = $this->scanner->whitespace();
913913

914914
// If not a PI, send to bogusComment.
915915
if (strlen($procName) == 0 || $white == 0 || $this->scanner->current() == false) {

0 commit comments

Comments
 (0)