Skip to content

Commit 350ed03

Browse files
committed
Added ByteBuffer::getUnreadLength()
this provides the needed functionality to implement both missing APIs in #10.
1 parent e062d33 commit 350ed03

2 files changed

Lines changed: 45 additions & 0 deletions

File tree

encoding.cpp

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -565,6 +565,17 @@ static PHP_METHOD(ByteBuffer, rewind) {
565565
object->offset = 0;
566566
}
567567

568+
ZEND_BEGIN_ARG_WITH_RETURN_TYPE_INFO_EX(ByteBuffer_getUnreadLength, 0, 0, IS_LONG, 0)
569+
ZEND_END_ARG_INFO()
570+
571+
static PHP_METHOD(ByteBuffer, getUnreadLength) {
572+
zend_parse_parameters_none_throw();
573+
574+
auto object = fetch_from_zend_object<byte_buffer_zend_object>(Z_OBJ_P(ZEND_THIS));
575+
576+
RETURN_LONG(object->used - object->offset);
577+
}
578+
568579
ZEND_BEGIN_ARG_WITH_RETURN_TYPE_INFO_EX(ByteBuffer___serialize, 0, 0, IS_ARRAY, 0)
569580
ZEND_END_ARG_INFO()
570581

@@ -721,6 +732,8 @@ static zend_function_entry byte_buffer_methods[] = {
721732

722733
PHP_ME(ByteBuffer, rewind, ByteBuffer_rewind, ZEND_ACC_PUBLIC)
723734

735+
PHP_ME(ByteBuffer, getUnreadLength, ByteBuffer_getUnreadLength, ZEND_ACC_PUBLIC)
736+
724737
PHP_ME(ByteBuffer, __serialize, ByteBuffer___serialize, ZEND_ACC_PUBLIC)
725738
PHP_ME(ByteBuffer, __unserialize, ByteBuffer___unserialize, ZEND_ACC_PUBLIC)
726739
PHP_ME(ByteBuffer, __debugInfo, ByteBuffer___debugInfo, ZEND_ACC_PUBLIC)

tests/unread-length.phpt

Lines changed: 32 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,32 @@
1+
--TEST--
2+
Test that ByteBuffer::getUnreadLength() works correctly
3+
--FILE--
4+
<?php
5+
6+
use pmmp\encoding\ByteBuffer;
7+
8+
$buffer = new ByteBuffer("hello world");
9+
var_dump($buffer->getUnreadLength());
10+
11+
$buffer = new ByteBuffer();
12+
var_dump($buffer->getUnreadLength());
13+
14+
$buffer->writeByteArray("hello world");
15+
var_dump($buffer->getUnreadLength()); //zero, because read and write share the same offset (perhaps a bad idea?)
16+
17+
$buffer->rewind();
18+
var_dump($buffer->getUnreadLength());
19+
20+
$buffer->readByteArray(6);
21+
var_dump($buffer->getUnreadLength());
22+
23+
$buffer->readByteArray($buffer->getUnreadLength());
24+
var_dump($buffer->getUnreadLength());
25+
?>
26+
--EXPECT--
27+
int(11)
28+
int(0)
29+
int(0)
30+
int(11)
31+
int(5)
32+
int(0)

0 commit comments

Comments
 (0)