Skip to content

Allow passing null as $length to slice() methods #197

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

Merged
merged 1 commit into from
Jul 29, 2023
Merged
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
4 changes: 4 additions & 0 deletions src/common.h
Original file line number Diff line number Diff line change
Expand Up @@ -216,6 +216,10 @@ int name##_unserialize( \
zend_ce_type_error, \
"Index must be of type integer, %s given", zend_get_type_by_const(Z_TYPE_P(z)))

#define INTEGER_LENGTH_REQUIRED(z) ds_throw_exception( \
zend_ce_type_error, \
"Length must be of type integer, %s given", zend_get_type_by_const(Z_TYPE_P(z)))

#define ITERATION_BY_REF_NOT_SUPPORTED() ds_throw_exception( \
zend_ce_error, \
"Iterating by reference is not supported")
Expand Down
12 changes: 8 additions & 4 deletions src/php/classes/php_deque_ce.c
Original file line number Diff line number Diff line change
Expand Up @@ -85,11 +85,15 @@ METHOD(slice)
{
ds_deque_t *deque = THIS_DS_DEQUE();

if (ZEND_NUM_ARGS() > 1) {
PARSE_LONG_AND_LONG(index, length);
RETURN_DS_DEQUE(ds_deque_slice(deque, index, length));
PARSE_LONG_AND_OPTIONAL_ZVAL(index, length);

if (ZEND_NUM_ARGS() > 1 && Z_TYPE_P(length) != IS_NULL) {
if (Z_TYPE_P(length) != IS_LONG) {
INTEGER_LENGTH_REQUIRED(length);
} else {
RETURN_DS_DEQUE(ds_deque_slice(deque, index, Z_LVAL_P(length)));
}
} else {
PARSE_LONG(index);
RETURN_DS_DEQUE(ds_deque_slice(deque, index, deque->size));
}
}
Expand Down
12 changes: 8 additions & 4 deletions src/php/classes/php_map_ce.c
Original file line number Diff line number Diff line change
Expand Up @@ -246,11 +246,15 @@ METHOD(slice)
{
ds_map_t *map = THIS_DS_MAP();

if (ZEND_NUM_ARGS() > 1) {
PARSE_LONG_AND_LONG(index, length);
RETURN_DS_MAP(ds_map_slice(map, index, length));
PARSE_LONG_AND_OPTIONAL_ZVAL(index, length);

if (ZEND_NUM_ARGS() > 1 && Z_TYPE_P(length) != IS_NULL) {
if (Z_TYPE_P(length) != IS_LONG) {
INTEGER_LENGTH_REQUIRED(length);
} else {
RETURN_DS_MAP(ds_map_slice(map, index, Z_LVAL_P(length)));
}
} else {
PARSE_LONG(index);
RETURN_DS_MAP(ds_map_slice(map, index, DS_MAP_SIZE(map)));
}
}
Expand Down
12 changes: 8 additions & 4 deletions src/php/classes/php_set_ce.c
Original file line number Diff line number Diff line change
Expand Up @@ -178,11 +178,15 @@ METHOD(slice)
{
ds_set_t *set = THIS_DS_SET();

if (ZEND_NUM_ARGS() > 1) {
PARSE_LONG_AND_LONG(index, length);
RETURN_DS_SET(ds_set_slice(set, index, length));
PARSE_LONG_AND_OPTIONAL_ZVAL(index, length);

if (ZEND_NUM_ARGS() > 1 && Z_TYPE_P(length) != IS_NULL) {
if (Z_TYPE_P(length) != IS_LONG) {
INTEGER_LENGTH_REQUIRED(length);
} else {
RETURN_DS_SET(ds_set_slice(set, index, Z_LVAL_P(length)));
}
} else {
PARSE_LONG(index);
RETURN_DS_SET(ds_set_slice(set, index, DS_SET_SIZE(set)));
}
}
Expand Down
12 changes: 8 additions & 4 deletions src/php/classes/php_vector_ce.c
Original file line number Diff line number Diff line change
Expand Up @@ -204,11 +204,15 @@ METHOD(slice)
{
ds_vector_t *vector = THIS_DS_VECTOR();

if (ZEND_NUM_ARGS() > 1) {
PARSE_LONG_AND_LONG(index, length);
RETURN_DS_VECTOR(ds_vector_slice(vector, index, length));
PARSE_LONG_AND_OPTIONAL_ZVAL(index, length);

if (ZEND_NUM_ARGS() > 1 && Z_TYPE_P(length) != IS_NULL) {
if (Z_TYPE_P(length) != IS_LONG) {
INTEGER_LENGTH_REQUIRED(length);
} else {
RETURN_DS_VECTOR(ds_vector_slice(vector, index, Z_LVAL_P(length)));
}
} else {
PARSE_LONG(index);
RETURN_DS_VECTOR(ds_vector_slice(vector, index, vector->size));
}
}
Expand Down
5 changes: 5 additions & 0 deletions src/php/parameters.h
Original file line number Diff line number Diff line change
Expand Up @@ -44,6 +44,11 @@ zend_long l = 0; \
zval *z = NULL; \
PARSE_2("lz", &l, &z)

#define PARSE_LONG_AND_OPTIONAL_ZVAL(l, z) \
zend_long l = 0; \
zval *z = NULL; \
PARSE_2("l|z", &l, &z)

#define PARSE_ZVAL_AND_LONG(z, l) \
zval *z = NULL; \
zend_long l = 0; \
Expand Down