| @@ 464-492 (lines=29) @@ | ||
| 461 | * |
|
| 462 | * @since 1.0.0 |
|
| 463 | */ |
|
| 464 | public static function fromTraversable($traversable) |
|
| 465 | { |
|
| 466 | $bits = new BitArray(count($traversable)); |
|
| 467 | $offset = 0; |
|
| 468 | $ord = 0; |
|
| 469 | ||
| 470 | foreach ($traversable as $value) |
|
| 471 | { |
|
| 472 | if ($value) |
|
| 473 | { |
|
| 474 | $ord |= 1 << $offset % 8; |
|
| 475 | } |
|
| 476 | ||
| 477 | if ($offset % 8 === 7) |
|
| 478 | { |
|
| 479 | $bits->data[(int) ($offset / 8)] = chr($ord); |
|
| 480 | $ord = 0; |
|
| 481 | } |
|
| 482 | ||
| 483 | $offset++; |
|
| 484 | } |
|
| 485 | ||
| 486 | if ($offset % 8 !== 0) |
|
| 487 | { |
|
| 488 | $bits->data[(int) ($offset / 8)] = chr($ord); |
|
| 489 | } |
|
| 490 | ||
| 491 | return $bits; |
|
| 492 | } |
|
| 493 | ||
| 494 | /** |
|
| 495 | * Create a new BitArray from a bit string |
|
| @@ 503-528 (lines=26) @@ | ||
| 500 | * |
|
| 501 | * @since 1.0.0 |
|
| 502 | */ |
|
| 503 | public static function fromString($string) |
|
| 504 | { |
|
| 505 | $bits = new BitArray(strlen($string)); |
|
| 506 | $ord = 0; |
|
| 507 | ||
| 508 | for ($offset = 0; $offset < $bits->size; $offset++) |
|
| 509 | { |
|
| 510 | if ($string[$offset] !== '0') |
|
| 511 | { |
|
| 512 | $ord |= 1 << $offset % 8; |
|
| 513 | } |
|
| 514 | ||
| 515 | if ($offset % 8 === 7) |
|
| 516 | { |
|
| 517 | $bits->data[(int) ($offset / 8)] = chr($ord); |
|
| 518 | $ord = 0; |
|
| 519 | } |
|
| 520 | } |
|
| 521 | ||
| 522 | if ($offset % 8 !== 0) |
|
| 523 | { |
|
| 524 | $bits->data[(int) ($offset / 8)] = chr($ord); |
|
| 525 | } |
|
| 526 | ||
| 527 | return $bits; |
|
| 528 | } |
|
| 529 | ||
| 530 | /** |
|
| 531 | * Create a new BitArray from json |
|