Skip to content

Commit 11d14b3

Browse files
committed
Fix reading past end to consistently return NULL
1 parent 1870b0c commit 11d14b3

File tree

5 files changed

+21
-4
lines changed

5 files changed

+21
-4
lines changed

ChangeLog.md

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,10 @@ Tokenize log
33

44
## ?.?.? / ????-??-??
55

6+
## 9.0.1 / 2020-10-09
7+
8+
* Fixed reading past end to consistently return `NULL` - @thekid
9+
610
## 9.0.0 / 2019-11-30
711

812
* Dropped support for PHP 5.6, see xp-framework/rfc#334 - @thekid

src/main/php/text/StreamTokenizer.class.php

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -69,12 +69,13 @@ public function pushBack($str) {
6969
*/
7070
public function nextToken($delimiters= null) {
7171
if (empty($this->_stack)) {
72-
72+
if (false === $this->_buf) return null;
73+
7374
// Read until we have either find a delimiter or until we have
7475
// consumed the entire content.
7576
do {
7677
$offset= strcspn($this->_buf, $delimiters ? $delimiters : $this->delimiters);
77-
if ($offset < strlen($this->_buf)- 1 || !$this->_src->available()) break;
78+
if ($offset < strlen($this->_buf) - 1 || !$this->_src->available()) break;
7879
$this->_buf.= $this->_src->read();
7980
} while (true);
8081

src/main/php/text/StringTokenizer.class.php

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -74,6 +74,8 @@ public function pushBack($str) {
7474
*/
7575
public function nextToken($delimiters= null) {
7676
if (empty($this->_stack)) {
77+
if ($this->_ofs >= $this->_len) return null;
78+
7779
$offset= strcspn($this->source, $delimiters ? $delimiters : $this->delimiters, $this->_ofs);
7880
if (!$this->returnDelims || $offset > 0) $this->_stack[]= substr($this->source, $this->_ofs, $offset);
7981
if ($this->returnDelims && $this->_ofs + $offset < $this->_len) {

src/main/php/text/TextTokenizer.class.php

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -67,12 +67,13 @@ public function pushBack($str) {
6767
*/
6868
public function nextToken($delimiters= null) {
6969
if (empty($this->_stack)) {
70-
70+
if (false === $this->_buf) return null;
71+
7172
// Read until we have either find a delimiter or until we have
7273
// consumed the entire content.
7374
do {
7475
$offset= strcspn($this->_buf, $delimiters ? $delimiters : $this->delimiters);
75-
if ($offset < strlen($this->_buf)- 1) break;
76+
if ($offset < strlen($this->_buf) - 1) break;
7677
if (null === ($buf= $this->source->read())) {
7778
break;
7879
}

src/test/php/text/unittest/AbstractTokenizerTest.class.php

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -224,4 +224,13 @@ public function performance() {
224224
$token= $t->nextToken();
225225
}
226226
}
227+
228+
#[Test]
229+
public function reading_past_end_returns_null() {
230+
$t= $this->tokenizerInstance('Test', "\n", false);
231+
while ($t->hasMoreTokens()) {
232+
$t->nextToken();
233+
}
234+
$this->assertEquals([null, null], [$t->nextToken(), $t->nextToken()]);
235+
}
227236
}

0 commit comments

Comments
 (0)