Skip to content

Commit

Permalink
Simplify the decoding int 32 (0xd2)
Browse files Browse the repository at this point in the history
Add more test cases.
  • Loading branch information
AlexMasterov committed Mar 14, 2018
1 parent d256133 commit 9658a74
Show file tree
Hide file tree
Showing 2 changed files with 8 additions and 3 deletions.
4 changes: 1 addition & 3 deletions src/Decoder.php
Expand Up @@ -224,12 +224,10 @@ private function decodeInt32(): int
throw InsufficientData::fromOffset($this->data, $this->offset, 4);
}

$num = ORD[$this->data[$this->offset++]] * 0x1000000
return ORD[$this->data[$this->offset++]] << 24
| ORD[$this->data[$this->offset++]] << 16
| ORD[$this->data[$this->offset++]] << 8
| ORD[$this->data[$this->offset++]];

return $num & 0x80000000 ? $num - 0x100000000 : $num;
}

private function decodeInt64(): int
Expand Down
7 changes: 7 additions & 0 deletions tests/DecoderTest.php
Expand Up @@ -61,6 +61,13 @@ public function supportedNumberTypes(): Iterable
yield from Type::uint();
yield from Type::uint64();
yield from Type::int();
// int 32
yield ['d200000000', 0];
yield ['d200000001', 1];
yield ['d2ffffffff', -1];
yield ['d200008000', 32768];
yield ['d27fffffff', 2147483647];
// int 64
yield ['d37fffffffffffffff', PHP_INT_MAX, /* 9223372036854775807 */];
}

Expand Down

0 comments on commit 9658a74

Please sign in to comment.