From 321ed9626c091f1f4dcef8223d88ee88a400a241 Mon Sep 17 00:00:00 2001 From: Titouan Galopin Date: Sat, 3 Nov 2018 01:03:34 +0100 Subject: Improve performance by relying on a native string instead of InputStream --- src/HTML5.php | 35 ++--- src/HTML5/Parser/FileInputStream.php | 3 +- src/HTML5/Parser/InputStream.php | 4 +- src/HTML5/Parser/Scanner.php | 237 ++++++++++++++++++++++++++++----- src/HTML5/Parser/StringInputStream.php | 9 +- 5 files changed, 228 insertions(+), 60 deletions(-) (limited to 'src') diff --git a/src/HTML5.php b/src/HTML5.php index 6c57553..a3db74c 100644 --- a/src/HTML5.php +++ b/src/HTML5.php @@ -55,7 +55,7 @@ class HTML5 * * The rules governing parsing are set out in the HTML 5 spec. * - * @param string $file + * @param string|resource $file * The path to the file to parse. If this is a resource, it is * assumed to be an open stream whose pointer is set to the first * byte of input. @@ -68,13 +68,10 @@ class HTML5 { // Handle the case where file is a resource. if (is_resource($file)) { - // FIXME: We need a StreamInputStream class. - return $this->loadHTML(stream_get_contents($file), $options); + return $this->parse(stream_get_contents($file), $options); } - $input = new FileInputStream($file); - - return $this->parse($input, $options); + return $this->parse(file_get_contents($file), $options); } /** @@ -92,9 +89,7 @@ class HTML5 */ public function loadHTML($string, array $options = array()) { - $input = new StringInputStream($string); - - return $this->parse($input, $options); + return $this->parse($string, $options); } /** @@ -121,19 +116,15 @@ class HTML5 /** * Parse a HTML fragment from a string. * - * @param string $string - * The html5 fragment as a string. - * @param array $options - * Configuration options when parsing the HTML + * @param string $string The HTML5 fragment as a string. + * @param array $options Configuration options when parsing the HTML * * @return \DOMDocumentFragment A DOM fragment. The DOM is part of libxml, which is included with * almost all distributions of PHP. */ public function loadHTMLFragment($string, array $options = array()) { - $input = new StringInputStream($string); - - return $this->parseFragment($input, $options); + return $this->parseFragment($string, $options); } /** @@ -162,12 +153,12 @@ class HTML5 * Lower-level loading function. This requires an input stream instead * of a string, file, or resource. * - * @param InputStream $input + * @param string $input * @param array $options * * @return \DOMDocument */ - public function parse(InputStream $input, array $options = array()) + public function parse($input, array $options = array()) { $this->errors = array(); $options = array_merge($this->getOptions(), $options); @@ -187,14 +178,12 @@ class HTML5 * Lower-level loading function. This requires an input stream instead * of a string, file, or resource. * - * @param InputStream $input - * The input data to parse in the form of a InputStream instance. - * @param array $options - * An array of options + * @param string $input The input data to parse in the form of a string. + * @param array $options An array of options * * @return \DOMDocumentFragment */ - public function parseFragment(InputStream $input, array $options = array()) + public function parseFragment($input, array $options = array()) { $options = array_merge($this->getOptions(), $options); $events = new DOMTreeBuilder(true, $options); diff --git a/src/HTML5/Parser/FileInputStream.php b/src/HTML5/Parser/FileInputStream.php index fbf006d..76bd17b 100644 --- a/src/HTML5/Parser/FileInputStream.php +++ b/src/HTML5/Parser/FileInputStream.php @@ -11,11 +11,10 @@ namespace Masterminds\HTML5\Parser; * really like to rewrite this class to efficiently handle lower level * stream reads (and thus efficiently handle large documents). * - * @todo A buffered input stream would be useful. + * @deprecated since 2.4, to remove in 3.0. Use a string in the scanner instead. */ class FileInputStream extends StringInputStream implements InputStream { - /** * Load a file input stream. * diff --git a/src/HTML5/Parser/InputStream.php b/src/HTML5/Parser/InputStream.php index 0bdc803..e4a106a 100644 --- a/src/HTML5/Parser/InputStream.php +++ b/src/HTML5/Parser/InputStream.php @@ -1,4 +1,5 @@ is = $input; + if ($data instanceof InputStream) { + @trigger_error('InputStream objects are deprecated since version 2.4 and will be removed in 3.0. Use strings instead.', E_USER_DEPRECATED); + $data = (string) $data; + } + + $data = UTF8Utils::convertToUTF8($data, $encoding); + + // There is good reason to question whether it makes sense to + // do this here, since most of these checks are done during + // parsing, and since this check doesn't actually *do* anything. + $this->errors = UTF8Utils::checkForIllegalCodepoints($data); + + $data = $this->replaceLinefeeds($data); + + $this->data = $data; + $this->char = 0; + $this->EOF = strlen($data); } /** @@ -38,7 +68,7 @@ class Scanner */ public function position() { - return $this->is->key(); + return $this->char; } /** @@ -48,7 +78,11 @@ class Scanner */ public function peek() { - return $this->is->peek(); + if (($this->char + 1) <= $this->EOF) { + return $this->data[$this->char + 1]; + } + + return false; } /** @@ -60,11 +94,10 @@ class Scanner */ public function next() { - $this->is->next(); - if ($this->is->valid()) { - if ($this->debug) - fprintf(STDOUT, "> %s\n", $this->is->current()); - return $this->is->current(); + $this->char++; + + if ($this->char < $this->EOF) { + return $this->data[$this->char]; } return false; @@ -79,8 +112,8 @@ class Scanner */ public function current() { - if ($this->is->valid()) { - return $this->is->current(); + if ($this->char < $this->EOF) { + return $this->data[$this->char]; } return false; @@ -88,6 +121,8 @@ class Scanner /** * Silently consume N chars. + * + * @param int $count */ public function consume($count = 1) { @@ -105,7 +140,9 @@ class Scanner */ public function unconsume($howMany = 1) { - $this->is->unconsume($howMany); + if (($this->char - $howMany) >= 0) { + $this->char = $this->char - $howMany; + } } /** @@ -118,7 +155,7 @@ class Scanner */ public function getHex() { - return $this->is->charsWhile(static::CHARS_HEX); + return $this->doCharsWhile(static::CHARS_HEX); } /** @@ -131,7 +168,7 @@ class Scanner */ public function getAsciiAlpha() { - return $this->is->charsWhile(static::CHARS_ALPHA); + return $this->doCharsWhile(static::CHARS_ALPHA); } /** @@ -144,7 +181,7 @@ class Scanner */ public function getAsciiAlphaNum() { - return $this->is->charsWhile(static::CHARS_ALNUM); + return $this->doCharsWhile(static::CHARS_ALNUM); } /** @@ -157,7 +194,7 @@ class Scanner */ public function getNumeric() { - return $this->is->charsWhile('0123456789'); + return $this->doCharsWhile('0123456789'); } /** @@ -167,7 +204,7 @@ class Scanner */ public function whitespace() { - return $this->is->charsWhile("\n\t\f "); + return $this->doCharsWhile("\n\t\f "); } /** @@ -177,23 +214,37 @@ class Scanner */ public function currentLine() { - return $this->is->currentLine(); + if (empty($this->EOF) || $this->char == 0) { + return 1; + } + + // Add one to $this->char because we want the number for the next + // byte to be processed. + return substr_count($this->data, "\n", 0, min($this->char, $this->EOF)) + 1; } /** * Read chars until something in the mask is encountered. + * + * @param string $mask + * + * @return mixed */ public function charsUntil($mask) { - return $this->is->charsUntil($mask); + return $this->doCharsUntil($mask); } /** * Read chars as long as the mask matches. + * + * @param string $mask + * + * @return int */ public function charsWhile($mask) { - return $this->is->charsWhile($mask); + return $this->doCharsWhile($mask); } /** @@ -205,7 +256,29 @@ class Scanner */ public function columnOffset() { - return $this->is->columnOffset(); + // Short circuit for the first char. + if ($this->char == 0) { + return 0; + } + + // strrpos is weird, and the offset needs to be negative for what we + // want (i.e., the last \n before $this->char). This needs to not have + // one (to make it point to the next character, the one we want the + // position of) added to it because strrpos's behaviour includes the + // final offset byte. + $backwardFrom = $this->char - 1 - strlen($this->data); + $lastLine = strrpos($this->data, "\n", $backwardFrom); + + // However, for here we want the length up until the next byte to be + // processed, so add one to the current byte ($this->char). + if ($lastLine !== false) { + $findLengthOf = substr($this->data, $lastLine + 1, $this->char - 1 - $lastLine); + } else { + // After a newline. + $findLengthOf = substr($this->data, 0, $this->char); + } + + return UTF8Utils::countChars($findLengthOf); } /** @@ -217,6 +290,104 @@ class Scanner */ public function remainingChars() { - return $this->is->remainingChars(); + if ($this->char < $this->EOF) { + $data = substr($this->data, $this->char); + $this->char = $this->EOF; + + return $data; + } + + return ''; // false; + } + + /** + * Replace linefeed characters according to the spec. + * + * @param $data + * + * @return string + */ + private function replaceLinefeeds($data) + { + /* + * U+000D CARRIAGE RETURN (CR) characters and U+000A LINE FEED (LF) characters are treated specially. + * Any CR characters that are followed by LF characters must be removed, and any CR characters not + * followed by LF characters must be converted to LF characters. Thus, newlines in HTML DOMs are + * represented by LF characters, and there are never any CR characters in the input to the tokenization + * stage. + */ + $crlfTable = array( + "\0" => "\xEF\xBF\xBD", + "\r\n" => "\n", + "\r" => "\n" + ); + + return strtr($data, $crlfTable); + } + + /** + * Read to a particular match (or until $max bytes are consumed). + * + * This operates on byte sequences, not characters. + * + * Matches as far as possible until we reach a certain set of bytes + * and returns the matched substring. + * + * @param string $bytes + * Bytes to match. + * @param int $max + * Maximum number of bytes to scan. + * @return mixed Index or false if no match is found. You should use strong + * equality when checking the result, since index could be 0. + */ + private function doCharsUntil($bytes, $max = null) + { + if ($this->char >= $this->EOF) { + return false; + } + + if ($max === 0 || $max) { + $len = strcspn($this->data, $bytes, $this->char, $max); + } else { + $len = strcspn($this->data, $bytes, $this->char); + } + + $string = (string) substr($this->data, $this->char, $len); + $this->char += $len; + + return $string; + } + + /** + * Returns the string so long as $bytes matches. + * + * Matches as far as possible with a certain set of bytes + * and returns the matched substring. + * + * @param string $bytes + * A mask of bytes to match. If ANY byte in this mask matches the + * current char, the pointer advances and the char is part of the + * substring. + * @param int $max + * The max number of chars to read. + * + * @return string + */ + private function doCharsWhile($bytes, $max = null) + { + if ($this->char >= $this->EOF) { + return false; + } + + if ($max === 0 || $max) { + $len = strspn($this->data, $bytes, $this->char, $max); + } else { + $len = strspn($this->data, $bytes, $this->char); + } + + $string = (string) substr($this->data, $this->char, $len); + $this->char += $len; + + return $string; } } diff --git a/src/HTML5/Parser/StringInputStream.php b/src/HTML5/Parser/StringInputStream.php index 0973941..0118468 100644 --- a/src/HTML5/Parser/StringInputStream.php +++ b/src/HTML5/Parser/StringInputStream.php @@ -39,9 +39,11 @@ SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. // // - // indicates regular comments +/** + * @deprecated since 2.4, to remove in 3.0. Use a string in the scanner instead. + */ class StringInputStream implements InputStream { - /** * The string data we're parsing. */ @@ -88,6 +90,11 @@ class StringInputStream implements InputStream $this->EOF = strlen($data); } + public function __toString() + { + return $this->data; + } + /** * Replace linefeed characters according to the spec. */ -- cgit v1.2.3