1
|
|
|
<?php |
2
|
|
|
/** |
3
|
|
|
* @author: Viskov Sergey |
4
|
|
|
* @date: 3/18/16 |
5
|
|
|
* @time: 1:15 PM |
6
|
|
|
*/ |
7
|
|
|
|
8
|
|
|
namespace LTDBeget\stringstream; |
9
|
|
|
|
10
|
|
|
use ArrayIterator; |
11
|
|
|
use LTDBeget\ascii\AsciiChar; |
12
|
|
|
|
13
|
|
|
/** |
14
|
|
|
* String stream data structure |
15
|
|
|
* Class StringStream |
16
|
|
|
* @package LTDBeget\stringstream |
17
|
|
|
*/ |
18
|
|
|
class StringStream |
19
|
|
|
{ |
20
|
|
|
/** |
21
|
|
|
* StringStream constructor. |
22
|
|
|
* @param string $string |
23
|
|
|
*/ |
24
|
|
|
public function __construct(string $string) |
25
|
|
|
{ |
26
|
|
|
$this->stream = $this->makeIterator($string); |
|
|
|
|
27
|
|
|
$this->pointerAtStart = true; |
28
|
|
|
$this->pointerAtEnd = false; |
29
|
|
|
} |
30
|
|
|
|
31
|
|
|
/** |
32
|
|
|
* Current char of stream |
33
|
|
|
* @return string |
34
|
|
|
*/ |
35
|
|
|
public function current() : string |
36
|
|
|
{ |
37
|
|
|
return current($this->stream); |
38
|
|
|
} |
39
|
|
|
|
40
|
|
|
/** |
41
|
|
|
* @return int |
42
|
|
|
*/ |
43
|
|
|
public function ord() : int |
44
|
|
|
{ |
45
|
|
|
return ord($this->current()); |
46
|
|
|
} |
47
|
|
|
|
48
|
|
|
/** |
49
|
|
|
* Current char of stream as AsciiChar |
50
|
|
|
* @return AsciiChar |
51
|
|
|
* @throws \LogicException |
52
|
|
|
* @throws \InvalidArgumentException |
53
|
|
|
*/ |
54
|
|
|
public function currentAscii() : AsciiChar |
55
|
|
|
{ |
56
|
|
|
return AsciiChar::get($this->ord()); |
57
|
|
|
} |
58
|
|
|
|
59
|
|
|
/** |
60
|
|
|
* Position in stream of current char |
61
|
|
|
* @return int |
62
|
|
|
*/ |
63
|
|
|
public function position() : int |
64
|
|
|
{ |
65
|
|
|
return key($this->stream); |
66
|
|
|
} |
67
|
|
|
|
68
|
|
|
/** |
69
|
|
|
* go to next char in stream |
70
|
|
|
*/ |
71
|
|
|
public function next() |
72
|
|
|
{ |
73
|
|
|
$this->pointerAtStart = false; |
74
|
|
|
$this->pointerAtEnd = next($this->stream) === false; |
75
|
|
|
} |
76
|
|
|
|
77
|
|
|
/** |
78
|
|
|
* go to previous char of stream |
79
|
|
|
*/ |
80
|
|
|
public function previous() |
81
|
|
|
{ |
82
|
|
|
$this->pointerAtEnd = false; |
83
|
|
|
$this->pointerAtStart = prev($this->stream) === false; |
84
|
|
|
} |
85
|
|
|
|
86
|
|
|
/** |
87
|
|
|
* go to start of stream |
88
|
|
|
*/ |
89
|
|
|
public function start() |
90
|
|
|
{ |
91
|
|
|
reset($this->stream); |
92
|
|
|
} |
93
|
|
|
|
94
|
|
|
/** |
95
|
|
|
* is start of stream |
96
|
|
|
* @return bool |
97
|
|
|
*/ |
98
|
|
|
public function isStart() : bool |
99
|
|
|
{ |
100
|
|
|
return $this->pointerAtStart; |
101
|
|
|
} |
102
|
|
|
|
103
|
|
|
/** |
104
|
|
|
* go to end of stream |
105
|
|
|
*/ |
106
|
|
|
public function end() |
107
|
|
|
{ |
108
|
|
|
end($this->stream); |
109
|
|
|
} |
110
|
|
|
|
111
|
|
|
/** |
112
|
|
|
* is end of stream |
113
|
|
|
* @return bool |
114
|
|
|
*/ |
115
|
|
|
public function isEnd() : bool |
116
|
|
|
{ |
117
|
|
|
return $this->pointerAtEnd; |
118
|
|
|
} |
119
|
|
|
|
120
|
|
|
/** |
121
|
|
|
* ignore chars in stream while it is white space |
122
|
|
|
* @throws \InvalidArgumentException |
123
|
|
|
* @throws \LogicException |
124
|
|
|
*/ |
125
|
|
|
public function ignoreWhitespace() |
126
|
|
|
{ |
127
|
|
|
ignoreWhitespace: |
128
|
|
|
if (!$this->isEnd() && $this->currentAscii()->isWhiteSpace()) { |
129
|
|
|
$this->next(); |
130
|
|
|
goto ignoreWhitespace; |
131
|
|
|
} |
132
|
|
|
} |
133
|
|
|
|
134
|
|
|
/** |
135
|
|
|
* ignore chars in stream while it is horizontal space |
136
|
|
|
* @throws \LogicException |
137
|
|
|
* @throws \InvalidArgumentException |
138
|
|
|
*/ |
139
|
|
|
public function ignoreHorizontalSpace() |
140
|
|
|
{ |
141
|
|
|
ignoreHorizontalSpace: |
142
|
|
|
if (!$this->isEnd() && $this->currentAscii()->isHorizontalSpace()) { |
143
|
|
|
$this->next(); |
144
|
|
|
goto ignoreHorizontalSpace; |
145
|
|
|
} |
146
|
|
|
} |
147
|
|
|
|
148
|
|
|
/** |
149
|
|
|
* ignore chars in stream while it is vertical space |
150
|
|
|
* @throws \InvalidArgumentException |
151
|
|
|
* @throws \LogicException |
152
|
|
|
*/ |
153
|
|
|
public function ignoreVerticalSpace() |
154
|
|
|
{ |
155
|
|
|
ignoreHorizontalSpace: |
156
|
|
|
if (!$this->isEnd() && $this->currentAscii()->isVerticalSpace()) { |
157
|
|
|
$this->next(); |
158
|
|
|
goto ignoreHorizontalSpace; |
159
|
|
|
} |
160
|
|
|
} |
161
|
|
|
|
162
|
|
|
/** |
163
|
|
|
* @param string $string |
164
|
|
|
* @return ArrayIterator |
165
|
|
|
*/ |
166
|
|
|
private function makeIterator(string $string) : ArrayIterator |
167
|
|
|
{ |
168
|
|
|
return new ArrayIterator(preg_split('#(?<!^)(?!$)#u', $string)); |
169
|
|
|
} |
170
|
|
|
|
171
|
|
|
/** |
172
|
|
|
* @var array |
173
|
|
|
*/ |
174
|
|
|
private $stream; |
175
|
|
|
|
176
|
|
|
/** |
177
|
|
|
* if next returns false it member will be true, else false |
178
|
|
|
* @var bool |
179
|
|
|
*/ |
180
|
|
|
private $pointerAtEnd; |
181
|
|
|
|
182
|
|
|
/** |
183
|
|
|
* if prev returns false it member will be true, else false |
184
|
|
|
* @var bool |
185
|
|
|
*/ |
186
|
|
|
private $pointerAtStart; |
187
|
|
|
} |
188
|
|
|
|
Our type inference engine has found an assignment to a property that is incompatible with the declared type of that property.
Either this assignment is in error or the assigned type should be added to the documentation/type hint for that property..