Skip to content

Commit

Permalink
Rename from/toBinaryString() -> from/toBytes()
Browse files Browse the repository at this point in the history
  • Loading branch information
BenMorel committed Aug 18, 2020
1 parent 4a3dcba commit 90c46f0
Show file tree
Hide file tree
Showing 2 changed files with 27 additions and 29 deletions.
24 changes: 11 additions & 13 deletions src/BigInteger.php
Original file line number Diff line number Diff line change
Expand Up @@ -169,29 +169,28 @@ public static function fromArbitraryBase(string $number, string $alphabet) : Big
}

/**
* Translates a string containing the binary representation of a BigInteger into a BigInteger.
* Translates a string of bytes containing the binary representation of a BigInteger into a BigInteger.
*
* The input string is assumed to be in big-endian byte-order: the most significant byte is in the zeroth element.
*
* If `$signed` is true, the input is assumed to be in two's-complement representation, and the leading bit is
* interpreted as a sign bit. If `$signed` is false, the input is interpreted as an unsigned number, and the
* resulting BigInteger will always be positive or zero.
*
* This method can be used to retrieve a number exported by `toBinaryString()`, as long as the `$signed` flags
* match.
* This method can be used to retrieve a number exported by `toBytes()`, as long as the `$signed` flags match.
*
* @param string $value The binary string value.
* @param string $value The byte string.
* @param bool $signed Whether to interpret as a signed number in two's-complement representation with a leading
* sign bit.
*
* @return BigInteger
*
* @throws NumberFormatException If the string is empty.
*/
public static function fromBinaryString(string $value, bool $signed = true) : BigInteger
public static function fromBytes(string $value, bool $signed = true) : BigInteger
{
if ($value === '') {
throw new NumberFormatException('The binary string must not be empty.');
throw new NumberFormatException('The byte string must not be empty.');
}

$twosComplement = false;
Expand Down Expand Up @@ -249,7 +248,7 @@ public static function randomBits(int $numBits, ?callable $randomBytesGenerator
$randomBytes = $randomBytesGenerator($byteLength);
$randomBytes[0] = $randomBytes[0] & $bitmask;

return self::fromBinaryString($randomBytes, false);
return self::fromBytes($randomBytes, false);
}

/**
Expand Down Expand Up @@ -1044,9 +1043,9 @@ public function toArbitraryBase(string $alphabet) : string
}

/**
* Returns a string containing the binary representation of this BigInteger.
* Returns a string of bytes containing the binary representation of this BigInteger.
*
* The binary string is in big-endian byte-order: the most significant byte is in the zeroth element.
* The string is in big-endian byte-order: the most significant byte is in the zeroth element.
*
* If `$signed` is true, the output will be in two's-complement representation, and a sign bit will be prepended to
* the output. If `$signed` is false, no sign bit will be prepended, and this method will throw an exception if the
Expand All @@ -1055,19 +1054,18 @@ public function toArbitraryBase(string $alphabet) : string
* The string will contain the minimum number of bytes required to represent this BigInteger, including a sign bit
* if `$signed` is true.
*
* This representation is compatible with the `fromBinaryString()` factory method, as long as the `$signed` flags
* match.
* This representation is compatible with the `fromBytes()` factory method, as long as the `$signed` flags match.
*
* @param bool $signed Whether to output a signed number in two's-complement representation with a leading sign bit.
*
* @return string
*
* @throws NegativeNumberException If $signed is false, and the number is negative.
*/
public function toBinaryString(bool $signed = true) : string
public function toBytes(bool $signed = true) : string
{
if (! $signed && $this->isNegative()) {
throw new NegativeNumberException('Cannot convert a negative number to a binary string when $signed is false.');
throw new NegativeNumberException('Cannot convert a negative number to a byte string when $signed is false.');
}

$pad = function(string $hex) : string {
Expand Down
32 changes: 16 additions & 16 deletions tests/BigIntegerTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -3275,41 +3275,41 @@ public function testToArbitraryBaseOnNegativeNumber() : void
}

/**
* @dataProvider providerFromBinaryString
* @dataProvider providerFromBytes
*/
public function testFromBinaryString(string $binaryStringHex, bool $signed, string $expectedNumber) : void
public function testFromBytes(string $byteStringHex, bool $signed, string $expectedNumber) : void
{
$number = BigInteger::fromBinaryString(hex2bin($binaryStringHex), $signed);
$number = BigInteger::fromBytes(hex2bin($byteStringHex), $signed);
self::assertSame($expectedNumber, (string) $number);
}

public function providerFromBinaryString() : Generator
public function providerFromBytes() : Generator
{
foreach ($this->providerToBinaryString() as [$expectedNumber, $signed, $binaryStringHex]) {
yield [$binaryStringHex, $signed, $expectedNumber];
foreach ($this->providerToBytes() as [$expectedNumber, $signed, $byteStringHex]) {
yield [$byteStringHex, $signed, $expectedNumber];

// test with extra leading bits: these should return the same number
$prefix = ($expectedNumber[0] === '-') ? 'FF' : '00';
yield [$prefix . $binaryStringHex, $signed, $expectedNumber];
yield [$prefix . $byteStringHex, $signed, $expectedNumber];
}
}

public function testFromBinaryStringWithEmptyString() : void
public function testFromBytesWithEmptyString() : void
{
$this->expectException(NumberFormatException::class);
BigInteger::fromBinaryString('');
BigInteger::fromBytes('');
}

/**
* @dataProvider providerToBinaryString
* @dataProvider providerToBytes
*/
public function testToBinaryString(string $number, bool $signed, string $expectedBinaryStringHex) : void
public function testToBytes(string $number, bool $signed, string $expectedByteStringHex) : void
{
$binaryString = BigInteger::of($number)->toBinaryString($signed);
self::assertSame($expectedBinaryStringHex, strtoupper(bin2hex($binaryString)));
$byteString = BigInteger::of($number)->toBytes($signed);
self::assertSame($expectedByteStringHex, strtoupper(bin2hex($byteString)));
}

public function providerToBinaryString() : array
public function providerToBytes() : array
{
return [
['-32769', true, 'FF7FFF'],
Expand Down Expand Up @@ -3461,11 +3461,11 @@ public function providerToBinaryString() : array
];
}

public function testToBinaryStringNotSignedWithNegativeNumber() : void
public function testToBytesNotSignedWithNegativeNumber() : void
{
$number = BigInteger::of(-1);
$this->expectException(NegativeNumberException::class);
$number->toBinaryString(false);
$number->toBytes(false);
}

/**
Expand Down

0 comments on commit 90c46f0

Please sign in to comment.