From 0602455484ed43c00e93240a15b889a265595274 Mon Sep 17 00:00:00 2001 From: Andrey Kutejko Date: Sat, 22 Mar 2014 16:28:59 +0200 Subject: [PATCH] add pushback streams --- composer.lock | 10 +++--- src/io/pushback.php | 78 +++++++++++++++++++++++++++++++++++++++++++++ 2 files changed, 83 insertions(+), 5 deletions(-) create mode 100644 src/io/pushback.php diff --git a/composer.lock b/composer.lock index cf52404..c0c485a 100644 --- a/composer.lock +++ b/composer.lock @@ -237,16 +237,16 @@ }, { "name": "phpunit/php-token-stream", - "version": "1.2.1", + "version": "1.2.2", "source": { "type": "git", "url": "https://github.com/sebastianbergmann/php-token-stream.git", - "reference": "5220af2a7929aa35cf663d97c89ad3d50cf5fa3e" + "reference": "ad4e1e23ae01b483c16f600ff1bebec184588e32" }, "dist": { "type": "zip", - "url": "https://api.github.com/repos/sebastianbergmann/php-token-stream/zipball/5220af2a7929aa35cf663d97c89ad3d50cf5fa3e", - "reference": "5220af2a7929aa35cf663d97c89ad3d50cf5fa3e", + "url": "https://api.github.com/repos/sebastianbergmann/php-token-stream/zipball/ad4e1e23ae01b483c16f600ff1bebec184588e32", + "reference": "ad4e1e23ae01b483c16f600ff1bebec184588e32", "shasum": "" }, "require": { @@ -283,7 +283,7 @@ "keywords": [ "tokenizer" ], - "time": "2013-09-13 04:58:23" + "time": "2014-03-03 05:10:30" }, { "name": "phpunit/phpunit", diff --git a/src/io/pushback.php b/src/io/pushback.php new file mode 100644 index 0000000..e2dfc8b --- /dev/null +++ b/src/io/pushback.php @@ -0,0 +1,78 @@ +fd = fopen($path, $mode); + } + + public function __destruct() + { + $this->close(); + } + + public function close() + { + if ($this->fd) { + fclose($this->fd); + $this->fd = null; + } + } + + public function getc() + { + if (count($this->buffer)) { + return array_shift($this->buffer); + } else { + return fgetc($this->fd); + } + } + + public function ungetc($char) + { + array_unshift($this->buffer, $char); + } +} + +class StringInputStream implements PushBackInputStream +{ + private $string, $position; + + public function __construct($string) + { + $this->string = $string; + $this->position = 0; + } + + public function close() + { + if ($this->string !== null) + $this->string = null; + } + + public function getc() + { + if ($this->position >= strlen($this->string)) + return false; + else + return $this->string[$this->position++]; + } + + public function ungetc($char) + { + --$this->position; + } +} + -- 2.49.0