Skip to content
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
1 change: 1 addition & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -198,6 +198,7 @@ Method | Type | Values
`$buffer->getFloat()` | float (4 bytes) | single-precision 32-bit IEEE 754 floating point number
`$buffer->getDouble()` | double (5 bytes) | double-precision 64-bit IEEE 754 floating point number
`$buffer->getArrayBytes($length)` | byte[] | `array`
`$buffer->getArrayUnsignedBytes($length)` | unsigned byte[] | `array`
`$buffer->getString($length)` | string (length bytes) | `string`
`$buffer->getUTF()` | string | `string`
`$buffer->getUTF16($length)` | string (length * 2) | `string`
Expand Down
17 changes: 17 additions & 0 deletions src/Nelexa/Buffer/Buffer.php
Original file line number Diff line number Diff line change
Expand Up @@ -389,6 +389,23 @@ public function getArrayBytes($length)
return [];
}

/**
* Reads $length unsigned bytes from an input stream.
*
* @param $length int
* @return int[]
* @throws BufferException
*/
public function getArrayUnsignedBytes($length)
{
if ($length > 0) {
return array_values(
unpack('C*', $this->get($length))
);
}
return [];
}

/**
* Reads in a string that has been encoded using
* a modified UTF-8 format.
Expand Down
20 changes: 16 additions & 4 deletions tests/Nelexa/Buffer/BufferTestCase.php
Original file line number Diff line number Diff line change
Expand Up @@ -152,6 +152,9 @@ public function testInsertFunctional()
$arrayBytes = [0x01, 0x02, 0x03, 0x4, Cast::toByte(Cast::INTEGER_MAX_VALUE)];
$this->buffer->insertArrayBytes($arrayBytes);

$arrayUnsignedBytes = [0x01, 0x22, 0x7f, 0xee, Cast::toUnsignedByte(Cast::INTEGER_MAX_VALUE)];
$this->buffer->insertArrayBytes($arrayUnsignedBytes);

$string = 'String... Строка... 串...
😀 😬 😁 😂 😃 😄 😅 😆 😇 😉 😊 😊 🙂 🙃 ☺️ 😋 😌 😍 😘
🇦🇫 🇦🇽 🇦🇱 🇩🇿 🇦🇸 🇦🇩 🇦🇴 🇦🇮 🇦🇶 🇦🇬 🇦🇷 🇦🇲 🇦🇼 🇦🇺 🇦🇹
Expand Down Expand Up @@ -261,17 +264,20 @@ public function testInsertFunctional()
$this->assertEquals($this->buffer->getArrayBytes(5), $arrayBytes);
$this->assertEquals($this->buffer->position(), 76);

$this->assertEquals($this->buffer->getArrayUnsignedBytes(5), $arrayUnsignedBytes);
$this->assertEquals($this->buffer->position(), 81);

$this->assertEquals($this->buffer->getString($lengthString), $string);
$this->assertEquals($this->buffer->position(), 76 + $lengthString);
$this->assertEquals($this->buffer->position(), 81 + $lengthString);

$this->assertEquals($this->buffer->getUTF(), $string);
$this->assertEquals($this->buffer->position(), 78 + $lengthString * 2);
$this->assertEquals($this->buffer->position(), 83 + $lengthString * 2);

$this->assertEquals($this->buffer->getUTF16($lengthString), $string);
$this->assertEquals($this->buffer->position(), 78 + $lengthString * 4);
$this->assertEquals($this->buffer->position(), 83 + $lengthString * 4);

$this->assertEquals($this->buffer->getString($lengthString), $otherBuffer->toString());
$this->assertEquals($this->buffer->position(), 78 + $lengthString * 5);
$this->assertEquals($this->buffer->position(), 83 + $lengthString * 5);
}
}

Expand Down Expand Up @@ -554,6 +560,9 @@ public function testDouble()

$buffer->rewind();
$this->assertEquals($buffer->getArrayBytes(8), [64, 41, 85, 54, 37, 109, -74, 71]);

$buffer->rewind();
$this->assertEquals($buffer->getArrayUnsignedBytes(8), [64, 41, 85, 54, 37, 109, 182, 71]);
}

/**
Expand All @@ -579,5 +588,8 @@ public function testFloat()

$buffer->rewind();
$this->assertEquals($buffer->getArrayBytes(4), [65, 74, -87, -79]);

$buffer->rewind();
$this->assertEquals($buffer->getArrayUnsignedBytes(4), [65, 74, 169, 177]);
}
}