From fbba757d05a67261802ad94d7ff292aa3d1bcd7f Mon Sep 17 00:00:00 2001 From: Konstantin Grachev Date: Mon, 1 Feb 2021 12:58:33 +0300 Subject: [PATCH] Add methods for UInt32LE --- src/ByteBuffer.php | 34 ++++++++++++++++++++++++++++++++++ tests/ByteBufferTest.php | 15 +++++++++++++++ 2 files changed, 49 insertions(+) diff --git a/src/ByteBuffer.php b/src/ByteBuffer.php index 16924f6..8e693f3 100644 --- a/src/ByteBuffer.php +++ b/src/ByteBuffer.php @@ -438,6 +438,40 @@ public function consumeUint32(): int return $r; } + /** + * @param int $value + * + * @return static + */ + public function appendUint32LE(int $value): self + { + return $this->append(\pack("V", $value)); + } + + /** + * @param int $offset + * + * @return int + * @throws BufferOverflow + */ + public function readUint32LE(int $offset = 0): int + { + return \unpack("V", $this->read(4, $offset))[1]; + } + + /** + * @return int + * @throws BufferOverflow + */ + public function consumeUint32LE(): int + { + $r = \unpack("V", $this->data)[1]; + + $this->discard(4); + + return $r; + } + /** * @param int $value * diff --git a/tests/ByteBufferTest.php b/tests/ByteBufferTest.php index 0c852ed..8061dd8 100644 --- a/tests/ByteBufferTest.php +++ b/tests/ByteBufferTest.php @@ -321,6 +321,11 @@ public function testReadUint32() self::assertEquals(0xA9782361, (new Buffer("\xA9\x78\x23\x61"))->readUint32()); } + public function testReadUint32LE() + { + self::assertEquals(0xA9782361, (new Buffer("\x61\x23\x78\xA9"))->readUint32LE()); + } + public function testReadInt32() { self::assertEquals(0xA9782361 - 0x100000000, (new Buffer("\xA9\x78\x23\x61"))->readInt32()); @@ -331,6 +336,11 @@ public function testConsumeUint32() self::assertEquals(0xA9782361, (new Buffer("\xA9\x78\x23\x61"))->consumeUint32()); } + public function testConsumeUint32LE() + { + self::assertEquals(0xA9782361, (new Buffer("\x61\x23\x78\xA9"))->consumeUint32LE()); + } + public function testConsumeInt32() { self::assertEquals(0xA9782361 - 0x100000000, (new Buffer("\xA9\x78\x23\x61"))->consumeInt32()); @@ -341,6 +351,11 @@ public function testAppendUint32() self::assertEquals("\xA9\x78\x23\x61", (new Buffer)->appendUint32(0xA9782361)->read(4)); } + public function testAppendUint32LE() + { + self::assertEquals("\x61\x23\x78\xA9", (new Buffer)->appendUint32LE(0xA9782361)->read(4)); + } + public function testAppendInt32() { self::assertEquals("\xA9\x78\x23\x61", (new Buffer)->appendInt32(0xA9782361)->read(4));