|
1
|
|
|
<?php |
|
2
|
|
|
|
|
3
|
|
|
namespace Dazzle\Util\Buffer; |
|
4
|
|
|
|
|
5
|
|
|
class Buffer implements BufferInterface |
|
6
|
|
|
{ |
|
7
|
|
|
/** |
|
8
|
|
|
* @var string |
|
9
|
|
|
*/ |
|
10
|
|
|
private $data; |
|
11
|
|
|
|
|
12
|
|
|
/** |
|
13
|
|
|
* @param string $data |
|
14
|
|
|
*/ |
|
15
|
39 |
|
public function __construct($data = '') |
|
16
|
|
|
{ |
|
17
|
39 |
|
$this->data = (string) $data; |
|
18
|
39 |
|
} |
|
19
|
|
|
|
|
20
|
|
|
/** |
|
21
|
|
|
* |
|
22
|
|
|
*/ |
|
23
|
1 |
|
public function __destruct() |
|
24
|
|
|
{ |
|
25
|
1 |
|
unset($this->data); |
|
26
|
1 |
|
} |
|
27
|
|
|
|
|
28
|
|
|
/** |
|
29
|
|
|
* @override |
|
30
|
|
|
* @inheritDoc |
|
31
|
|
|
*/ |
|
32
|
25 |
|
public function __toString() |
|
33
|
|
|
{ |
|
34
|
25 |
|
return $this->data; |
|
35
|
|
|
} |
|
36
|
|
|
|
|
37
|
|
|
/** |
|
38
|
|
|
* @override |
|
39
|
|
|
* @inheritDoc |
|
40
|
|
|
*/ |
|
41
|
3 |
|
public function length() |
|
42
|
|
|
{ |
|
43
|
3 |
|
return strlen($this->data); |
|
44
|
|
|
} |
|
45
|
|
|
|
|
46
|
|
|
/** |
|
47
|
|
|
* @override |
|
48
|
|
|
* @inheritDoc |
|
49
|
|
|
*/ |
|
50
|
1 |
|
public function count() |
|
51
|
|
|
{ |
|
52
|
1 |
|
return $this->length(); |
|
53
|
|
|
} |
|
54
|
|
|
|
|
55
|
|
|
/** |
|
56
|
|
|
* @override |
|
57
|
|
|
* @inheritDoc |
|
58
|
|
|
*/ |
|
59
|
3 |
|
public function isEmpty() |
|
60
|
|
|
{ |
|
61
|
3 |
|
return '' === $this->data; |
|
62
|
|
|
} |
|
63
|
|
|
|
|
64
|
|
|
/** |
|
65
|
|
|
* @override |
|
66
|
|
|
* @inheritDoc |
|
67
|
|
|
*/ |
|
68
|
2 |
|
public function push($data) |
|
69
|
|
|
{ |
|
70
|
2 |
|
$this->data .= $data; |
|
71
|
2 |
|
} |
|
72
|
|
|
|
|
73
|
|
|
/** |
|
74
|
|
|
* @override |
|
75
|
|
|
* @inheritDoc |
|
76
|
|
|
*/ |
|
77
|
1 |
|
public function unshift($data) |
|
78
|
|
|
{ |
|
79
|
1 |
|
$this->data = $data . $this->data; |
|
80
|
1 |
|
} |
|
81
|
|
|
|
|
82
|
|
|
/** |
|
83
|
|
|
* @override |
|
84
|
|
|
* @inheritDoc |
|
85
|
|
|
*/ |
|
86
|
2 |
|
public function shift($length) |
|
87
|
|
|
{ |
|
88
|
2 |
|
$length = (int) $length; |
|
89
|
2 |
|
if (0 >= $length) |
|
90
|
|
|
{ |
|
91
|
1 |
|
return ''; |
|
92
|
|
|
} |
|
93
|
|
|
|
|
94
|
1 |
|
if (strlen($this->data) <= $length) |
|
95
|
|
|
{ |
|
96
|
|
|
$buffer = $this->data; |
|
97
|
|
|
$this->data = ''; |
|
98
|
|
|
return $buffer; |
|
99
|
|
|
} |
|
100
|
|
|
|
|
101
|
1 |
|
$buffer = (string) substr($this->data, 0, $length); |
|
102
|
|
|
|
|
103
|
1 |
|
$this->data = (string) substr($this->data, $length); |
|
104
|
|
|
|
|
105
|
1 |
|
return $buffer; |
|
106
|
|
|
} |
|
107
|
|
|
|
|
108
|
|
|
/** |
|
109
|
|
|
* @override |
|
110
|
|
|
* @inheritDoc |
|
111
|
|
|
*/ |
|
112
|
6 |
|
public function peek($length = 0, $offset = 0) |
|
113
|
|
|
{ |
|
114
|
6 |
|
$length = (int) $length; |
|
115
|
6 |
|
if (0 > $length) |
|
116
|
|
|
{ |
|
117
|
1 |
|
return ''; |
|
118
|
|
|
} |
|
119
|
5 |
|
else if (0 === $length) |
|
120
|
|
|
{ |
|
121
|
1 |
|
return $this->data; |
|
122
|
|
|
} |
|
123
|
|
|
|
|
124
|
4 |
|
$offset = (int) $offset; |
|
125
|
4 |
|
if (0 > $offset) |
|
126
|
|
|
{ |
|
127
|
1 |
|
$offset = 0; |
|
128
|
|
|
} |
|
129
|
|
|
|
|
130
|
4 |
|
if (0 === $offset && strlen($this->data) <= $length) |
|
131
|
|
|
{ |
|
132
|
1 |
|
return $this->data; |
|
133
|
|
|
} |
|
134
|
|
|
|
|
135
|
3 |
|
$result = (string) substr($this->data, $offset, $length); |
|
136
|
|
|
|
|
137
|
3 |
|
return $result; |
|
138
|
|
|
} |
|
139
|
|
|
|
|
140
|
|
|
/** |
|
141
|
|
|
* @override |
|
142
|
|
|
* @inheritDoc |
|
143
|
|
|
*/ |
|
144
|
2 |
|
public function pop($length) |
|
145
|
|
|
{ |
|
146
|
2 |
|
$length = (int) $length; |
|
147
|
2 |
|
if (0 >= $length) |
|
148
|
|
|
{ |
|
149
|
1 |
|
return ''; |
|
150
|
|
|
} |
|
151
|
|
|
|
|
152
|
1 |
|
$buffer = (string) substr($this->data, -$length); |
|
153
|
|
|
|
|
154
|
1 |
|
$this->data = (string) substr($this->data, 0, -$length); |
|
155
|
|
|
|
|
156
|
1 |
|
return $buffer; |
|
157
|
|
|
} |
|
158
|
|
|
|
|
159
|
|
|
/** |
|
160
|
|
|
* @override |
|
161
|
|
|
* @inheritDoc |
|
162
|
|
|
*/ |
|
163
|
4 |
|
public function remove($length, $offset = 0) |
|
164
|
|
|
{ |
|
165
|
4 |
|
$length = (int) $length; |
|
166
|
4 |
|
if (0 >= $length) |
|
167
|
|
|
{ |
|
168
|
1 |
|
return ''; |
|
169
|
|
|
} |
|
170
|
|
|
|
|
171
|
3 |
|
$offset = (int) $offset; |
|
172
|
3 |
|
if (0 > $offset) |
|
173
|
|
|
{ |
|
174
|
1 |
|
$offset = 0; |
|
175
|
|
|
} |
|
176
|
|
|
|
|
177
|
3 |
|
$buffer = (string) substr($this->data, $offset, $length); |
|
178
|
|
|
|
|
179
|
3 |
|
if (0 === $offset) |
|
180
|
|
|
{ |
|
181
|
2 |
|
$this->data = (string) substr($this->data, $length); |
|
182
|
|
|
} |
|
183
|
|
|
else |
|
184
|
|
|
{ |
|
185
|
1 |
|
$this->data = (string) (substr($this->data, 0, $offset) . substr($this->data, $offset + $length)); |
|
186
|
|
|
} |
|
187
|
|
|
|
|
188
|
3 |
|
return $buffer; |
|
189
|
|
|
} |
|
190
|
|
|
|
|
191
|
|
|
/** |
|
192
|
|
|
* @override |
|
193
|
|
|
* @inheritDoc |
|
194
|
|
|
*/ |
|
195
|
1 |
|
public function drain() |
|
196
|
|
|
{ |
|
197
|
1 |
|
$buffer = $this->data; |
|
198
|
1 |
|
$this->data = ''; |
|
199
|
1 |
|
return $buffer; |
|
200
|
|
|
} |
|
201
|
|
|
|
|
202
|
|
|
/** |
|
203
|
|
|
* @override |
|
204
|
|
|
* @inheritDoc |
|
205
|
|
|
*/ |
|
206
|
1 |
|
public function insert($string, $position) |
|
207
|
|
|
{ |
|
208
|
1 |
|
$this->data = substr_replace($this->data, $string, $position, 0); |
|
209
|
1 |
|
} |
|
210
|
|
|
|
|
211
|
|
|
/** |
|
212
|
|
|
* @override |
|
213
|
|
|
* @inheritDoc |
|
214
|
|
|
*/ |
|
215
|
1 |
|
public function replace($search, $replace) |
|
216
|
|
|
{ |
|
217
|
1 |
|
$this->data = str_replace($search, $replace, $this->data, $count); |
|
218
|
|
|
|
|
219
|
1 |
|
return $count; |
|
220
|
|
|
} |
|
221
|
|
|
|
|
222
|
|
|
/** |
|
223
|
|
|
* @override |
|
224
|
|
|
* @inheritDoc |
|
225
|
|
|
*/ |
|
226
|
2 |
|
public function search($string, $reverse = false) |
|
227
|
|
|
{ |
|
228
|
2 |
|
if ($reverse) |
|
229
|
|
|
{ |
|
230
|
1 |
|
return strrpos($this->data, $string); |
|
231
|
|
|
} |
|
232
|
|
|
|
|
233
|
1 |
|
return strpos($this->data, $string); |
|
234
|
|
|
} |
|
235
|
|
|
|
|
236
|
|
|
/** |
|
237
|
|
|
* @override |
|
238
|
|
|
* @inheritDoc |
|
239
|
|
|
*/ |
|
240
|
7 |
|
public function offsetExists($index) |
|
241
|
|
|
{ |
|
242
|
7 |
|
return isset($this->data[$index]); |
|
243
|
|
|
} |
|
244
|
|
|
|
|
245
|
|
|
/** |
|
246
|
|
|
* @override |
|
247
|
|
|
* @inheritDoc |
|
248
|
|
|
*/ |
|
249
|
5 |
|
public function offsetGet($index) |
|
250
|
|
|
{ |
|
251
|
5 |
|
return $this->data[$index]; |
|
252
|
|
|
} |
|
253
|
|
|
|
|
254
|
|
|
/** |
|
255
|
|
|
* @override |
|
256
|
|
|
* @inheritDoc |
|
257
|
|
|
*/ |
|
258
|
3 |
|
public function offsetSet($index, $data) |
|
259
|
|
|
{ |
|
260
|
3 |
|
$this->data = substr_replace($this->data, $data, $index, 1); |
|
261
|
3 |
|
} |
|
262
|
|
|
|
|
263
|
|
|
/** |
|
264
|
|
|
* @override |
|
265
|
|
|
* @inheritDoc |
|
266
|
|
|
*/ |
|
267
|
2 |
|
public function offsetUnset($index) |
|
268
|
|
|
{ |
|
269
|
2 |
|
if (isset($this->data[$index])) |
|
270
|
|
|
{ |
|
271
|
2 |
|
$this->data = substr_replace($this->data, null, $index, 1); |
|
272
|
|
|
} |
|
273
|
2 |
|
} |
|
274
|
|
|
|
|
275
|
|
|
/** |
|
276
|
|
|
* @override |
|
277
|
|
|
* @inheritDoc |
|
278
|
|
|
*/ |
|
279
|
9 |
|
public function getIterator() |
|
280
|
|
|
{ |
|
281
|
9 |
|
return new BufferIterator($this); |
|
282
|
|
|
} |
|
283
|
|
|
} |
|
284
|
|
|
|