Skip to content

Add methods for UInt32LE #6

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
34 changes: 34 additions & 0 deletions src/ByteBuffer.php
Original file line number Diff line number Diff line change
Expand Up @@ -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
*
Expand Down
15 changes: 15 additions & 0 deletions tests/ByteBufferTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -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());
Expand All @@ -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());
Expand All @@ -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));
Expand Down