diff --git a/src/BigInteger.php b/src/BigInteger.php index ec6989f..d341da4 100644 --- a/src/BigInteger.php +++ b/src/BigInteger.php @@ -169,7 +169,7 @@ 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. * @@ -177,10 +177,9 @@ public static function fromArbitraryBase(string $number, string $alphabet) : Big * 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. * @@ -188,10 +187,10 @@ public static function fromArbitraryBase(string $number, string $alphabet) : Big * * @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; @@ -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); } /** @@ -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 @@ -1055,8 +1054,7 @@ 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. * @@ -1064,10 +1062,10 @@ public function toArbitraryBase(string $alphabet) : 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 { diff --git a/tests/BigIntegerTest.php b/tests/BigIntegerTest.php index 8686cb4..139b12c 100644 --- a/tests/BigIntegerTest.php +++ b/tests/BigIntegerTest.php @@ -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'], @@ -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); } /**