wfMsgForContentNoTrans() was removed
[mediawiki.git] / includes / libs / ExplodeIterator.php
blob3b34d9bce0d56966d9265d27dbc3fc51e1bda74f
1 <?php
2 /**
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
18 * @file
21 /**
22 * An iterator which works exactly like:
24 * foreach ( explode( $delim, $s ) as $element ) {
25 * ...
26 * }
28 * Except it doesn't use 193 byte per element
30 class ExplodeIterator implements Iterator {
31 // The subject string
32 private $subject, $subjectLength;
34 // The delimiter
35 private $delim, $delimLength;
37 // The position of the start of the line
38 private $curPos;
40 // The position after the end of the next delimiter
41 private $endPos;
43 // The current token
44 private $current;
46 /**
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 );
59 $this->rewind();
62 public function rewind() {
63 $this->curPos = 0;
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 ) {
72 $this->current = '';
73 } elseif ( $this->endPos === false ) {
74 $this->current = substr( $this->subject, $this->curPos );
75 } else {
76 $this->current = substr( $this->subject, $this->curPos, $this->endPos - $this->curPos );
80 public function current() {
81 return $this->current;
84 /**
85 * @return int|bool Current position or boolean false if invalid
87 public function key() {
88 return $this->curPos;
91 /**
92 * @return string
94 public function next() {
95 if ( $this->endPos === false ) {
96 $this->curPos = false;
97 } else {
98 $this->curPos = $this->endPos + $this->delimLength;
99 if ( $this->curPos >= $this->subjectLength ) {
100 $this->endPos = false;
101 } else {
102 $this->endPos = strpos( $this->subject, $this->delim, $this->curPos );
105 $this->refreshCurrent();
107 return $this->current;
111 * @return bool
113 public function valid() {
114 return $this->curPos !== false;