From f9590a91ad948db1449a3dc61d90e9c9a71c6ee3 Mon Sep 17 00:00:00 2001 From: Matt Farina Date: Wed, 10 Apr 2013 13:07:49 -0400 Subject: Moved the scanner to the new Parser InputStream and updated the unit tests to use StringInputStream --- src/HTML5/Parser/StringInputStream.php | 62 ++++++++++++++++++++++++---------- 1 file changed, 45 insertions(+), 17 deletions(-) (limited to 'src/HTML5/Parser/StringInputStream.php') diff --git a/src/HTML5/Parser/StringInputStream.php b/src/HTML5/Parser/StringInputStream.php index 4ceae44..e26bb38 100644 --- a/src/HTML5/Parser/StringInputStream.php +++ b/src/HTML5/Parser/StringInputStream.php @@ -269,16 +269,40 @@ class StringInputStream implements InputStream { } /** - * Retrieve the currently consumed character. - * @note This performs bounds checking + * Get the current character. + * + * @return string + * The current character. + */ + public function current() { + return $this->data[$this->char]; + } + + /** + * Advance the pointer. This is part of the Iterator interface. + */ + public function next() { + $this->char++; + } + + /** + * Rewind to the start of the string. + */ + public function rewind() { + $this->char = 0; + } + + /** + * Is the current pointer location valid. + * + * @return bool + * Is the current pointer location valid. */ - public function char() { - // MPB: This appears to advance the pointer, which is not the same - // as "retrieving the currently consumed character". Calling char() - // twice will return two different results. - if ($this->char++ < $this->EOF) { - return $this->data[$this->char - 1]; + public function valid() { + if ($this->char < $this->EOF) { + return TRUE; } + return FALSE; } @@ -362,22 +386,26 @@ class StringInputStream implements InputStream { } /** - * Unconsume one character. + * Unconsume characters. + * + * @param int $howMany + * The number of characters to unconsume. */ - public function unconsume() { - if ($this->char > 0 && $this->char <= $this->EOF) { - $this->char--; + public function unconsume($howMany = 1) { + if (($this->char - $howMany) >= 0) { + $this->char = $this->char - $howMany; } } - public function unget() { - $this->unconsume(); - } public function peek() { - return $this->data[$this->char + 1]; + if (($this->char + 1) <= $this->EOF) { + return $this->data[$this->char + 1]; + } + + return FALSE; } - public function position() { + public function key() { return $this->char; } } -- cgit v1.2.3