Skip to content

Commit

Permalink
Flip default signedness to match common standard
Browse files Browse the repository at this point in the history
  • Loading branch information
olafkryus committed Apr 3, 2020
1 parent 35fc499 commit 46b20eb
Show file tree
Hide file tree
Showing 5 changed files with 22 additions and 22 deletions.
24 changes: 12 additions & 12 deletions src/ByteStream.php
Original file line number Diff line number Diff line change
Expand Up @@ -6,9 +6,9 @@
use Kryus\Binary\DataType\BinaryValue;
use Kryus\Binary\DataType\Byte;
use Kryus\Binary\DataType\Dword;
use Kryus\Binary\DataType\SignedByte;
use Kryus\Binary\DataType\SignedDword;
use Kryus\Binary\DataType\SignedWord;
use Kryus\Binary\DataType\UnsignedByte;
use Kryus\Binary\DataType\UnsignedDword;
use Kryus\Binary\DataType\UnsignedWord;
use Kryus\Binary\DataType\Word;

class ByteStream
Expand Down Expand Up @@ -143,18 +143,18 @@ public function readByte(?int $endianness = null): Byte

/**
* @param int|null $endianness
* @return SignedByte
* @return UnsignedByte
* @throws \Exception
*/
public function readSignedByte(?int $endianness = null): SignedByte
public function readUnsignedByte(?int $endianness = null): UnsignedByte
{
if (!in_array($endianness, [null, BinaryValue::ENDIANNESS_LITTLE_ENDIAN, BinaryValue::ENDIANNESS_BIG_ENDIAN], true)) {
throw new \Exception('Invalid endianness type.');
}

$binaryValue = $this->readBytes(1);

return new SignedByte($binaryValue, $endianness ?? $this->endianness);
return new UnsignedByte($binaryValue, $endianness ?? $this->endianness);
}

/**
Expand All @@ -175,18 +175,18 @@ public function readWord(?int $endianness = null): Word

/**
* @param int|null $endianness
* @return SignedWord
* @return UnsignedWord
* @throws \Exception
*/
public function readSignedWord(?int $endianness = null): SignedWord
public function readUnsignedWord(?int $endianness = null): UnsignedWord
{
if (!in_array($endianness, [null, BinaryValue::ENDIANNESS_LITTLE_ENDIAN, BinaryValue::ENDIANNESS_BIG_ENDIAN], true)) {
throw new \Exception('Invalid endianness type.');
}

$binaryValue = $this->readBytes(2);

return new SignedWord($binaryValue, $endianness ?? $this->endianness);
return new UnsignedWord($binaryValue, $endianness ?? $this->endianness);
}

/**
Expand All @@ -207,17 +207,17 @@ public function readDword(?int $endianness = null): Dword

/**
* @param int|null $endianness
* @return SignedDword
* @return UnsignedDword
* @throws \Exception
*/
public function readSignedDword(?int $endianness = null): SignedDword
public function readUnsignedDword(?int $endianness = null): UnsignedDword
{
if (!in_array($endianness, [null, BinaryValue::ENDIANNESS_LITTLE_ENDIAN, BinaryValue::ENDIANNESS_BIG_ENDIAN], true)) {
throw new \Exception('Invalid endianness type.');
}

$binaryValue = $this->readBytes(4);

return new SignedDword($binaryValue, $endianness ?? $this->endianness);
return new UnsignedDword($binaryValue, $endianness ?? $this->endianness);
}
}
2 changes: 1 addition & 1 deletion src/DataType/BinaryValue.php
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@ class BinaryValue
* @param bool $signed
* @throws \Exception
*/
public function __construct(string $value, int $endianness = self::ENDIANNESS_LITTLE_ENDIAN, bool $signed = false)
public function __construct(string $value, int $endianness = self::ENDIANNESS_LITTLE_ENDIAN, bool $signed = true)
{
if (!in_array($endianness, [self::ENDIANNESS_LITTLE_ENDIAN, self::ENDIANNESS_BIG_ENDIAN], true)) {
throw new \Exception('Invalid endianness type.');
Expand Down
6 changes: 3 additions & 3 deletions src/DataType/SignedByte.php → src/DataType/UnsignedByte.php
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@

namespace Kryus\Binary\DataType;

class SignedByte extends BinaryValue
class UnsignedByte extends BinaryValue
{
/**
* @param string $value
Expand All @@ -14,9 +14,9 @@ public function __construct(string $value, int $endianness = BinaryValue::ENDIAN
{
$byteCount = strlen($value);
if ($byteCount !== 1) {
throw new \Exception("Invalid byte count of {$byteCount} for value of type Signed Byte.");
throw new \Exception("Invalid byte count of {$byteCount} for value of type Unsigned Byte.");
}

parent::__construct($value, $endianness, true);
parent::__construct($value, $endianness, false);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@

namespace Kryus\Binary\DataType;

class SignedDword extends BinaryValue
class UnsignedDword extends BinaryValue
{
/**
* @param string $value
Expand All @@ -14,9 +14,9 @@ public function __construct(string $value, int $endianness = BinaryValue::ENDIAN
{
$byteCount = strlen($value);
if ($byteCount !== 4) {
throw new \Exception("Invalid byte count of {$byteCount} for value of type Signed Dword.");
throw new \Exception("Invalid byte count of {$byteCount} for value of type Unsigned Dword.");
}

parent::__construct($value, $endianness, true);
parent::__construct($value, $endianness, false);
}
}
6 changes: 3 additions & 3 deletions src/DataType/SignedWord.php → src/DataType/UnsignedWord.php
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@

namespace Kryus\Binary\DataType;

class SignedWord extends BinaryValue
class UnsignedWord extends BinaryValue
{
/**
* @param string $value
Expand All @@ -14,9 +14,9 @@ public function __construct(string $value, int $endianness = BinaryValue::ENDIAN
{
$byteCount = strlen($value);
if ($byteCount !== 2) {
throw new \Exception("Invalid byte count of {$byteCount} for value of type Signed Word.");
throw new \Exception("Invalid byte count of {$byteCount} for value of type Unsigned Word.");
}

parent::__construct($value, $endianness, true);
parent::__construct($value, $endianness, false);
}
}

0 comments on commit 46b20eb

Please sign in to comment.