php.net |  support |  documentation |  report a bug |  advanced search |  search howto |  statistics |  random bug |  login
Bug #27042 SPL: SeekableIterator seek() broken
Submitted: 2004-01-26 02:16 UTC Modified: 2004-01-28 17:53 UTC
Votes:1
Avg. Score:3.0 ± 0.0
Reproduced:1 of 1 (100.0%)
Same Version:1 (100.0%)
Same OS:0 (0.0%)
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
Welcome back! If you're the original bug submitter, here's where you can edit the bug or add additional notes.
If this is not your bug, you can add a comment by following this link.
If this is your bug, but you forgot your password, you can retrieve your password here.
Password:
Status:
Package:
Bug Type:
Summary:
From: adam at trachtenberg dot com
New email:
PHP Version: OS:

 

 [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

Add a Patch

Pull Requests

Add a Pull Request

History

AllCommentsChangesGit/SVN commitsRelated reports
 [2004-01-26 17:29 UTC] helly@php.net
This bug has been fixed in CVS.

Snapshots of the sources are packaged every three hours; this change
will be in the next snapshot. You can grab the snapshot at
http://snaps.php.net/.
 
Thank you for the report, and for helping us make PHP better.


 [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
This bug has been fixed in CVS.

Snapshots of the sources are packaged every three hours; this change
will be in the next snapshot. You can grab the snapshot at
http://snaps.php.net/.
 
Thank you for the report, and for helping us make PHP better.
 
PHP Copyright © 2001-2024 The PHP Group
All rights reserved.
Last updated: Fri Apr 26 03:01:32 2024 UTC