Debian Codegraph @

includes/libs/ExplodeIterator.php

raw symbols SCIP
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.
7 *
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.
12 *
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
17 *
18 * @file
19 */
20
21 /**
22 * An iterator which works exactly like:
23 *
24 * foreach ( explode( $delim, $s ) as $element ) {
25 * ...
26 * }
27 *
28 * Except it doesn't use 193 byte per element
29 */
30 class ExplodeIterator implements Iterator {
31 /** @var string */
32 private $subject;
33
34 /** @var int */
35 private $subjectLength;
36
37 /** @var string */
38 private $delim;
39
40 /** @var int */
41 private $delimLength;
42
43 /** @var int|false The position of the start of the line */
44 private $curPos;
45
46 /** @var int|false The position after the end of the next delimiter */
47 private $endPos;
48
49 /** @var string|false The current token */
50 private $current;
51
52 /**
53 * Construct a DelimIterator
54 * @param string $delim
55 * @param string $subject
56 */
57 public function __construct( $delim, $subject ) {
58 $this->subject = $subject;
59 $this->delim = $delim;
60
61 // Micro-optimisation (theoretical)
62 $this->subjectLength = strlen( $subject );
63 $this->delimLength = strlen( $delim );
64
65 $this->rewind();
66 }
67
68 public function rewind(): void {
69 $this->curPos = 0;
70 $this->endPos = strpos( $this->subject, $this->delim );
71 $this->refreshCurrent();
72 }
73
74 public function refreshCurrent() {
75 if ( $this->curPos === false ) {
76 $this->current = false;
77 } elseif ( $this->curPos >= $this->subjectLength ) {
78 $this->current = '';
79 } elseif ( $this->endPos === false ) {
80 $this->current = substr( $this->subject, $this->curPos );
81 } else {
82 $this->current = substr( $this->subject, $this->curPos, $this->endPos - $this->curPos );
83 }
84 }
85
86 /**
87 * @return string|false
88 */
89 #[\ReturnTypeWillChange]
90 public function current() {
91 return $this->current;
92 }
93
94 /**
95 * @return int|false Current position or boolean false if invalid
96 */
97 #[\ReturnTypeWillChange]
98 public function key() {
99 return $this->curPos;
100 }
101
102 /**
103 * @return void
104 */
105 public function next(): void {
106 if ( $this->endPos === false ) {
107 $this->curPos = false;
108 } else {
109 $this->curPos = $this->endPos + $this->delimLength;
110 if ( $this->curPos >= $this->subjectLength ) {
111 $this->endPos = false;
112 } else {
113 $this->endPos = strpos( $this->subject, $this->delim, $this->curPos );
114 }
115 }
116 $this->refreshCurrent();
117 }
118
119 /**
120 * @return bool
121 */
122 public function valid(): bool {
123 return $this->curPos !== false;
124 }
125 }