3 * This program is free software; you can redistribute it and/or modify
4 * it under the terms of the GNU General Public License as published by
5 * the Free Software Foundation; either version 2 of the License, or
6 * (at your option) any later version.
8 * This program is distributed in the hope that it will be useful,
9 * but WITHOUT ANY WARRANTY; without even the implied warranty of
10 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
11 * GNU General Public License for more details.
13 * You should have received a copy of the GNU General Public License along
14 * with this program; if not, write to the Free Software Foundation, Inc.,
15 * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
16 * http://www.gnu.org/copyleft/gpl.html
22 * An iterator which works exactly like:
24 * foreach ( explode( $delim, $s ) as $element ) {
28 * Except it doesn't use 193 byte per element
30 class ExplodeIterator
implements Iterator
{
32 private $subject, $subjectLength;
35 private $delim, $delimLength;
37 // The position of the start of the line
40 // The position after the end of the next delimiter
43 /** @var string|false The current token */
47 * Construct a DelimIterator
48 * @param string $delim
49 * @param string $subject
51 public function __construct( $delim, $subject ) {
52 $this->subject
= $subject;
53 $this->delim
= $delim;
55 // Micro-optimisation (theoretical)
56 $this->subjectLength
= strlen( $subject );
57 $this->delimLength
= strlen( $delim );
62 public function rewind() {
64 $this->endPos
= strpos( $this->subject
, $this->delim
);
65 $this->refreshCurrent();
68 public function refreshCurrent() {
69 if ( $this->curPos
=== false ) {
70 $this->current
= false;
71 } elseif ( $this->curPos
>= $this->subjectLength
) {
73 } elseif ( $this->endPos
=== false ) {
74 $this->current
= substr( $this->subject
, $this->curPos
);
76 $this->current
= substr( $this->subject
, $this->curPos
, $this->endPos
- $this->curPos
);
80 public function current() {
81 return $this->current
;
85 * @return int|bool Current position or boolean false if invalid
87 public function key() {
94 public function next() {
95 if ( $this->endPos
=== false ) {
96 $this->curPos
= false;
98 $this->curPos
= $this->endPos +
$this->delimLength
;
99 if ( $this->curPos
>= $this->subjectLength
) {
100 $this->endPos
= false;
102 $this->endPos
= strpos( $this->subject
, $this->delim
, $this->curPos
);
105 $this->refreshCurrent();
107 return $this->current
;
113 public function valid() {
114 return $this->curPos
!== false;