@@ -23,382 +23,382 @@ |
||
23 | 23 | */ |
24 | 24 | abstract class BitString implements IComparable, \ArrayAccess |
25 | 25 | { |
26 | - protected $bits; |
|
27 | - protected $len; |
|
28 | - |
|
29 | - |
|
30 | - /** |
|
31 | - * @param string $bits |
|
32 | - */ |
|
33 | - protected function __construct($bits) |
|
34 | - { |
|
35 | - if (!preg_match('~^[01]*$~', $bits)) { |
|
36 | - throw new \InvalidArgumentException('bits'); |
|
37 | - } |
|
38 | - |
|
39 | - $this->bits = $bits; |
|
40 | - $this->len = strlen($bits); |
|
41 | - } |
|
42 | - |
|
43 | - |
|
44 | - /** |
|
45 | - * @return string textual representation of this bit string (index 0 is the leftmost bit): a string of 1's and 0's |
|
46 | - */ |
|
47 | - public function toString() |
|
48 | - { |
|
49 | - return $this->bits; |
|
50 | - } |
|
51 | - |
|
52 | - /** |
|
53 | - * @return int number of bits in the bit string |
|
54 | - */ |
|
55 | - public function getLength() |
|
56 | - { |
|
57 | - return $this->len; |
|
58 | - } |
|
59 | - |
|
60 | - /** |
|
61 | - * @return bool whether all the bits are zero |
|
62 | - */ |
|
63 | - public function isZero() |
|
64 | - { |
|
65 | - return !$this->isNonZero(); |
|
66 | - } |
|
67 | - |
|
68 | - /** |
|
69 | - * @return bool whether some bit is one |
|
70 | - */ |
|
71 | - public function isNonZero() |
|
72 | - { |
|
73 | - for ($i = 0; $i < $this->len; $i++) { |
|
74 | - if ($this->bits[$i] == '1') { |
|
75 | - return true; |
|
76 | - } |
|
77 | - } |
|
78 | - |
|
79 | - return false; |
|
80 | - } |
|
81 | - |
|
82 | - /** |
|
83 | - * @param BitString $other |
|
84 | - * @return bool whether this and the other bit string are of the same type, have the same bits and same length |
|
85 | - */ |
|
86 | - public function equals($other) |
|
87 | - { |
|
88 | - if ($other === null) { |
|
89 | - return null; |
|
90 | - } |
|
91 | - return (get_class($this) == get_class($other) && $this->bits == $other->bits); |
|
92 | - } |
|
93 | - |
|
94 | - /** |
|
95 | - * @param BitString $other any bit string of any length |
|
96 | - * @return bool whether this and the other bit string have the same bits, i.e., are the same 1's and 0's strings |
|
97 | - */ |
|
98 | - public function bitEquals(BitString $other) |
|
99 | - { |
|
100 | - return ($this->bits === $other->bits); // ===: PHP would otherwise convert both strings to integers |
|
101 | - } |
|
102 | - |
|
103 | - /** |
|
104 | - * Finds out whether the operands intersect at a 1 bit. |
|
105 | - * |
|
106 | - * Works for any lengths of the bit strings. The shorter bit string is right-padded with 0's. |
|
107 | - * |
|
108 | - * @param BitString $other any bit string of any length |
|
109 | - * @return bool whether this and the other bit string share at least one set bit at the same offset |
|
110 | - */ |
|
111 | - public function intersects(BitString $other) |
|
112 | - { |
|
113 | - for ($i = min($this->len, $other->len) - 1; $i >= 0; $i--) { |
|
114 | - if ($this->bits[$i] == '1' && $other->bits[$i] == '1') { |
|
115 | - return true; |
|
116 | - } |
|
117 | - } |
|
118 | - |
|
119 | - return false; |
|
120 | - } |
|
121 | - |
|
122 | - /** |
|
123 | - * Extracts a bit substring from this bit string. |
|
124 | - * |
|
125 | - * Note the semantics are according to the SQL standard, which PostgreSQL implements. That is, the arguments do NOT |
|
126 | - * work the same as for PHP {@link substr()} function. |
|
127 | - * |
|
128 | - * Examples: |
|
129 | - * - `substring(2)` on `'1101001'` yields `'101001'` |
|
130 | - * - `substring(2, 4)` on `'1101001'` yields `'1010'` |
|
131 | - * - `substring(2, 0)` on `'1101001'` yields `''` |
|
132 | - * - `substring(-2, 4)` on `'1101001'` yields `'1'` |
|
133 | - * |
|
134 | - * @param int $from position to start at; |
|
135 | - * one-based - e.g., <tt>$from = 2</tt> omits the first character; |
|
136 | - * if less than one, it yields the start of the string, but counting from the given (negative or |
|
137 | - * zero) position, effectively decreasing the <tt>$for</tt> argument, if given |
|
138 | - * @param int|null $for number of characters to take; must be non-negative; |
|
139 | - * <tt>null</tt> for the rest of the string |
|
140 | - * @return FixedBitString the substring of the bit string |
|
141 | - * @throws UndefinedOperationException when <tt>$for</tt> is negative |
|
142 | - */ |
|
143 | - public function substring($from, $for = null) |
|
144 | - { |
|
145 | - if ($for < 0) { |
|
146 | - throw new UndefinedOperationException('negative number of bits to take'); |
|
147 | - } |
|
148 | - |
|
149 | - $offset = max(0, $from - 1); |
|
150 | - if ($offset >= $this->len) { |
|
151 | - return FixedBitString::fromString(''); |
|
152 | - } |
|
153 | - |
|
154 | - if ($for === null) { |
|
155 | - $len = $this->len; |
|
156 | - } |
|
157 | - elseif ($from >= 0) { |
|
158 | - $len = $for; |
|
159 | - } |
|
160 | - else { |
|
161 | - $len = max(0, $for + $from - 1); |
|
162 | - } |
|
163 | - |
|
164 | - return FixedBitString::fromString(substr($this->bits, $offset, $len)); |
|
165 | - } |
|
166 | - |
|
167 | - /** |
|
168 | - * Concatenates this bit string with another one. |
|
169 | - * |
|
170 | - * The result is a bit string with bits of this bit string followed by the bits from the `$concatenated` bit string. |
|
171 | - * |
|
172 | - * If both the operands are fixed-length of length `A` and `B`, the result is a fixed-length bit string of length |
|
173 | - * `A+B`. Otherwise, the result is a variable-length bit string. |
|
174 | - * |
|
175 | - * @param BitString $concatenated |
|
176 | - * @return VarBitString bit string made up by concatenating the <tt>$concatenated</tt> after this bit string |
|
177 | - */ |
|
178 | - public function concat(BitString $concatenated) |
|
179 | - { |
|
180 | - return VarBitString::fromString($this->bits . $concatenated->bits); |
|
181 | - } |
|
182 | - |
|
183 | - /** |
|
184 | - * Bitwise AND operation. Only legal for operands of equal bit lengths. |
|
185 | - * |
|
186 | - * @param BitString $other the other operand |
|
187 | - * @return FixedBitString new bit string: <tt>$this & $other</tt> |
|
188 | - * @throws UndefinedOperationException if the operands are of different bit lengths |
|
189 | - */ |
|
190 | - public function bitAnd(BitString $other) |
|
191 | - { |
|
192 | - if ($this->len != $other->len) { |
|
193 | - throw new UndefinedOperationException('operands are of different bit lengths'); |
|
194 | - } |
|
195 | - |
|
196 | - $res = str_repeat('0', $this->len); |
|
197 | - for ($i = 0; $i < $this->len; $i++) { |
|
198 | - if ($this->bits[$i] == '1' && $other->bits[$i] == '1') { |
|
199 | - $res[$i] = '1'; |
|
200 | - } |
|
201 | - } |
|
202 | - |
|
203 | - return FixedBitString::fromString($res); |
|
204 | - } |
|
205 | - |
|
206 | - /** |
|
207 | - * Bitwise OR operation. Only legal for operands of equal bit lengths. |
|
208 | - * |
|
209 | - * @param BitString $other the other operand |
|
210 | - * @return FixedBitString new bit string: <tt>$this | $other</tt> |
|
211 | - * @throws UndefinedOperationException if the operands are of different bit lengths |
|
212 | - */ |
|
213 | - public function bitOr(BitString $other) |
|
214 | - { |
|
215 | - if ($this->len != $other->len) { |
|
216 | - throw new UndefinedOperationException('operands are of different bit lengths'); |
|
217 | - } |
|
218 | - |
|
219 | - $res = str_repeat('1', $this->len); |
|
220 | - for ($i = 0; $i < $this->len; $i++) { |
|
221 | - if ($this->bits[$i] == '0' && $other->bits[$i] == '0') { |
|
222 | - $res[$i] = '0'; |
|
223 | - } |
|
224 | - } |
|
225 | - |
|
226 | - return FixedBitString::fromString($res); |
|
227 | - } |
|
228 | - |
|
229 | - /** |
|
230 | - * Bitwise exclusive OR operation. Only legal for operands of equal bit lengths. |
|
231 | - * |
|
232 | - * @param BitString $other the other operand |
|
233 | - * @return FixedBitString new bit string: <tt>$this ^ $other</tt> |
|
234 | - * @throws UndefinedOperationException if the operands are of different bit lengths |
|
235 | - */ |
|
236 | - public function bitXor(BitString $other) |
|
237 | - { |
|
238 | - if ($this->len != $other->len) { |
|
239 | - throw new UndefinedOperationException('operands are of different bit lengths'); |
|
240 | - } |
|
241 | - |
|
242 | - $res = str_repeat('0', $this->len); |
|
243 | - for ($i = 0; $i < $this->len; $i++) { |
|
244 | - if ($this->bits[$i] != $other->bits[$i]) { |
|
245 | - $res[$i] = '1'; |
|
246 | - } |
|
247 | - } |
|
248 | - |
|
249 | - return FixedBitString::fromString($res); |
|
250 | - } |
|
251 | - |
|
252 | - /** |
|
253 | - * Bitwise negation, i.e., reverses all bits of the bit string. |
|
254 | - * |
|
255 | - * @return FixedBitString new bit string: <tt>~$this</tt> |
|
256 | - */ |
|
257 | - public function bitNot() |
|
258 | - { |
|
259 | - return FixedBitString::fromString(strtr($this->bits, '01', '10')); |
|
260 | - } |
|
261 | - |
|
262 | - /** |
|
263 | - * Shifts the bits to the left. |
|
264 | - * |
|
265 | - * The length of the bit string is preserved, thus, the `$shift` left trailing bits are discarded. The shifted bit |
|
266 | - * positions are filled with 0's. |
|
267 | - * |
|
268 | - * @param int $shift number of positions to shift the bits to the left; |
|
269 | - * might even be negative (results in shifting to the right by <tt>-$shift</tt>) |
|
270 | - * @return FixedBitString a bit string with bits shifted by <tt>$shift</tt> to the left |
|
271 | - */ |
|
272 | - public function bitShiftLeft($shift) |
|
273 | - { |
|
274 | - if ($shift < 0) { |
|
275 | - return $this->bitShiftRight(-$shift); |
|
276 | - } |
|
277 | - |
|
278 | - $pad = min($this->len, $shift); |
|
279 | - $prefix = ($pad == $this->len ? '' : substr($this->bits, $pad)); |
|
280 | - return FixedBitString::fromString($prefix . str_repeat('0', $pad)); |
|
281 | - } |
|
282 | - |
|
283 | - /** |
|
284 | - * Shifts the bits to the right. |
|
285 | - * |
|
286 | - * The length of the bit string is preserved, thus, the `$shift` right trailing bits are discarded. The shifted bit |
|
287 | - * positions are filled with 0's. |
|
288 | - * |
|
289 | - * @param int $shift number of positions to shift the bits to the right; |
|
290 | - * might even be negative (results in shifting to the left by <tt>-$shift</tt>) |
|
291 | - * @return FixedBitString a bit string with bits shifted by <tt>$shift</tt> to the right |
|
292 | - */ |
|
293 | - public function bitShiftRight($shift) |
|
294 | - { |
|
295 | - if ($shift < 0) { |
|
296 | - return $this->bitShiftLeft(-$shift); |
|
297 | - } |
|
298 | - |
|
299 | - $pad = min($this->len, $shift); |
|
300 | - $postfix = substr($this->bits, 0, $this->len - $pad); |
|
301 | - return FixedBitString::fromString(str_repeat('0', $pad) . $postfix); |
|
302 | - } |
|
303 | - |
|
304 | - /** |
|
305 | - * Rotates the bits to the left. |
|
306 | - * |
|
307 | - * @param int $rot the length of the rotation |
|
308 | - * @return FixedBitString a bit string with <tt>$rot</tt> (mod length) leftmost bits moved to the back of the bit |
|
309 | - * string |
|
310 | - */ |
|
311 | - public function bitRotateLeft($rot) |
|
312 | - { |
|
313 | - if ($rot < 0) { |
|
314 | - return $this->bitRotateRight(-$rot); |
|
315 | - } |
|
316 | - if ($this->len == 0) { |
|
317 | - return FixedBitString::fromString(''); |
|
318 | - } |
|
319 | - |
|
320 | - $pos = $rot % $this->len; |
|
321 | - return FixedBitString::fromString(substr($this->bits, $pos) . substr($this->bits, 0, $pos)); |
|
322 | - } |
|
323 | - |
|
324 | - /** |
|
325 | - * Rotates the bits to the right. |
|
326 | - * |
|
327 | - * @param int $rot the length of the rotation |
|
328 | - * @return FixedBitString a bit string with <tt>$rot</tt> (mod length) rightmost bits moved to the front of the bit |
|
329 | - * string |
|
330 | - */ |
|
331 | - public function bitRotateRight($rot) |
|
332 | - { |
|
333 | - if ($rot < 0) { |
|
334 | - return $this->bitRotateLeft(-$rot); |
|
335 | - } |
|
336 | - if ($this->len == 0) { |
|
337 | - return FixedBitString::fromString(''); |
|
338 | - } |
|
339 | - |
|
340 | - $pos = $this->len - ($rot % $this->len); |
|
341 | - return FixedBitString::fromString(substr($this->bits, $pos) . substr($this->bits, 0, $pos)); |
|
342 | - } |
|
343 | - |
|
344 | - |
|
345 | - public function __toString() |
|
346 | - { |
|
347 | - return $this->toString(); |
|
348 | - } |
|
349 | - |
|
350 | - |
|
351 | - /** |
|
352 | - * @param int $offset the bit to know existence of; 0 for the leftmost bit, 1 for the next bit, -1 for the rightmost |
|
26 | + protected $bits; |
|
27 | + protected $len; |
|
28 | + |
|
29 | + |
|
30 | + /** |
|
31 | + * @param string $bits |
|
32 | + */ |
|
33 | + protected function __construct($bits) |
|
34 | + { |
|
35 | + if (!preg_match('~^[01]*$~', $bits)) { |
|
36 | + throw new \InvalidArgumentException('bits'); |
|
37 | + } |
|
38 | + |
|
39 | + $this->bits = $bits; |
|
40 | + $this->len = strlen($bits); |
|
41 | + } |
|
42 | + |
|
43 | + |
|
44 | + /** |
|
45 | + * @return string textual representation of this bit string (index 0 is the leftmost bit): a string of 1's and 0's |
|
46 | + */ |
|
47 | + public function toString() |
|
48 | + { |
|
49 | + return $this->bits; |
|
50 | + } |
|
51 | + |
|
52 | + /** |
|
53 | + * @return int number of bits in the bit string |
|
54 | + */ |
|
55 | + public function getLength() |
|
56 | + { |
|
57 | + return $this->len; |
|
58 | + } |
|
59 | + |
|
60 | + /** |
|
61 | + * @return bool whether all the bits are zero |
|
62 | + */ |
|
63 | + public function isZero() |
|
64 | + { |
|
65 | + return !$this->isNonZero(); |
|
66 | + } |
|
67 | + |
|
68 | + /** |
|
69 | + * @return bool whether some bit is one |
|
70 | + */ |
|
71 | + public function isNonZero() |
|
72 | + { |
|
73 | + for ($i = 0; $i < $this->len; $i++) { |
|
74 | + if ($this->bits[$i] == '1') { |
|
75 | + return true; |
|
76 | + } |
|
77 | + } |
|
78 | + |
|
79 | + return false; |
|
80 | + } |
|
81 | + |
|
82 | + /** |
|
83 | + * @param BitString $other |
|
84 | + * @return bool whether this and the other bit string are of the same type, have the same bits and same length |
|
85 | + */ |
|
86 | + public function equals($other) |
|
87 | + { |
|
88 | + if ($other === null) { |
|
89 | + return null; |
|
90 | + } |
|
91 | + return (get_class($this) == get_class($other) && $this->bits == $other->bits); |
|
92 | + } |
|
93 | + |
|
94 | + /** |
|
95 | + * @param BitString $other any bit string of any length |
|
96 | + * @return bool whether this and the other bit string have the same bits, i.e., are the same 1's and 0's strings |
|
97 | + */ |
|
98 | + public function bitEquals(BitString $other) |
|
99 | + { |
|
100 | + return ($this->bits === $other->bits); // ===: PHP would otherwise convert both strings to integers |
|
101 | + } |
|
102 | + |
|
103 | + /** |
|
104 | + * Finds out whether the operands intersect at a 1 bit. |
|
105 | + * |
|
106 | + * Works for any lengths of the bit strings. The shorter bit string is right-padded with 0's. |
|
107 | + * |
|
108 | + * @param BitString $other any bit string of any length |
|
109 | + * @return bool whether this and the other bit string share at least one set bit at the same offset |
|
110 | + */ |
|
111 | + public function intersects(BitString $other) |
|
112 | + { |
|
113 | + for ($i = min($this->len, $other->len) - 1; $i >= 0; $i--) { |
|
114 | + if ($this->bits[$i] == '1' && $other->bits[$i] == '1') { |
|
115 | + return true; |
|
116 | + } |
|
117 | + } |
|
118 | + |
|
119 | + return false; |
|
120 | + } |
|
121 | + |
|
122 | + /** |
|
123 | + * Extracts a bit substring from this bit string. |
|
124 | + * |
|
125 | + * Note the semantics are according to the SQL standard, which PostgreSQL implements. That is, the arguments do NOT |
|
126 | + * work the same as for PHP {@link substr()} function. |
|
127 | + * |
|
128 | + * Examples: |
|
129 | + * - `substring(2)` on `'1101001'` yields `'101001'` |
|
130 | + * - `substring(2, 4)` on `'1101001'` yields `'1010'` |
|
131 | + * - `substring(2, 0)` on `'1101001'` yields `''` |
|
132 | + * - `substring(-2, 4)` on `'1101001'` yields `'1'` |
|
133 | + * |
|
134 | + * @param int $from position to start at; |
|
135 | + * one-based - e.g., <tt>$from = 2</tt> omits the first character; |
|
136 | + * if less than one, it yields the start of the string, but counting from the given (negative or |
|
137 | + * zero) position, effectively decreasing the <tt>$for</tt> argument, if given |
|
138 | + * @param int|null $for number of characters to take; must be non-negative; |
|
139 | + * <tt>null</tt> for the rest of the string |
|
140 | + * @return FixedBitString the substring of the bit string |
|
141 | + * @throws UndefinedOperationException when <tt>$for</tt> is negative |
|
142 | + */ |
|
143 | + public function substring($from, $for = null) |
|
144 | + { |
|
145 | + if ($for < 0) { |
|
146 | + throw new UndefinedOperationException('negative number of bits to take'); |
|
147 | + } |
|
148 | + |
|
149 | + $offset = max(0, $from - 1); |
|
150 | + if ($offset >= $this->len) { |
|
151 | + return FixedBitString::fromString(''); |
|
152 | + } |
|
153 | + |
|
154 | + if ($for === null) { |
|
155 | + $len = $this->len; |
|
156 | + } |
|
157 | + elseif ($from >= 0) { |
|
158 | + $len = $for; |
|
159 | + } |
|
160 | + else { |
|
161 | + $len = max(0, $for + $from - 1); |
|
162 | + } |
|
163 | + |
|
164 | + return FixedBitString::fromString(substr($this->bits, $offset, $len)); |
|
165 | + } |
|
166 | + |
|
167 | + /** |
|
168 | + * Concatenates this bit string with another one. |
|
169 | + * |
|
170 | + * The result is a bit string with bits of this bit string followed by the bits from the `$concatenated` bit string. |
|
171 | + * |
|
172 | + * If both the operands are fixed-length of length `A` and `B`, the result is a fixed-length bit string of length |
|
173 | + * `A+B`. Otherwise, the result is a variable-length bit string. |
|
174 | + * |
|
175 | + * @param BitString $concatenated |
|
176 | + * @return VarBitString bit string made up by concatenating the <tt>$concatenated</tt> after this bit string |
|
177 | + */ |
|
178 | + public function concat(BitString $concatenated) |
|
179 | + { |
|
180 | + return VarBitString::fromString($this->bits . $concatenated->bits); |
|
181 | + } |
|
182 | + |
|
183 | + /** |
|
184 | + * Bitwise AND operation. Only legal for operands of equal bit lengths. |
|
185 | + * |
|
186 | + * @param BitString $other the other operand |
|
187 | + * @return FixedBitString new bit string: <tt>$this & $other</tt> |
|
188 | + * @throws UndefinedOperationException if the operands are of different bit lengths |
|
189 | + */ |
|
190 | + public function bitAnd(BitString $other) |
|
191 | + { |
|
192 | + if ($this->len != $other->len) { |
|
193 | + throw new UndefinedOperationException('operands are of different bit lengths'); |
|
194 | + } |
|
195 | + |
|
196 | + $res = str_repeat('0', $this->len); |
|
197 | + for ($i = 0; $i < $this->len; $i++) { |
|
198 | + if ($this->bits[$i] == '1' && $other->bits[$i] == '1') { |
|
199 | + $res[$i] = '1'; |
|
200 | + } |
|
201 | + } |
|
202 | + |
|
203 | + return FixedBitString::fromString($res); |
|
204 | + } |
|
205 | + |
|
206 | + /** |
|
207 | + * Bitwise OR operation. Only legal for operands of equal bit lengths. |
|
208 | + * |
|
209 | + * @param BitString $other the other operand |
|
210 | + * @return FixedBitString new bit string: <tt>$this | $other</tt> |
|
211 | + * @throws UndefinedOperationException if the operands are of different bit lengths |
|
212 | + */ |
|
213 | + public function bitOr(BitString $other) |
|
214 | + { |
|
215 | + if ($this->len != $other->len) { |
|
216 | + throw new UndefinedOperationException('operands are of different bit lengths'); |
|
217 | + } |
|
218 | + |
|
219 | + $res = str_repeat('1', $this->len); |
|
220 | + for ($i = 0; $i < $this->len; $i++) { |
|
221 | + if ($this->bits[$i] == '0' && $other->bits[$i] == '0') { |
|
222 | + $res[$i] = '0'; |
|
223 | + } |
|
224 | + } |
|
225 | + |
|
226 | + return FixedBitString::fromString($res); |
|
227 | + } |
|
228 | + |
|
229 | + /** |
|
230 | + * Bitwise exclusive OR operation. Only legal for operands of equal bit lengths. |
|
231 | + * |
|
232 | + * @param BitString $other the other operand |
|
233 | + * @return FixedBitString new bit string: <tt>$this ^ $other</tt> |
|
234 | + * @throws UndefinedOperationException if the operands are of different bit lengths |
|
235 | + */ |
|
236 | + public function bitXor(BitString $other) |
|
237 | + { |
|
238 | + if ($this->len != $other->len) { |
|
239 | + throw new UndefinedOperationException('operands are of different bit lengths'); |
|
240 | + } |
|
241 | + |
|
242 | + $res = str_repeat('0', $this->len); |
|
243 | + for ($i = 0; $i < $this->len; $i++) { |
|
244 | + if ($this->bits[$i] != $other->bits[$i]) { |
|
245 | + $res[$i] = '1'; |
|
246 | + } |
|
247 | + } |
|
248 | + |
|
249 | + return FixedBitString::fromString($res); |
|
250 | + } |
|
251 | + |
|
252 | + /** |
|
253 | + * Bitwise negation, i.e., reverses all bits of the bit string. |
|
254 | + * |
|
255 | + * @return FixedBitString new bit string: <tt>~$this</tt> |
|
256 | + */ |
|
257 | + public function bitNot() |
|
258 | + { |
|
259 | + return FixedBitString::fromString(strtr($this->bits, '01', '10')); |
|
260 | + } |
|
261 | + |
|
262 | + /** |
|
263 | + * Shifts the bits to the left. |
|
264 | + * |
|
265 | + * The length of the bit string is preserved, thus, the `$shift` left trailing bits are discarded. The shifted bit |
|
266 | + * positions are filled with 0's. |
|
267 | + * |
|
268 | + * @param int $shift number of positions to shift the bits to the left; |
|
269 | + * might even be negative (results in shifting to the right by <tt>-$shift</tt>) |
|
270 | + * @return FixedBitString a bit string with bits shifted by <tt>$shift</tt> to the left |
|
271 | + */ |
|
272 | + public function bitShiftLeft($shift) |
|
273 | + { |
|
274 | + if ($shift < 0) { |
|
275 | + return $this->bitShiftRight(-$shift); |
|
276 | + } |
|
277 | + |
|
278 | + $pad = min($this->len, $shift); |
|
279 | + $prefix = ($pad == $this->len ? '' : substr($this->bits, $pad)); |
|
280 | + return FixedBitString::fromString($prefix . str_repeat('0', $pad)); |
|
281 | + } |
|
282 | + |
|
283 | + /** |
|
284 | + * Shifts the bits to the right. |
|
285 | + * |
|
286 | + * The length of the bit string is preserved, thus, the `$shift` right trailing bits are discarded. The shifted bit |
|
287 | + * positions are filled with 0's. |
|
288 | + * |
|
289 | + * @param int $shift number of positions to shift the bits to the right; |
|
290 | + * might even be negative (results in shifting to the left by <tt>-$shift</tt>) |
|
291 | + * @return FixedBitString a bit string with bits shifted by <tt>$shift</tt> to the right |
|
292 | + */ |
|
293 | + public function bitShiftRight($shift) |
|
294 | + { |
|
295 | + if ($shift < 0) { |
|
296 | + return $this->bitShiftLeft(-$shift); |
|
297 | + } |
|
298 | + |
|
299 | + $pad = min($this->len, $shift); |
|
300 | + $postfix = substr($this->bits, 0, $this->len - $pad); |
|
301 | + return FixedBitString::fromString(str_repeat('0', $pad) . $postfix); |
|
302 | + } |
|
303 | + |
|
304 | + /** |
|
305 | + * Rotates the bits to the left. |
|
306 | + * |
|
307 | + * @param int $rot the length of the rotation |
|
308 | + * @return FixedBitString a bit string with <tt>$rot</tt> (mod length) leftmost bits moved to the back of the bit |
|
309 | + * string |
|
310 | + */ |
|
311 | + public function bitRotateLeft($rot) |
|
312 | + { |
|
313 | + if ($rot < 0) { |
|
314 | + return $this->bitRotateRight(-$rot); |
|
315 | + } |
|
316 | + if ($this->len == 0) { |
|
317 | + return FixedBitString::fromString(''); |
|
318 | + } |
|
319 | + |
|
320 | + $pos = $rot % $this->len; |
|
321 | + return FixedBitString::fromString(substr($this->bits, $pos) . substr($this->bits, 0, $pos)); |
|
322 | + } |
|
323 | + |
|
324 | + /** |
|
325 | + * Rotates the bits to the right. |
|
326 | + * |
|
327 | + * @param int $rot the length of the rotation |
|
328 | + * @return FixedBitString a bit string with <tt>$rot</tt> (mod length) rightmost bits moved to the front of the bit |
|
329 | + * string |
|
330 | + */ |
|
331 | + public function bitRotateRight($rot) |
|
332 | + { |
|
333 | + if ($rot < 0) { |
|
334 | + return $this->bitRotateLeft(-$rot); |
|
335 | + } |
|
336 | + if ($this->len == 0) { |
|
337 | + return FixedBitString::fromString(''); |
|
338 | + } |
|
339 | + |
|
340 | + $pos = $this->len - ($rot % $this->len); |
|
341 | + return FixedBitString::fromString(substr($this->bits, $pos) . substr($this->bits, 0, $pos)); |
|
342 | + } |
|
343 | + |
|
344 | + |
|
345 | + public function __toString() |
|
346 | + { |
|
347 | + return $this->toString(); |
|
348 | + } |
|
349 | + |
|
350 | + |
|
351 | + /** |
|
352 | + * @param int $offset the bit to know existence of; 0 for the leftmost bit, 1 for the next bit, -1 for the rightmost |
|
353 | 353 | * bit, etc. |
354 | - * @return bool <tt>true</tt> if the bit string has <tt>$offset</tt>-th bit defined, |
|
355 | - * <tt>false</tt> if the bit string is too short (i.e., shorter than <tt>$offset+1</tt> bits) |
|
356 | - */ |
|
357 | - public function offsetExists($offset) |
|
358 | - { |
|
359 | - if ($offset < 0 && PHP_VERSION_ID < 70100) { // PHP < 7.1 did not support negative string offsets |
|
354 | + * @return bool <tt>true</tt> if the bit string has <tt>$offset</tt>-th bit defined, |
|
355 | + * <tt>false</tt> if the bit string is too short (i.e., shorter than <tt>$offset+1</tt> bits) |
|
356 | + */ |
|
357 | + public function offsetExists($offset) |
|
358 | + { |
|
359 | + if ($offset < 0 && PHP_VERSION_ID < 70100) { // PHP < 7.1 did not support negative string offsets |
|
360 | 360 | $offset = $this->len + $offset; |
361 | 361 | } |
362 | 362 | |
363 | 363 | return isset($this->bits[$offset]); |
364 | - } |
|
365 | - |
|
366 | - /** |
|
367 | - * @param int $offset the bit to get; 0 for the leftmost bit, 1 for the next bit, -1 for the rightmost bit, etc. |
|
368 | - * @return int|null 0 or 1 depending on whether the <tt>$offset</tt>-th bit is set, or |
|
369 | - * <tt>null</tt> if outside of the bit string |
|
370 | - */ |
|
371 | - public function offsetGet($offset) |
|
372 | - { |
|
364 | + } |
|
365 | + |
|
366 | + /** |
|
367 | + * @param int $offset the bit to get; 0 for the leftmost bit, 1 for the next bit, -1 for the rightmost bit, etc. |
|
368 | + * @return int|null 0 or 1 depending on whether the <tt>$offset</tt>-th bit is set, or |
|
369 | + * <tt>null</tt> if outside of the bit string |
|
370 | + */ |
|
371 | + public function offsetGet($offset) |
|
372 | + { |
|
373 | 373 | if ($offset < 0 && PHP_VERSION_ID < 70100) { // PHP < 7.1 did not support negative string offsets |
374 | 374 | $offset = $this->len + $offset; |
375 | 375 | } |
376 | 376 | |
377 | - if (isset($this->bits[$offset])) { |
|
378 | - return (int)$this->bits[$offset]; |
|
379 | - } |
|
380 | - else { |
|
381 | - return null; |
|
382 | - } |
|
383 | - } |
|
384 | - |
|
385 | - /** |
|
386 | - * Setting a bit is not implemented - the bit string is immutable. |
|
387 | - * |
|
388 | - * @throws ImmutableException |
|
389 | - */ |
|
390 | - public function offsetSet($offset, $value) |
|
391 | - { |
|
392 | - throw new ImmutableException(); |
|
393 | - } |
|
394 | - |
|
395 | - /** |
|
396 | - * Unsetting a bit is not implemented - the bit string is immutable. |
|
397 | - * |
|
398 | - * @throws ImmutableException |
|
399 | - */ |
|
400 | - public function offsetUnset($offset) |
|
401 | - { |
|
402 | - throw new ImmutableException(); |
|
403 | - } |
|
377 | + if (isset($this->bits[$offset])) { |
|
378 | + return (int)$this->bits[$offset]; |
|
379 | + } |
|
380 | + else { |
|
381 | + return null; |
|
382 | + } |
|
383 | + } |
|
384 | + |
|
385 | + /** |
|
386 | + * Setting a bit is not implemented - the bit string is immutable. |
|
387 | + * |
|
388 | + * @throws ImmutableException |
|
389 | + */ |
|
390 | + public function offsetSet($offset, $value) |
|
391 | + { |
|
392 | + throw new ImmutableException(); |
|
393 | + } |
|
394 | + |
|
395 | + /** |
|
396 | + * Unsetting a bit is not implemented - the bit string is immutable. |
|
397 | + * |
|
398 | + * @throws ImmutableException |
|
399 | + */ |
|
400 | + public function offsetUnset($offset) |
|
401 | + { |
|
402 | + throw new ImmutableException(); |
|
403 | + } |
|
404 | 404 | } |
@@ -153,11 +153,9 @@ discard block |
||
153 | 153 | |
154 | 154 | if ($for === null) { |
155 | 155 | $len = $this->len; |
156 | - } |
|
157 | - elseif ($from >= 0) { |
|
156 | + } elseif ($from >= 0) { |
|
158 | 157 | $len = $for; |
159 | - } |
|
160 | - else { |
|
158 | + } else { |
|
161 | 159 | $len = max(0, $for + $from - 1); |
162 | 160 | } |
163 | 161 | |
@@ -376,8 +374,7 @@ discard block |
||
376 | 374 | |
377 | 375 | if (isset($this->bits[$offset])) { |
378 | 376 | return (int)$this->bits[$offset]; |
379 | - } |
|
380 | - else { |
|
377 | + } else { |
|
381 | 378 | return null; |
382 | 379 | } |
383 | 380 | } |
@@ -38,7 +38,7 @@ |
||
38 | 38 | $addYears = 0; |
39 | 39 | $dateCreateInput = preg_replace_callback( |
40 | 40 | '~\d{5,}(?=(?:-\d+-\d+|\d{4})(?:\s+|T)\d)~', // supports both dash-separated date/time parts and also the form without dash separators |
41 | - function ($y) use (&$addYears) { |
|
41 | + function($y) use (&$addYears) { |
|
42 | 42 | $res = $y[0] % 10000; |
43 | 43 | $addYears = $y[0] - $res; |
44 | 44 | return $res; |
@@ -70,8 +70,7 @@ discard block |
||
70 | 70 | { |
71 | 71 | if ($float >= 10) { |
72 | 72 | return (string)$float; |
73 | - } |
|
74 | - else { |
|
73 | + } else { |
|
75 | 74 | return '0' . (float)$float; |
76 | 75 | } |
77 | 76 | } |
@@ -138,11 +137,9 @@ discard block |
||
138 | 137 | { |
139 | 138 | if ($this->inf) { |
140 | 139 | return null; |
141 | - } |
|
142 | - elseif ($this->dt->format('u')) { |
|
140 | + } elseif ($this->dt->format('u')) { |
|
143 | 141 | return (float)$this->dt->format('s.u'); |
144 | - } |
|
145 | - else { |
|
142 | + } else { |
|
146 | 143 | return (int)$this->dt->format('s'); |
147 | 144 | } |
148 | 145 | } |
@@ -33,8 +33,7 @@ |
||
33 | 33 | $scanned = sscanf($str, '%X/%X', $hi, $lo); |
34 | 34 | if ($scanned == 2) { |
35 | 35 | return new PgLogSequenceNumber($hi, $lo); |
36 | - } |
|
37 | - else { |
|
36 | + } else { |
|
38 | 37 | throw new \InvalidArgumentException('$str'); |
39 | 38 | } |
40 | 39 | } |
@@ -28,11 +28,9 @@ |
||
28 | 28 | foreach ($points as $i => $point) { |
29 | 29 | if ($point instanceof Point) { |
30 | 30 | $normalized[] = $point; |
31 | - } |
|
32 | - elseif (is_array($point)) { |
|
31 | + } elseif (is_array($point)) { |
|
33 | 32 | $normalized[] = Point::fromCoords($point[0], $point[1]); |
34 | - } |
|
35 | - else { |
|
33 | + } else { |
|
36 | 34 | throw new \InvalidArgumentException("points[$i]"); |
37 | 35 | } |
38 | 36 | } |
@@ -25,15 +25,13 @@ |
||
25 | 25 | { |
26 | 26 | if (is_array($corner)) { |
27 | 27 | $corner = Point::fromCoords($corner); |
28 | - } |
|
29 | - elseif (!$corner instanceof Point) { |
|
28 | + } elseif (!$corner instanceof Point) { |
|
30 | 29 | throw new \InvalidArgumentException('corner'); |
31 | 30 | } |
32 | 31 | |
33 | 32 | if (is_array($oppositeCorner)) { |
34 | 33 | $oppositeCorner = Point::fromCoords($oppositeCorner); |
35 | - } |
|
36 | - elseif (!$oppositeCorner instanceof Point) { |
|
34 | + } elseif (!$oppositeCorner instanceof Point) { |
|
37 | 35 | throw new \InvalidArgumentException('oppositeCorner'); |
38 | 36 | } |
39 | 37 |
@@ -66,8 +66,7 @@ discard block |
||
66 | 66 | if ($min > 0 || $sec > 0) { |
67 | 67 | throw new \OutOfRangeException('with hour 24, the minutes and seconds must be zero'); |
68 | 68 | } |
69 | - } |
|
70 | - elseif ($hour < 0 || $hour > 24) { |
|
69 | + } elseif ($hour < 0 || $hour > 24) { |
|
71 | 70 | throw new \OutOfRangeException('hours'); |
72 | 71 | } |
73 | 72 | |
@@ -81,11 +80,9 @@ discard block |
||
81 | 80 | |
82 | 81 | if (!isset($m['zone'])) { |
83 | 82 | $offset = (new \DateTime())->getOffset(); |
84 | - } |
|
85 | - elseif ($m['zone'] == 'Z') { |
|
83 | + } elseif ($m['zone'] == 'Z') { |
|
86 | 84 | $offset = 0; |
87 | - } |
|
88 | - else { |
|
85 | + } else { |
|
89 | 86 | $offset = $m['offh'] * 60 * 60; |
90 | 87 | if (isset($m['offm'])) { |
91 | 88 | $offset += ($m['offh'] >= 0 ? 1 : -1) * $m['offm'] * 60; |
@@ -34,11 +34,9 @@ discard block |
||
34 | 34 | |
35 | 35 | if ($s < 0) { |
36 | 36 | throw new \OutOfRangeException('The resulting time underruns 00:00:00'); |
37 | - } |
|
38 | - elseif ($s > 24 * 60 * 60) { |
|
37 | + } elseif ($s > 24 * 60 * 60) { |
|
39 | 38 | throw new \OutOfRangeException('The resulting time exceeds 24:00:00'); |
40 | - } |
|
41 | - else { |
|
39 | + } else { |
|
42 | 40 | return $s; |
43 | 41 | } |
44 | 42 | } |
@@ -57,8 +55,7 @@ discard block |
||
57 | 55 | if ($minute > 0 || $second > 0) { |
58 | 56 | throw new \OutOfRangeException('with hour 24, the minutes and seconds must be zero'); |
59 | 57 | } |
60 | - } |
|
61 | - elseif ($hour < 0 || $hour > 24) { |
|
58 | + } elseif ($hour < 0 || $hour > 24) { |
|
62 | 59 | throw new \OutOfRangeException('hours'); |
63 | 60 | } |
64 | 61 | |
@@ -141,16 +138,14 @@ discard block |
||
141 | 138 | { |
142 | 139 | if ($date === null) { |
143 | 140 | return $this->sec; |
144 | - } |
|
145 | - else { |
|
141 | + } else { |
|
146 | 142 | if (!$date instanceof Date) { |
147 | 143 | $date = Date::fromISOString($date); |
148 | 144 | } |
149 | 145 | $dayTs = $date->toUnixTimestamp(); |
150 | 146 | if ($dayTs !== null) { |
151 | 147 | return $dayTs + $this->sec; |
152 | - } |
|
153 | - else { |
|
148 | + } else { |
|
154 | 149 | throw new \InvalidArgumentException('infinite date'); |
155 | 150 | } |
156 | 151 | } |
@@ -23,8 +23,7 @@ |
||
23 | 23 | { |
24 | 24 | if (is_array($center)) { |
25 | 25 | $center = Point::fromCoords($center); |
26 | - } |
|
27 | - elseif (!$center instanceof Point) { |
|
26 | + } elseif (!$center instanceof Point) { |
|
28 | 27 | throw new \InvalidArgumentException('center'); |
29 | 28 | } |
30 | 29 |
@@ -73,11 +73,9 @@ discard block |
||
73 | 73 | { |
74 | 74 | if ($dateTime instanceof \DateTimeImmutable) { |
75 | 75 | return new TimestampTz(0, $dateTime); |
76 | - } |
|
77 | - elseif ($dateTime instanceof \DateTime) { |
|
76 | + } elseif ($dateTime instanceof \DateTime) { |
|
78 | 77 | return new TimestampTz(0, \DateTimeImmutable::createFromMutable($dateTime)); |
79 | - } |
|
80 | - else { |
|
78 | + } else { |
|
81 | 79 | // there should not be any other implementation of \DateTimeInterface, but PHP is not that predictable |
82 | 80 | return self::fromParts( |
83 | 81 | $dateTime->format('Y'), |
@@ -138,8 +136,7 @@ discard block |
||
138 | 136 | $tz |
139 | 137 | ); |
140 | 138 | return new TimestampTz(0, $dt); |
141 | - } |
|
142 | - else { |
|
139 | + } else { |
|
143 | 140 | $dt = self::isoStringToDateTime( |
144 | 141 | sprintf('%s%04d-01-01 00:00:00', ($z < 0 ? '-' : ''), abs($z)), |
145 | 142 | $tz |
@@ -203,21 +200,18 @@ discard block |
||
203 | 200 | |
204 | 201 | if (filter_var($timezone, FILTER_VALIDATE_INT) !== false) { |
205 | 202 | $tzSpec = ($timezone >= 0 ? '+' : '-') . gmdate('H:i', abs($timezone)); |
206 | - } |
|
207 | - elseif (preg_match('~^([^:]+:\d+):\d+$~', $timezone, $m)) { |
|
203 | + } elseif (preg_match('~^([^:]+:\d+):\d+$~', $timezone, $m)) { |
|
208 | 204 | $tzSpec = $m[1]; |
209 | 205 | $msg = "PHP's DateTimeZone is unable to represent GMT offsets with precision to seconds. " |
210 | 206 | . "Cutting '$timezone' to '$tzSpec'"; |
211 | 207 | trigger_error($msg, E_USER_WARNING); |
212 | - } |
|
213 | - else { |
|
208 | + } else { |
|
214 | 209 | $tzSpec = $timezone; |
215 | 210 | } |
216 | 211 | |
217 | 212 | try { |
218 | 213 | return new \DateTimeZone($tzSpec); |
219 | - } |
|
220 | - catch (\Exception $e) { |
|
214 | + } catch (\Exception $e) { |
|
221 | 215 | throw new \InvalidArgumentException('$timezone', 0, $e); |
222 | 216 | } |
223 | 217 | } |
@@ -247,8 +241,7 @@ discard block |
||
247 | 241 | { |
248 | 242 | if ($this->inf) { |
249 | 243 | return null; |
250 | - } |
|
251 | - else { |
|
244 | + } else { |
|
252 | 245 | $y = (int)$this->dt->format('Y'); |
253 | 246 | $u = $this->dt->format('u'); |
254 | 247 | return [ |