Skip to content

Commit 796d535

Browse files
committed
Adding fromSlice method
1 parent db64028 commit 796d535

File tree

2 files changed

+169
-1
lines changed

2 files changed

+169
-1
lines changed

src/BitArray/BitArray.php

Lines changed: 67 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -389,6 +389,73 @@ public static function fromJson($json)
389389
return self::fromTraversable(json_decode($json));
390390
}
391391

392+
/**
393+
* Create a new BitArray using a slice
394+
*
395+
* @param BitArray $bits A bitarray to get the slice
396+
* @param int $offset If offset is non-negative, the slice will start at that offset in the bits argument.
397+
* If offset is negative, the slice will start that far from the end of the bits argument.
398+
* @param mixed $size If size is given and is positive, then the slice will have up to that many elements in it.
399+
* If the bits argument is shorter than the size, then only the available elements will be present.
400+
* If size is given and is negative then the slice will stop that many elements from the end of the bits argument.
401+
* If it is omitted, then the slice will have everything from offset up until the end of the bits argument.
402+
*
403+
* @return BitArray A new BitArray
404+
*
405+
* @since 1.0.0
406+
*/
407+
public static function fromSlice(BitArray $bits, $offset = 0, $size = null)
408+
{
409+
$offset = (int) $offset;
410+
411+
if ($offset < 0)
412+
{
413+
// Start from the end
414+
$offset = $bits->size + $offset;
415+
416+
if ($offset < 0)
417+
{
418+
$offset = 0;
419+
}
420+
}
421+
elseif ($offset > $bits->size)
422+
{
423+
$offset = $bits->size;
424+
}
425+
426+
if ($size === null)
427+
{
428+
$size = $bits->size - $offset;
429+
}
430+
else
431+
{
432+
$size = (int) $size;
433+
434+
if ($size < 0)
435+
{
436+
$size = $bits->size + $size - $offset;
437+
438+
if ($size < 0)
439+
{
440+
$size = 0;
441+
}
442+
}
443+
elseif ($size > $bits->size - $offset)
444+
{
445+
$size = $bits->size - $offset;
446+
}
447+
}
448+
449+
$slice = new BitArray($size);
450+
451+
for ($i = 0; $i < $size; $i++)
452+
{
453+
$slice[$i] = $bits[$i + $offset];
454+
}
455+
456+
return $slice;
457+
}
458+
392459
/**
393460
* Complement the bit array
394461
*

tests/BitArray/BitArrayTest.php

Lines changed: 102 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -436,7 +436,7 @@ public function cases_fromString()
436436
/**
437437
* Tests BitArray::fromString
438438
*
439-
* @param array $string Initial values
439+
* @param string $string bits
440440
*
441441
* @return void
442442
*
@@ -479,6 +479,107 @@ public function test_fromJson()
479479
);
480480
}
481481

482+
/**
483+
* Data provider for test_fromSlice
484+
*
485+
* @return array
486+
*
487+
* @since 1.0.0
488+
*/
489+
public function cases_fromSlice()
490+
{
491+
return [
492+
['', 0, null, ''],
493+
['11010110', 0, null, '11010110'],
494+
['11010110', 1, null, '1010110'],
495+
['11010110', 8, null, ''],
496+
['11010110', 9, null, ''],
497+
['11010110', -7, null, '1010110'],
498+
['11010110', -8, null, '11010110'],
499+
['11010110', -9, null, '11010110'],
500+
501+
['', 0, 4, ''],
502+
['11010110', 0, 4, '1101'],
503+
['11010110', 1, 4, '1010'],
504+
['11010110', 8, 4, ''],
505+
['11010110', 9, 4, ''],
506+
['11010110', -7, 4, '1010'],
507+
['11010110', -8, 4, '1101'],
508+
['11010110', -9, 4, '1101'],
509+
510+
['', 0, 8, ''],
511+
['11010110', 0, 8, '11010110'],
512+
['11010110', 1, 8, '1010110'],
513+
['11010110', 8, 8, ''],
514+
['11010110', 9, 8, ''],
515+
['11010110', -7, 8, '1010110'],
516+
['11010110', -8, 8, '11010110'],
517+
['11010110', -9, 8, '11010110'],
518+
519+
['', 0, 9, ''],
520+
['11010110', 0, 9, '11010110'],
521+
['11010110', 1, 9, '1010110'],
522+
['11010110', 8, 9, ''],
523+
['11010110', 9, 9, ''],
524+
['11010110', -7, 9, '1010110'],
525+
['11010110', -8, 9, '11010110'],
526+
['11010110', -9, 9, '11010110'],
527+
528+
['', 0, -4, ''],
529+
['11010110', 0, -4, '1101'],
530+
['11010110', 1, -4, '101'],
531+
['11010110', 8, -4, ''],
532+
['11010110', 9, -4, ''],
533+
['11010110', -7, -4, '101'],
534+
['11010110', -8, -4, '1101'],
535+
['11010110', -9, -4, '1101'],
536+
537+
['', 0, -8, ''],
538+
['11010110', 0, -8, ''],
539+
['11010110', 1, -8, ''],
540+
['11010110', 8, -8, ''],
541+
['11010110', 9, -8, ''],
542+
['11010110', -7, -8, ''],
543+
['11010110', -8, -8, ''],
544+
['11010110', -9, -8, ''],
545+
546+
['', 0, -9, ''],
547+
['11010110', 0, -9, ''],
548+
['11010110', 1, -9, ''],
549+
['11010110', 8, -9, ''],
550+
['11010110', 9, -9, ''],
551+
['11010110', -7, -9, ''],
552+
['11010110', -8, -9, ''],
553+
['11010110', -9, -9, ''],
554+
];
555+
}
556+
557+
/**
558+
* Tests BitArray::fromSlice
559+
*
560+
* @param string $string Initial values
561+
* @param integer $offset Slice offset
562+
* @param integer $size Slice size
563+
* @param string $result Expected result
564+
*
565+
* @return void
566+
*
567+
* @covers chdemko\BitArray\BitArray::fromSlice
568+
* @covers chdemko\BitArray\BitArray::__construct
569+
* @covers chdemko\BitArray\BitArray::__toString
570+
*
571+
* @dataProvider cases_fromSlice
572+
*
573+
* @since 1.0.0
574+
*/
575+
public function test_fromSlice($string, $offset, $size, $result)
576+
{
577+
$this->assertEquals(
578+
$result,
579+
(string) BitArray::fromSlice(BitArray::fromString($string), $offset, $size)
580+
);
581+
}
582+
482583
/**
483584
* Tests BitArray::applyComplement
484585
*

0 commit comments

Comments
 (0)