Skip to content

Commit 182f34d

Browse files
authored
Merge pull request #161 from stof/optimize_main_loop
Optimize main loop
2 parents 88b7c66 + 6cdf428 commit 182f34d

File tree

1 file changed

+31
-43
lines changed

1 file changed

+31
-43
lines changed

src/HTML5/Parser/Tokenizer.php

Lines changed: 31 additions & 43 deletions
Original file line numberDiff line numberDiff line change
@@ -131,22 +131,28 @@ protected function consumeData()
131131

132132
$tok = $this->scanner->next();
133133

134-
$this->markupDeclaration($tok)
135-
|| $this->endTag()
136-
|| $this->processingInstruction()
137-
|| $this->tagName()
138-
// This always returns false.
139-
|| $this->parseError('Illegal tag opening')
140-
|| $this->characterData();
134+
if ('!' === $tok) {
135+
$this->markupDeclaration();
136+
} elseif ('/' === $tok) {
137+
$this->endTag();
138+
} elseif ('?' === $tok) {
139+
$this->processingInstruction();
140+
} elseif (ctype_alpha($tok)) {
141+
$this->tagName();
142+
} else {
143+
$this->parseError('Illegal tag opening');
144+
// TODO is this necessary ?
145+
$this->characterData();
146+
}
141147

142148
$tok = $this->scanner->current();
143149
}
144150

145-
// Handle end of document
146-
$this->eof($tok);
147-
148-
// Parse character
149-
if (false !== $tok) {
151+
if (false === $tok) {
152+
// Handle end of document
153+
$this->eof();
154+
} else {
155+
// Parse character
150156
switch ($this->textMode) {
151157
case Elements::TEXT_RAW:
152158
$this->rawText($tok);
@@ -288,29 +294,19 @@ protected function rcdata($tok)
288294
/**
289295
* If the document is read, emit an EOF event.
290296
*/
291-
protected function eof($tok)
297+
protected function eof()
292298
{
293-
if (false === $tok) {
294-
// fprintf(STDOUT, "EOF");
295-
$this->flushBuffer();
296-
$this->events->eof();
297-
$this->carryOn = false;
298-
299-
return true;
300-
}
301-
302-
return false;
299+
// fprintf(STDOUT, "EOF");
300+
$this->flushBuffer();
301+
$this->events->eof();
302+
$this->carryOn = false;
303303
}
304304

305305
/**
306306
* Look for markup.
307307
*/
308-
protected function markupDeclaration($tok)
308+
protected function markupDeclaration()
309309
{
310-
if ('!' != $tok) {
311-
return false;
312-
}
313-
314310
$tok = $this->scanner->next();
315311

316312
// Comment:
@@ -377,11 +373,6 @@ protected function endTag()
377373
*/
378374
protected function tagName()
379375
{
380-
$tok = $this->scanner->current();
381-
if (!ctype_alpha($tok)) {
382-
return false;
383-
}
384-
385376
// We know this is at least one char.
386377
$name = $this->scanner->charsWhile(':_-0123456789ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz');
387378
$name = self::CONFORMANT_XML === $this->mode ? $name : strtolower($name);
@@ -743,12 +734,11 @@ protected function isCommentEnd()
743734
*/
744735
protected function doctype()
745736
{
746-
if (strcasecmp($this->scanner->current(), 'D')) {
747-
return false;
748-
}
749737
// Check that string is DOCTYPE.
750-
$chars = $this->scanner->charsWhile('DOCTYPEdoctype');
751-
if (strcasecmp($chars, 'DOCTYPE')) {
738+
if ($this->scanner->sequenceMatches('DOCTYPE', false)) {
739+
$this->scanner->consume(7);
740+
} else {
741+
$chars = $this->scanner->charsWhile('DOCTYPEdoctype');
752742
$this->parseError('Expected DOCTYPE, got %s', $chars);
753743

754744
return $this->bogusComment('<!' . $chars);
@@ -760,8 +750,9 @@ protected function doctype()
760750
// EOF: die.
761751
if (false === $tok) {
762752
$this->events->doctype('html5', EventHandler::DOCTYPE_NONE, '', true);
753+
$this->eof();
763754

764-
return $this->eof($tok);
755+
return true;
765756
}
766757

767758
// NULL char: convert.
@@ -812,7 +803,7 @@ protected function doctype()
812803
if (false === $id) {
813804
$this->events->doctype($doctypeName, $type, $pub, false);
814805

815-
return false;
806+
return true;
816807
}
817808

818809
// Premature EOF.
@@ -887,9 +878,6 @@ protected function quotedString($stopchars)
887878
*/
888879
protected function cdataSection()
889880
{
890-
if ('[' != $this->scanner->current()) {
891-
return false;
892-
}
893881
$cdata = '';
894882
$this->scanner->consume();
895883

0 commit comments

Comments
 (0)