| @@ 524-552 (lines=29) @@ | ||
| 521 | * |
|
| 522 | * @since 1.0.0 |
|
| 523 | */ |
|
| 524 | public static function fromTraversable($traversable) |
|
| 525 | { |
|
| 526 | $bits = new BitArray(count($traversable)); |
|
| 527 | $offset = 0; |
|
| 528 | $ord = 0; |
|
| 529 | ||
| 530 | foreach ($traversable as $value) |
|
| 531 | { |
|
| 532 | if ($value) |
|
| 533 | { |
|
| 534 | $ord |= 1 << $offset % 8; |
|
| 535 | } |
|
| 536 | ||
| 537 | if ($offset % 8 === 7) |
|
| 538 | { |
|
| 539 | $bits->data[(int) ($offset / 8)] = chr($ord); |
|
| 540 | $ord = 0; |
|
| 541 | } |
|
| 542 | ||
| 543 | $offset++; |
|
| 544 | } |
|
| 545 | ||
| 546 | if ($offset % 8 !== 0) |
|
| 547 | { |
|
| 548 | $bits->data[(int) ($offset / 8)] = chr($ord); |
|
| 549 | } |
|
| 550 | ||
| 551 | return $bits; |
|
| 552 | } |
|
| 553 | ||
| 554 | /** |
|
| 555 | * Create a new BitArray from a bit string |
|
| @@ 563-588 (lines=26) @@ | ||
| 560 | * |
|
| 561 | * @since 1.0.0 |
|
| 562 | */ |
|
| 563 | public static function fromString($string) |
|
| 564 | { |
|
| 565 | $bits = new BitArray(strlen($string)); |
|
| 566 | $ord = 0; |
|
| 567 | ||
| 568 | for ($offset = 0; $offset < $bits->size; $offset++) |
|
| 569 | { |
|
| 570 | if ($string[$offset] !== '0') |
|
| 571 | { |
|
| 572 | $ord |= 1 << $offset % 8; |
|
| 573 | } |
|
| 574 | ||
| 575 | if ($offset % 8 === 7) |
|
| 576 | { |
|
| 577 | $bits->data[(int) ($offset / 8)] = chr($ord); |
|
| 578 | $ord = 0; |
|
| 579 | } |
|
| 580 | } |
|
| 581 | ||
| 582 | if ($offset % 8 !== 0) |
|
| 583 | { |
|
| 584 | $bits->data[(int) ($offset / 8)] = chr($ord); |
|
| 585 | } |
|
| 586 | ||
| 587 | return $bits; |
|
| 588 | } |
|
| 589 | ||
| 590 | /** |
|
| 591 | * Create a new BitArray from json |
|