Skip to content

Commit

Permalink
Code optimizations
Browse files Browse the repository at this point in the history
  • Loading branch information
Spomky committed Sep 17, 2021
1 parent c1eb55a commit 95eb39c
Show file tree
Hide file tree
Showing 6 changed files with 8 additions and 39 deletions.
8 changes: 1 addition & 7 deletions src/IndefiniteLengthByteStringObject.php
Original file line number Diff line number Diff line change
Expand Up @@ -13,8 +13,6 @@

namespace CBOR;

use InvalidArgumentException;

/**
* @final
*/
Expand Down Expand Up @@ -44,11 +42,7 @@ public function __toString(): string
foreach ($this->chunks as $chunk) {
$result .= $chunk->__toString();
}
$bin = hex2bin('FF');
if (false === $bin) {
throw new InvalidArgumentException('Unable to convert the data');
}
$result .= $bin;
$result .= "\xFF";

return $result;
}
Expand Down
7 changes: 1 addition & 6 deletions src/IndefiniteLengthListObject.php
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,6 @@
use ArrayIterator;
use function count;
use Countable;
use InvalidArgumentException;
use Iterator;
use IteratorAggregate;

Expand Down Expand Up @@ -50,11 +49,7 @@ public function __toString(): string
foreach ($this->data as $object) {
$result .= (string) $object;
}
$bin = hex2bin('FF');
if (false === $bin) {
throw new InvalidArgumentException('Unable to convert the data');
}
$result .= $bin;
$result .= "\xFF";

return $result;
}
Expand Down
7 changes: 1 addition & 6 deletions src/IndefiniteLengthMapObject.php
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,6 @@
use ArrayIterator;
use function count;
use Countable;
use InvalidArgumentException;
use Iterator;
use IteratorAggregate;

Expand Down Expand Up @@ -51,11 +50,7 @@ public function __toString(): string
$result .= (string) $object->getKey();
$result .= (string) $object->getValue();
}
$bin = hex2bin('FF');
if (false === $bin) {
throw new InvalidArgumentException('Unable to convert the data');
}
$result .= $bin;
$result .= "\xFF";

return $result;
}
Expand Down
8 changes: 1 addition & 7 deletions src/IndefiniteLengthTextStringObject.php
Original file line number Diff line number Diff line change
Expand Up @@ -13,8 +13,6 @@

namespace CBOR;

use InvalidArgumentException;

/**
* @final
*/
Expand Down Expand Up @@ -44,11 +42,7 @@ public function __toString(): string
foreach ($this->data as $object) {
$result .= (string) $object;
}
$bin = hex2bin('FF');
if (false === $bin) {
throw new InvalidArgumentException('Unable to convert the data');
}
$result .= $bin;
$result .= "\xFF";

return $result;
}
Expand Down
12 changes: 4 additions & 8 deletions src/LengthCalculator.php
Original file line number Diff line number Diff line change
Expand Up @@ -53,28 +53,24 @@ private static function computeLength(int $length): array
case $length < 0xFF:
return [24, chr($length)];
case $length < 0xFFFF:
return [25, self::hex2bin(static::fixHexLength(Utils::intToHex($length)))];
return [25, self::hex2bin(dechex($length))];
case $length < 0xFFFFFFFF:
return [26, self::hex2bin(static::fixHexLength(Utils::intToHex($length)))];
return [26, self::hex2bin(dechex($length))];
case BigInteger::of($length)->isLessThan(BigInteger::fromBase('FFFFFFFFFFFFFFFF', 16)):
return [27, self::hex2bin(static::fixHexLength(Utils::intToHex($length)))];
return [27, self::hex2bin(dechex($length))];
default:
return [31, null];
}
}

private static function hex2bin(string $data): string
{
$data = str_pad($data, (int) (2 ** ceil(log(mb_strlen($data, '8bit'), 2))), '0', STR_PAD_LEFT);
$result = hex2bin($data);
if (false === $result) {
throw new InvalidArgumentException('Unable to convert the data');
}

return $result;
}

private static function fixHexLength(string $data): string
{
return str_pad($data, (int) (2 ** ceil(log(mb_strlen($data, '8bit'), 2))), '0', STR_PAD_LEFT);
}
}
5 changes: 0 additions & 5 deletions src/Utils.php
Original file line number Diff line number Diff line change
Expand Up @@ -47,11 +47,6 @@ public static function hexToString(string $value): string
return BigInteger::fromBase(bin2hex($value), 16)->toBase(10);
}

public static function intToHex(int $value): string
{
return BigInteger::of($value)->toBase(16);
}

public static function decode(string $data): string
{
$decoded = base64_decode(strtr($data, '-_', '+/'), true);
Expand Down

0 comments on commit 95eb39c

Please sign in to comment.