This repository has been archived by the owner on Nov 10, 2018. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 0
/
StringReader.php
219 lines (192 loc) · 5.21 KB
/
StringReader.php
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
<?php
/*
* This file is part of the IO package.
*
* (c) EXSyst
*
* For the full copyright and license information, please view the LICENSE
* file that was distributed with this source code.
*/
namespace EXSyst\Component\IO;
class StringReader
{
private $data;
private $start;
private $end;
private $offset = 0;
private $line = 0;
private $row = 0;
/**
* @param string $data
* @param int $start
* @param int|null $end
*/
public function __construct($data, $start = 0, $end = null)
{
$this->data = strval($data);
$this->start = intval($start);
$size = strlen($this->data);
$this->end = null === $end ? $size : min($size, $end);
if (0 !== $start) {
$this->moveCursor(substr($data, 0, $start));
}
}
/**
* @param callable $transactionFn
* @param bool $restoreOnFalseReturn
*
* @return mixed $transactionFn return
*/
public function transact(callable $transactionFn, $restoreOnFalseReturn = false)
{
$state = $this->captureState();
try {
$retval = call_user_func($transactionFn);
} catch (\Exception $ex) {
$state->restore();
throw $ex;
}
if ($restoreOnFalseReturn && false === $retval) {
$state->restore();
}
return $retval;
}
/**
* @param string[]|\Traversable
*
* @param string|null
*/
public function eatAny($strings) {
foreach ($strings as $string) {
if ($this->eat($string)) {
return $string;
}
}
}
/**
* @param string $string
*
* @return bool
*/
public function eat($string)
{
$state = $this->captureState();
if ($string === $this->read(strlen($string), true)) {
return true;
} else {
$state->restore();
return false;
}
}
/**
* @param string $mask
* @param int|null $maxLength
*
* @return string
*/
public function eatSpan($mask, $maxLength = null)
{
if (null === $maxLength) {
$maxLength = $this->getRemainingByteCount();
}
return $this->read(strspn($this->data, $mask, $this->offset, $maxLength));
}
/**
* @param string $mask
* @param int|null $maxLength
*
* @return string
*/
public function eatCSpan($mask, $maxLength = null)
{
if (null === $maxLength) {
$maxLength = $this->getRemainingByteCount();
}
return $this->read(strcspn($this->data, $mask, $this->offset, $maxLength));
}
public function getOffset()
{
return $this->offset;
}
public function getLine()
{
return $this->line;
}
public function getRow()
{
return $this->row;
}
/**
* Counts how many bytes were consumed from the source.
*
* @return int
*/
public function getConsumedByteCount()
{
return $this->offset - $this->start;
}
/**
* Counts how many bytes remain in the source.
*
* @return int
*/
public function getRemainingByteCount()
{
return $this->end - $this->offset;
}
/**
* Determines whether the source is fully consumed.
*
* @return bool
*/
public function isFullyConsumed()
{
return $this->offset >= $this->end;
}
/**
* Reads and consumes data from the source.
*
* @param int $byteCount Number of bytes to read
* @param bool $allowIncomplete true to accept any amount of data smaller than or equal to the requested amount, false (default) to throw an exception if the exact requested amount cannot be read
*
* @throws Exception\UnderflowException If the exact requested amount cannot be read
*
* @return string The data that was just read
*/
public function read($byteCount, $allowIncomplete = false)
{
$maxByteCount = $this->getRemainingByteCount();
if (!$allowIncomplete && $maxByteCount < $byteCount) {
throw new Exception\UnderflowException('The source doesn\'t have enough remaining data to fulfill the request');
}
$byteCount = max(min($byteCount, $maxByteCount), 0);
$substr = substr($this->data, $this->offset, $byteCount);
$this->moveCursor($substr);
return $substr;
}
/**
* Captures the source's current state, which then may be subsequently restored.
*
* @return StateInterface The source's current state
*/
public function captureState()
{
return new StringSourceState($this->offset, $this->line, $this->row);
}
/**
* @return string This object's string representation
*/
public function __toString()
{
return $this->data;
}
private function moveCursor(string $value = null)
{
$this->offset += strlen($value);
$this->row += strlen($value);
if ($lines = preg_match_all("/\r\n?|\n\r?/", $value)) {
$this->line += $lines;
$this->row = strlen($value) - max(strrpos($value, "\r"), strrpos($value, "\n")) - 1;
}
}
}