| @@ 324-352 (lines=29) @@ | ||
| 321 | * |
|
| 322 | * @since 1.0.0 |
|
| 323 | */ |
|
| 324 | public static function fromTraversable($traversable) |
|
| 325 | { |
|
| 326 | $bits = new BitArray(count($traversable)); |
|
| 327 | $offset = 0; |
|
| 328 | $ord = 0; |
|
| 329 | ||
| 330 | foreach ($traversable as $value) |
|
| 331 | { |
|
| 332 | if ($value) |
|
| 333 | { |
|
| 334 | $ord |= 1 << $offset % 8; |
|
| 335 | } |
|
| 336 | ||
| 337 | if ($offset % 8 === 7) |
|
| 338 | { |
|
| 339 | $bits->data[(int) ($offset / 8)] = chr($ord); |
|
| 340 | $ord = 0; |
|
| 341 | } |
|
| 342 | ||
| 343 | $offset++; |
|
| 344 | } |
|
| 345 | ||
| 346 | if ($offset % 8 !== 0) |
|
| 347 | { |
|
| 348 | $bits->data[(int) ($offset / 8)] = chr($ord); |
|
| 349 | } |
|
| 350 | ||
| 351 | return $bits; |
|
| 352 | } |
|
| 353 | ||
| 354 | /** |
|
| 355 | * Create a new BitArray from a bit string |
|
| @@ 363-388 (lines=26) @@ | ||
| 360 | * |
|
| 361 | * @since 1.0.0 |
|
| 362 | */ |
|
| 363 | public static function fromString($string) |
|
| 364 | { |
|
| 365 | $bits = new BitArray(strlen($string)); |
|
| 366 | $ord = 0; |
|
| 367 | ||
| 368 | for ($offset = 0; $offset < $bits->size; $offset++) |
|
| 369 | { |
|
| 370 | if ($string[$offset] !== '0') |
|
| 371 | { |
|
| 372 | $ord |= 1 << $offset % 8; |
|
| 373 | } |
|
| 374 | ||
| 375 | if ($offset % 8 === 7) |
|
| 376 | { |
|
| 377 | $bits->data[(int) ($offset / 8)] = chr($ord); |
|
| 378 | $ord = 0; |
|
| 379 | } |
|
| 380 | } |
|
| 381 | ||
| 382 | if ($offset % 8 !== 0) |
|
| 383 | { |
|
| 384 | $bits->data[(int) ($offset / 8)] = chr($ord); |
|
| 385 | } |
|
| 386 | ||
| 387 | return $bits; |
|
| 388 | } |
|
| 389 | ||
| 390 | /** |
|
| 391 | * Create a new BitArray from json |
|