Skip to content

Commit 3c61127

Browse files
committed
Reintroduce getUnreadLength()
We used to have this in 0.2.x before I cleaned it up. Now with ByteBufferReader and ByteBufferWriter separated, it makes sense to restore it.
1 parent 5f28a38 commit 3c61127

File tree

5 files changed

+62
-4
lines changed

5 files changed

+62
-4
lines changed

classes/ByteBufferReader.cpp

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -136,6 +136,13 @@ READER_METHOD(setOffset) {
136136
object->offset = static_cast<size_t>(offset);
137137
}
138138

139+
READER_METHOD(getUnreadLength) {
140+
zend_parse_parameters_none_throw();
141+
142+
auto object = READER_THIS();
143+
RETURN_LONG(ZSTR_LEN(object->buffer) - object->offset);
144+
}
145+
139146
READER_METHOD(__serialize) {
140147
zend_parse_parameters_none_throw();
141148

stubs/ByteBufferReader.stub.php

Lines changed: 8 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -25,8 +25,8 @@ public function getData() : string{}
2525
/** @genstubs-expose-comment-block
2626
* Reads $length raw bytes from the buffer at the current offset.
2727
* The internal offset will be updated by this operation.
28-
*
29-
* @throws DataDecodeException if there are not enough bytes available
28+
*
29+
* @throws DataDecodeException if there are not enough bytes available
3030
*/
3131
public function readByteArray(int $length) : string{}
3232

@@ -45,6 +45,12 @@ public function getOffset() : int{}
4545
*/
4646
public function setOffset(int $offset) : void{}
4747

48+
/** @genstubs-expose-comment-block
49+
* Returns the number of bytes available to read after the
50+
* current offset.
51+
*/
52+
public function getUnreadLength() : int{}
53+
4854
public function __serialize() : array{}
4955

5056
public function __unserialize(array $data) : void{}

stubs/ByteBufferReader_arginfo.h

Lines changed: 10 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/* This is a generated file, edit the .stub.php file instead.
2-
* Stub hash: cf60f8c040fc3c12584760bd1db045b054100c6b */
2+
* Stub hash: 62940f560eaa6f7714fc03a0d332265026a502f2 */
33

44
ZEND_BEGIN_ARG_INFO_EX(arginfo_class_pmmp_encoding_ByteBufferReader___construct, 0, 0, 1)
55
ZEND_ARG_TYPE_INFO(0, data, IS_STRING, 0)
@@ -19,6 +19,8 @@ ZEND_BEGIN_ARG_WITH_RETURN_TYPE_INFO_EX(arginfo_class_pmmp_encoding_ByteBufferRe
1919
ZEND_ARG_TYPE_INFO(0, offset, IS_LONG, 0)
2020
ZEND_END_ARG_INFO()
2121

22+
#define arginfo_class_pmmp_encoding_ByteBufferReader_getUnreadLength arginfo_class_pmmp_encoding_ByteBufferReader_getOffset
23+
2224
ZEND_BEGIN_ARG_WITH_RETURN_TYPE_INFO_EX(arginfo_class_pmmp_encoding_ByteBufferReader___serialize, 0, 0, IS_ARRAY, 0)
2325
ZEND_END_ARG_INFO()
2426

@@ -33,6 +35,7 @@ ZEND_METHOD(pmmp_encoding_ByteBufferReader, getData);
3335
ZEND_METHOD(pmmp_encoding_ByteBufferReader, readByteArray);
3436
ZEND_METHOD(pmmp_encoding_ByteBufferReader, getOffset);
3537
ZEND_METHOD(pmmp_encoding_ByteBufferReader, setOffset);
38+
ZEND_METHOD(pmmp_encoding_ByteBufferReader, getUnreadLength);
3639
ZEND_METHOD(pmmp_encoding_ByteBufferReader, __serialize);
3740
ZEND_METHOD(pmmp_encoding_ByteBufferReader, __unserialize);
3841
ZEND_METHOD(pmmp_encoding_ByteBufferReader, __debugInfo);
@@ -49,7 +52,7 @@ static const zend_function_entry class_pmmp_encoding_ByteBufferReader_methods[]
4952
ZEND_RAW_FENTRY("getData", zim_pmmp_encoding_ByteBufferReader_getData, arginfo_class_pmmp_encoding_ByteBufferReader_getData, ZEND_ACC_PUBLIC)
5053
#endif
5154
#if (PHP_VERSION_ID >= 80400)
52-
ZEND_RAW_FENTRY("readByteArray", zim_pmmp_encoding_ByteBufferReader_readByteArray, arginfo_class_pmmp_encoding_ByteBufferReader_readByteArray, ZEND_ACC_PUBLIC, NULL, "/**\n * Reads $length raw bytes from the buffer at the current offset.\n * The internal offset will be updated by this operation.\n *\n * @throws DataDecodeException if there are not enough bytes available\n */")
55+
ZEND_RAW_FENTRY("readByteArray", zim_pmmp_encoding_ByteBufferReader_readByteArray, arginfo_class_pmmp_encoding_ByteBufferReader_readByteArray, ZEND_ACC_PUBLIC, NULL, "/**\n * Reads $length raw bytes from the buffer at the current offset.\n * The internal offset will be updated by this operation.\n *\n * @throws DataDecodeException if there are not enough bytes available\n */")
5356
#else
5457
ZEND_RAW_FENTRY("readByteArray", zim_pmmp_encoding_ByteBufferReader_readByteArray, arginfo_class_pmmp_encoding_ByteBufferReader_readByteArray, ZEND_ACC_PUBLIC)
5558
#endif
@@ -62,6 +65,11 @@ static const zend_function_entry class_pmmp_encoding_ByteBufferReader_methods[]
6265
ZEND_RAW_FENTRY("setOffset", zim_pmmp_encoding_ByteBufferReader_setOffset, arginfo_class_pmmp_encoding_ByteBufferReader_setOffset, ZEND_ACC_PUBLIC, NULL, "/**\n * Sets the internal read offset to the given value.\n * The offset must be within the bounds of the buffer\n * (0 <= offset <= used length).\n *\n * @throws \\ValueError if the offset is out of bounds\n */")
6366
#else
6467
ZEND_RAW_FENTRY("setOffset", zim_pmmp_encoding_ByteBufferReader_setOffset, arginfo_class_pmmp_encoding_ByteBufferReader_setOffset, ZEND_ACC_PUBLIC)
68+
#endif
69+
#if (PHP_VERSION_ID >= 80400)
70+
ZEND_RAW_FENTRY("getUnreadLength", zim_pmmp_encoding_ByteBufferReader_getUnreadLength, arginfo_class_pmmp_encoding_ByteBufferReader_getUnreadLength, ZEND_ACC_PUBLIC, NULL, "/**\n * Returns the number of bytes available to read after the\n * current offset.\n */")
71+
#else
72+
ZEND_RAW_FENTRY("getUnreadLength", zim_pmmp_encoding_ByteBufferReader_getUnreadLength, arginfo_class_pmmp_encoding_ByteBufferReader_getUnreadLength, ZEND_ACC_PUBLIC)
6573
#endif
6674
ZEND_ME(pmmp_encoding_ByteBufferReader, __serialize, arginfo_class_pmmp_encoding_ByteBufferReader___serialize, ZEND_ACC_PUBLIC)
6775
ZEND_ME(pmmp_encoding_ByteBufferReader, __unserialize, arginfo_class_pmmp_encoding_ByteBufferReader___unserialize, ZEND_ACC_PUBLIC)

tests/reader/read-byte-array.phpt

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -43,6 +43,10 @@ try{
4343
}catch(DataDecodeException $e){
4444
echo $e->getMessage() . PHP_EOL;
4545
}
46+
47+
$buffer->setOffset(1);
48+
var_dump($buffer->readByteArray($buffer->getUnreadLength()));
49+
?>
4650
--EXPECT--
4751
Need at least 1 bytes, but only have 0 bytes
4852
string(3) "abc"
@@ -52,3 +56,4 @@ Length cannot be negative
5256
string(2) "bc"
5357
int(3)
5458
Need at least 2 bytes, but only have 0 bytes
59+
string(4) "bcde"

tests/reader/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 ByteBuffer::getUnreadLength()
3+
--FILE--
4+
<?php
5+
6+
use pmmp\encoding\ByteBufferReader;
7+
8+
$buffer = new ByteBufferReader("hello world");
9+
var_dump($buffer->getUnreadLength()); //11
10+
11+
$buffer->readByteArray(1);
12+
var_dump($buffer->getUnreadLength()); //10
13+
14+
$buffer->setOffset(5);
15+
var_dump($buffer->getUnreadLength()); //6
16+
17+
$buffer->readByteArray(6);
18+
var_dump($buffer->getUnreadLength()); //0
19+
20+
$buffer->setOffset(11); //length of buffer
21+
var_dump($buffer->getUnreadLength()); //0
22+
23+
$buffer->setOffset(0);
24+
var_dump($buffer->getUnreadLength()); //11
25+
?>
26+
--EXPECT--
27+
int(11)
28+
int(10)
29+
int(6)
30+
int(0)
31+
int(0)
32+
int(11)

0 commit comments

Comments
 (0)