PHP :: Bug #27042 :: SPL: SeekableIterator seek() broken
| Bug #27042 | SPL: SeekableIterator seek() broken | |||||||||||||
|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|
| Submitted: | 2004-01-26 02:16 UTC | Modified: | 2004-01-28 17:53 UTC |
|
||||||||||
| From: | adam at trachtenberg dot com | Assigned: | helly (profile) | |||||||||||
| Status: | Closed | Package: | Scripting Engine problem | |||||||||||
| PHP Version: | 5CVS-2004-01-26 (dev) | OS: | * | |||||||||||
| Private report: | No | CVE-ID: | None | |||||||||||
[2004-01-26 02:16 UTC] adam at trachtenberg dot com
Description:
------------
When a class implements SeekableIterator and provides a
seek() method, SPL does not seek correctly. When SPL is
forced to emulate seek() by advancing step-by-step, it
works correctly. (See NumericArrayIterator vs
SeekableNumericArrayIterator.)
At first, I thought spl_limit_it_seek() needed to call:
if (spl_dual_it_has_more(intern TSRMLS_CC) == SUCCESS) {
spl_dual_it_fetch(intern, 1 TSRMLS_CC);
}
Even for instances of SeekableIterator, but that only
worked with the offset was 0.
Reproduce code:
---------------
class NumericArrayIterator implements Iterator {
protected $a;
protected $i;
public function __construct($a) {
$this->a = $a;
}
public function rewind() {
$this->i = 0;
}
public function hasMore() {
return $this->i < count($this->a);
}
public function key() {
return $this->i;
}
public function current() {
return $this->a[$this->i];
}
public function next() {
$this->i++;
}
}
class SeekableNumericArrayIterator extends NumericArrayIterator implements SeekableIterator {
public function seek($index) {
if ($index < count($this->a)) {
$this->i = $index;
}
}
}
$a = array(1, 2, 3, 4, 5);
foreach (new LimitIterator(new NumericArrayIterator($a), 0, -1) as $v) {
print "$v\n";
}
foreach (new LimitIterator(new SeekableNumericArrayIterator($a), 0, -1) as $v) {
print "$v\n";
}
Expected result:
----------------
1
2
3
4
5
1
2
3
4
5
Actual result:
--------------
1
2
3
4
5
Patches
Pull Requests
History
AllCommentsChangesGit/SVN commits
[2004-01-26 17:29 UTC] helly@php.net
[2004-01-26 20:42 UTC] adam at trachtenberg dot com
Thanks for the speedy bug fix. However, I found another problem when you seek() to an amount other than -1. In those cases, you only get 1 result. For example, using the same code as before, but with "3" instead of "-1": $a = array(1, 2, 3, 4, 5); foreach (new LimitIterator(new NumericArrayIterator($a), 0, 3) as $v) { print "$v\n"; } foreach (new LimitIterator(new SeekableNumericArrayIterator($a), 0, 3) as $v) { print "$v\n"; } 1 2 3 1[2004-01-28 17:53 UTC] helly@php.net