Skip to content

Commit

Permalink
API changes
Browse files Browse the repository at this point in the history
  • Loading branch information
dktapps committed Jan 30, 2024
1 parent a0239e3 commit 0ef9289
Show file tree
Hide file tree
Showing 5 changed files with 37 additions and 61 deletions.
17 changes: 8 additions & 9 deletions encoding.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -493,7 +493,14 @@ BYTE_BUFFER_METHOD(setOffset) {
object->offset = static_cast<size_t>(offset);
}

BYTE_BUFFER_METHOD(getReserved) {
BYTE_BUFFER_METHOD(getUsedLength) {
zend_parse_parameters_none_throw();

auto object = BYTE_BUFFER_THIS();
RETURN_LONG(object->used);
}

BYTE_BUFFER_METHOD(getReservedLength) {
zend_parse_parameters_none_throw();

auto object = BYTE_BUFFER_THIS();
Expand Down Expand Up @@ -531,14 +538,6 @@ BYTE_BUFFER_METHOD(rewind) {
object->offset = 0;
}

BYTE_BUFFER_METHOD(getUnreadLength) {
zend_parse_parameters_none_throw();

auto object = BYTE_BUFFER_THIS();

RETURN_LONG(object->used - object->offset);
}

BYTE_BUFFER_METHOD(__serialize) {
zend_parse_parameters_none_throw();

Expand Down
25 changes: 17 additions & 8 deletions stubs/BaseByteBuffer.stub.php
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,12 @@
* @strict-properties
*/
abstract class BaseByteBuffer{
/**
* Constructs a new ByteBuffer.
* The offset will be initialized to 0.
* If you want to use the buffer for writing, you'll want to do
* setOffset(strlen($buffer)) before writing anything.
*/
public function __construct(string $buffer = ""){}

/**
Expand Down Expand Up @@ -41,16 +47,25 @@ public function getOffset() : int{}

/**
* Sets the internal offset to the given value.
* The offset must be within the intialized part of the buffer.
* The offset must be within the bounds of the buffer
* (0 <= offset <= used length).
*
* @throws \ValueError if the offset is out of bounds
*/
public function setOffset(int $offset) : void{}

/**
* Returns the total number of bytes written or available to read.
* This will always be less than or equal to the reserved length.
*/
public function getUsedLength() : int{}

/**
* Returns the number of bytes reserved by the ByteBuffer.
* This value may be larger than the number of readable bytes, as
* some memory may be preallocated to avoid reallocations.
*/
public function getReserved() : int{}
public function getReservedLength() : int{}

/**
* Increases buffer capacity to the given value, if the capacity
Expand All @@ -71,12 +86,6 @@ public function trim() : void{}
*/
public function rewind() : void{}

/**
* Returns the number of bytes available to read after the
* current offset.
*/
public function getUnreadLength() : int{}

public function __serialize() : array{}

public function __unserialize(array $data) : void{}
Expand Down
16 changes: 8 additions & 8 deletions stubs/BaseByteBuffer_arginfo.h
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
/* This is a generated file, edit the .stub.php file instead.
* Stub hash: 3bf9e52957f8e887b5addfdd7cc753b098a141f1 */
* Stub hash: 1a672ab65209adbee01706b4a9c5b14535e57298 */

ZEND_BEGIN_ARG_INFO_EX(arginfo_class_pmmp_encoding_BaseByteBuffer___construct, 0, 0, 0)
ZEND_ARG_TYPE_INFO_WITH_DEFAULT_VALUE(0, buffer, IS_STRING, 0, "\"\"")
Expand All @@ -23,7 +23,9 @@ ZEND_BEGIN_ARG_WITH_RETURN_TYPE_INFO_EX(arginfo_class_pmmp_encoding_BaseByteBuff
ZEND_ARG_TYPE_INFO(0, offset, IS_LONG, 0)
ZEND_END_ARG_INFO()

#define arginfo_class_pmmp_encoding_BaseByteBuffer_getReserved arginfo_class_pmmp_encoding_BaseByteBuffer_getOffset
#define arginfo_class_pmmp_encoding_BaseByteBuffer_getUsedLength arginfo_class_pmmp_encoding_BaseByteBuffer_getOffset

#define arginfo_class_pmmp_encoding_BaseByteBuffer_getReservedLength arginfo_class_pmmp_encoding_BaseByteBuffer_getOffset

ZEND_BEGIN_ARG_WITH_RETURN_TYPE_INFO_EX(arginfo_class_pmmp_encoding_BaseByteBuffer_reserve, 0, 1, IS_VOID, 0)
ZEND_ARG_TYPE_INFO(0, length, IS_LONG, 0)
Expand All @@ -34,8 +36,6 @@ ZEND_END_ARG_INFO()

#define arginfo_class_pmmp_encoding_BaseByteBuffer_rewind arginfo_class_pmmp_encoding_BaseByteBuffer_trim

#define arginfo_class_pmmp_encoding_BaseByteBuffer_getUnreadLength arginfo_class_pmmp_encoding_BaseByteBuffer_getOffset

ZEND_BEGIN_ARG_WITH_RETURN_TYPE_INFO_EX(arginfo_class_pmmp_encoding_BaseByteBuffer___serialize, 0, 0, IS_ARRAY, 0)
ZEND_END_ARG_INFO()

Expand All @@ -52,11 +52,11 @@ ZEND_METHOD(pmmp_encoding_BaseByteBuffer, readByteArray);
ZEND_METHOD(pmmp_encoding_BaseByteBuffer, writeByteArray);
ZEND_METHOD(pmmp_encoding_BaseByteBuffer, getOffset);
ZEND_METHOD(pmmp_encoding_BaseByteBuffer, setOffset);
ZEND_METHOD(pmmp_encoding_BaseByteBuffer, getReserved);
ZEND_METHOD(pmmp_encoding_BaseByteBuffer, getUsedLength);
ZEND_METHOD(pmmp_encoding_BaseByteBuffer, getReservedLength);
ZEND_METHOD(pmmp_encoding_BaseByteBuffer, reserve);
ZEND_METHOD(pmmp_encoding_BaseByteBuffer, trim);
ZEND_METHOD(pmmp_encoding_BaseByteBuffer, rewind);
ZEND_METHOD(pmmp_encoding_BaseByteBuffer, getUnreadLength);
ZEND_METHOD(pmmp_encoding_BaseByteBuffer, __serialize);
ZEND_METHOD(pmmp_encoding_BaseByteBuffer, __unserialize);
ZEND_METHOD(pmmp_encoding_BaseByteBuffer, __debugInfo);
Expand All @@ -69,11 +69,11 @@ static const zend_function_entry class_pmmp_encoding_BaseByteBuffer_methods[] =
ZEND_ME(pmmp_encoding_BaseByteBuffer, writeByteArray, arginfo_class_pmmp_encoding_BaseByteBuffer_writeByteArray, ZEND_ACC_PUBLIC)
ZEND_ME(pmmp_encoding_BaseByteBuffer, getOffset, arginfo_class_pmmp_encoding_BaseByteBuffer_getOffset, ZEND_ACC_PUBLIC)
ZEND_ME(pmmp_encoding_BaseByteBuffer, setOffset, arginfo_class_pmmp_encoding_BaseByteBuffer_setOffset, ZEND_ACC_PUBLIC)
ZEND_ME(pmmp_encoding_BaseByteBuffer, getReserved, arginfo_class_pmmp_encoding_BaseByteBuffer_getReserved, ZEND_ACC_PUBLIC)
ZEND_ME(pmmp_encoding_BaseByteBuffer, getUsedLength, arginfo_class_pmmp_encoding_BaseByteBuffer_getUsedLength, ZEND_ACC_PUBLIC)
ZEND_ME(pmmp_encoding_BaseByteBuffer, getReservedLength, arginfo_class_pmmp_encoding_BaseByteBuffer_getReservedLength, ZEND_ACC_PUBLIC)
ZEND_ME(pmmp_encoding_BaseByteBuffer, reserve, arginfo_class_pmmp_encoding_BaseByteBuffer_reserve, ZEND_ACC_PUBLIC)
ZEND_ME(pmmp_encoding_BaseByteBuffer, trim, arginfo_class_pmmp_encoding_BaseByteBuffer_trim, ZEND_ACC_PUBLIC)
ZEND_ME(pmmp_encoding_BaseByteBuffer, rewind, arginfo_class_pmmp_encoding_BaseByteBuffer_rewind, ZEND_ACC_PUBLIC)
ZEND_ME(pmmp_encoding_BaseByteBuffer, getUnreadLength, arginfo_class_pmmp_encoding_BaseByteBuffer_getUnreadLength, ZEND_ACC_PUBLIC)
ZEND_ME(pmmp_encoding_BaseByteBuffer, __serialize, arginfo_class_pmmp_encoding_BaseByteBuffer___serialize, ZEND_ACC_PUBLIC)
ZEND_ME(pmmp_encoding_BaseByteBuffer, __unserialize, arginfo_class_pmmp_encoding_BaseByteBuffer___unserialize, ZEND_ACC_PUBLIC)
ZEND_ME(pmmp_encoding_BaseByteBuffer, __debugInfo, arginfo_class_pmmp_encoding_BaseByteBuffer___debugInfo, ZEND_ACC_PUBLIC)
Expand Down
8 changes: 4 additions & 4 deletions tests/reserve.phpt
Original file line number Diff line number Diff line change
Expand Up @@ -6,18 +6,18 @@ Test that reserving works correctly
use pmmp\encoding\ByteBuffer;

$buffer = new ByteBuffer("");
var_dump($buffer->getReserved()); //none
var_dump($buffer->getReservedLength()); //none

$buffer->reserve(40);
var_dump($buffer->getReserved()); //40
var_dump($buffer->getReservedLength()); //40
var_dump($buffer->toString()); //still empty, we haven't used any space

$buffer->writeSignedByte(ord("a"));
var_dump($buffer->getReserved()); //40
var_dump($buffer->getReservedLength()); //40
var_dump($buffer->toString());

$buffer->writeByteArray(str_repeat("a", 40)); //cause new allocation, this should double the buffer size to 80
var_dump($buffer->getReserved()); //80
var_dump($buffer->getReservedLength()); //80
var_dump($buffer->toString());

try{
Expand Down
32 changes: 0 additions & 32 deletions tests/unread-length.phpt

This file was deleted.

0 comments on commit 0ef9289

Please sign in to comment.