Code Coverage
 
Lines
Functions and Methods
Classes and Traits
Total
0.00% covered (danger)
0.00%
0 / 26
0.00% covered (danger)
0.00%
0 / 7
CRAP
0.00% covered (danger)
0.00%
0 / 1
ExplodeIterator
0.00% covered (danger)
0.00%
0 / 25
0.00% covered (danger)
0.00%
0 / 7
156
0.00% covered (danger)
0.00%
0 / 1
 __construct
0.00% covered (danger)
0.00%
0 / 5
0.00% covered (danger)
0.00%
0 / 1
2
 rewind
0.00% covered (danger)
0.00%
0 / 3
0.00% covered (danger)
0.00%
0 / 1
2
 refreshCurrent
0.00% covered (danger)
0.00%
0 / 7
0.00% covered (danger)
0.00%
0 / 1
20
 current
0.00% covered (danger)
0.00%
0 / 1
0.00% covered (danger)
0.00%
0 / 1
2
 key
0.00% covered (danger)
0.00%
0 / 1
0.00% covered (danger)
0.00%
0 / 1
2
 next
0.00% covered (danger)
0.00%
0 / 7
0.00% covered (danger)
0.00%
0 / 1
12
 valid
0.00% covered (danger)
0.00%
0 / 1
0.00% covered (danger)
0.00%
0 / 1
2
1<?php
2/**
3 * @license GPL-2.0-or-later
4 * @file
5 */
6
7namespace Wikimedia\StringUtils;
8
9use Iterator;
10
11/**
12 * An iterator which works exactly like:
13 *
14 * foreach ( explode( $delim, $s ) as $element ) {
15 *    ...
16 * }
17 *
18 * Except it doesn't use 193 byte per element
19 */
20class ExplodeIterator implements Iterator {
21    /** @var string */
22    private $subject;
23
24    /** @var int */
25    private $subjectLength;
26
27    /** @var string */
28    private $delim;
29
30    /** @var int */
31    private $delimLength;
32
33    /** @var int|false The position of the start of the line */
34    private $curPos;
35
36    /** @var int|false The position after the end of the next delimiter */
37    private $endPos;
38
39    /** @var string|false The current token */
40    private $current;
41
42    /**
43     * Construct a DelimIterator
44     * @param string $delim
45     * @param string $subject
46     */
47    public function __construct( $delim, $subject ) {
48        $this->subject = $subject;
49        $this->delim = $delim;
50
51        // Micro-optimisation (theoretical)
52        $this->subjectLength = strlen( $subject );
53        $this->delimLength = strlen( $delim );
54
55        $this->rewind();
56    }
57
58    public function rewind(): void {
59        $this->curPos = 0;
60        $this->endPos = strpos( $this->subject, $this->delim );
61        $this->refreshCurrent();
62    }
63
64    public function refreshCurrent() {
65        if ( $this->curPos === false ) {
66            $this->current = false;
67        } elseif ( $this->curPos >= $this->subjectLength ) {
68            $this->current = '';
69        } elseif ( $this->endPos === false ) {
70            $this->current = substr( $this->subject, $this->curPos );
71        } else {
72            $this->current = substr( $this->subject, $this->curPos, $this->endPos - $this->curPos );
73        }
74    }
75
76    /**
77     * @return string|false
78     */
79    #[\ReturnTypeWillChange]
80    public function current() {
81        return $this->current;
82    }
83
84    /**
85     * @return int|false Current position or boolean false if invalid
86     */
87    #[\ReturnTypeWillChange]
88    public function key() {
89        return $this->curPos;
90    }
91
92    public function next(): void {
93        if ( $this->endPos === false ) {
94            $this->curPos = false;
95        } else {
96            $this->curPos = $this->endPos + $this->delimLength;
97            if ( $this->curPos >= $this->subjectLength ) {
98                $this->endPos = false;
99            } else {
100                $this->endPos = strpos( $this->subject, $this->delim, $this->curPos );
101            }
102        }
103        $this->refreshCurrent();
104    }
105
106    public function valid(): bool {
107        return $this->curPos !== false;
108    }
109}
110
111/** @deprecated class alias since 1.44 */
112class_alias( ExplodeIterator::class, 'ExplodeIterator' );