summaryrefslogtreecommitdiff
path: root/ext/standard/array.c
diff options
context:
space:
mode:
Diffstat (limited to 'ext/standard/array.c')
-rw-r--r--ext/standard/array.c117
1 files changed, 54 insertions, 63 deletions
diff --git a/ext/standard/array.c b/ext/standard/array.c
index 2658ad68f5..c56cbd096f 100644
--- a/ext/standard/array.c
+++ b/ext/standard/array.c
@@ -708,9 +708,8 @@ PHP_FUNCTION(krsort)
cmp = php_get_key_compare_func(sort_type, 1);
- if (zend_hash_sort(Z_ARRVAL_P(array), cmp, 0) == FAILURE) {
- RETURN_FALSE;
- }
+ zend_hash_sort(Z_ARRVAL_P(array), cmp, 0);
+
RETURN_TRUE;
}
/* }}} */
@@ -731,9 +730,8 @@ PHP_FUNCTION(ksort)
cmp = php_get_key_compare_func(sort_type, 0);
- if (zend_hash_sort(Z_ARRVAL_P(array), cmp, 0) == FAILURE) {
- RETURN_FALSE;
- }
+ zend_hash_sort(Z_ARRVAL_P(array), cmp, 0);
+
RETURN_TRUE;
}
/* }}} */
@@ -799,13 +797,13 @@ PHP_FUNCTION(count)
/* first, we check if the handler is defined */
if (Z_OBJ_HT_P(array)->count_elements) {
RETVAL_LONG(1);
- if (SUCCESS == Z_OBJ_HT(*array)->count_elements(array, &Z_LVAL_P(return_value))) {
+ if (SUCCESS == Z_OBJ_HT(*array)->count_elements(Z_OBJ_P(array), &Z_LVAL_P(return_value))) {
return;
}
}
/* if not and the object implements Countable we call its count() method */
if (instanceof_function(Z_OBJCE_P(array), zend_ce_countable)) {
- zend_call_method_with_0_params(array, NULL, NULL, "count", &retval);
+ zend_call_method_with_0_params(Z_OBJ_P(array), NULL, NULL, "count", &retval);
if (Z_TYPE(retval) != IS_UNDEF) {
RETVAL_LONG(zval_get_long(&retval));
zval_ptr_dtor(&retval);
@@ -835,13 +833,9 @@ static void php_natsort(INTERNAL_FUNCTION_PARAMETERS, int fold_case) /* {{{ */
ZEND_PARSE_PARAMETERS_END();
if (fold_case) {
- if (zend_hash_sort(Z_ARRVAL_P(array), php_array_natural_case_compare, 0) == FAILURE) {
- return;
- }
+ zend_hash_sort(Z_ARRVAL_P(array), php_array_natural_case_compare, 0);
} else {
- if (zend_hash_sort(Z_ARRVAL_P(array), php_array_natural_compare, 0) == FAILURE) {
- return;
- }
+ zend_hash_sort(Z_ARRVAL_P(array), php_array_natural_compare, 0);
}
RETURN_TRUE;
@@ -880,9 +874,8 @@ PHP_FUNCTION(asort)
cmp = php_get_data_compare_func(sort_type, 0);
- if (zend_hash_sort(Z_ARRVAL_P(array), cmp, 0) == FAILURE) {
- RETURN_FALSE;
- }
+ zend_hash_sort(Z_ARRVAL_P(array), cmp, 0);
+
RETURN_TRUE;
}
/* }}} */
@@ -903,9 +896,8 @@ PHP_FUNCTION(arsort)
cmp = php_get_data_compare_func(sort_type, 1);
- if (zend_hash_sort(Z_ARRVAL_P(array), cmp, 0) == FAILURE) {
- RETURN_FALSE;
- }
+ zend_hash_sort(Z_ARRVAL_P(array), cmp, 0);
+
RETURN_TRUE;
}
/* }}} */
@@ -926,9 +918,8 @@ PHP_FUNCTION(sort)
cmp = php_get_data_compare_func(sort_type, 0);
- if (zend_hash_sort(Z_ARRVAL_P(array), cmp, 1) == FAILURE) {
- RETURN_FALSE;
- }
+ zend_hash_sort(Z_ARRVAL_P(array), cmp, 1);
+
RETURN_TRUE;
}
/* }}} */
@@ -949,9 +940,8 @@ PHP_FUNCTION(rsort)
cmp = php_get_data_compare_func(sort_type, 1);
- if (zend_hash_sort(Z_ARRVAL_P(array), cmp, 1) == FAILURE) {
- RETURN_FALSE;
- }
+ zend_hash_sort(Z_ARRVAL_P(array), cmp, 1);
+
RETURN_TRUE;
}
/* }}} */
@@ -1021,7 +1011,6 @@ static void php_usort(INTERNAL_FUNCTION_PARAMETERS, compare_func_t compare_func,
{
zval *array;
zend_array *arr;
- zend_bool retval;
PHP_ARRAY_CMP_FUNC_VARS;
PHP_ARRAY_CMP_FUNC_BACKUP();
@@ -1040,13 +1029,13 @@ static void php_usort(INTERNAL_FUNCTION_PARAMETERS, compare_func_t compare_func,
/* Copy array, so the in-place modifications will not be visible to the callback function */
arr = zend_array_dup(arr);
- retval = zend_hash_sort(arr, compare_func, renumber) != FAILURE;
+ zend_hash_sort(arr, compare_func, renumber);
zval_ptr_dtor(array);
ZVAL_ARR(array, arr);
PHP_ARRAY_CMP_FUNC_RESTORE();
- RETURN_BOOL(retval);
+ RETURN_TRUE;
}
/* }}} */
@@ -1703,16 +1692,16 @@ static zend_always_inline int php_valid_var_name(const char *var_name, size_t va
}
/* }}} */
-PHPAPI int php_prefix_varname(zval *result, const zval *prefix, const char *var_name, size_t var_name_len, zend_bool add_underscore) /* {{{ */
+PHPAPI int php_prefix_varname(zval *result, zend_string *prefix, const char *var_name, size_t var_name_len, zend_bool add_underscore) /* {{{ */
{
- ZVAL_NEW_STR(result, zend_string_alloc(Z_STRLEN_P(prefix) + (add_underscore ? 1 : 0) + var_name_len, 0));
- memcpy(Z_STRVAL_P(result), Z_STRVAL_P(prefix), Z_STRLEN_P(prefix));
+ ZVAL_NEW_STR(result, zend_string_alloc(ZSTR_LEN(prefix) + (add_underscore ? 1 : 0) + var_name_len, 0));
+ memcpy(Z_STRVAL_P(result), ZSTR_VAL(prefix), ZSTR_LEN(prefix));
if (add_underscore) {
- Z_STRVAL_P(result)[Z_STRLEN_P(prefix)] = '_';
+ Z_STRVAL_P(result)[ZSTR_LEN(prefix)] = '_';
}
- memcpy(Z_STRVAL_P(result) + Z_STRLEN_P(prefix) + (add_underscore ? 1 : 0), var_name, var_name_len + 1);
+ memcpy(Z_STRVAL_P(result) + ZSTR_LEN(prefix) + (add_underscore ? 1 : 0), var_name, var_name_len + 1);
return SUCCESS;
}
@@ -1891,7 +1880,7 @@ static zend_long php_extract_overwrite(zend_array *arr, zend_array *symbol_table
}
/* }}} */
-static zend_long php_extract_ref_prefix_if_exists(zend_array *arr, zend_array *symbol_table, zval *prefix) /* {{{ */
+static zend_long php_extract_ref_prefix_if_exists(zend_array *arr, zend_array *symbol_table, zend_string *prefix) /* {{{ */
{
zend_long count = 0;
zend_string *var_name;
@@ -1947,7 +1936,7 @@ static zend_long php_extract_ref_prefix_if_exists(zend_array *arr, zend_array *s
}
/* }}} */
-static zend_long php_extract_prefix_if_exists(zend_array *arr, zend_array *symbol_table, zval *prefix) /* {{{ */
+static zend_long php_extract_prefix_if_exists(zend_array *arr, zend_array *symbol_table, zend_string *prefix) /* {{{ */
{
zend_long count = 0;
zend_string *var_name;
@@ -1998,7 +1987,7 @@ static zend_long php_extract_prefix_if_exists(zend_array *arr, zend_array *symbo
}
/* }}} */
-static zend_long php_extract_ref_prefix_same(zend_array *arr, zend_array *symbol_table, zval *prefix) /* {{{ */
+static zend_long php_extract_ref_prefix_same(zend_array *arr, zend_array *symbol_table, zend_string *prefix) /* {{{ */
{
zend_long count = 0;
zend_string *var_name;
@@ -2072,7 +2061,7 @@ static zend_long php_extract_ref_prefix_same(zend_array *arr, zend_array *symbol
}
/* }}} */
-static zend_long php_extract_prefix_same(zend_array *arr, zend_array *symbol_table, zval *prefix) /* {{{ */
+static zend_long php_extract_prefix_same(zend_array *arr, zend_array *symbol_table, zend_string *prefix) /* {{{ */
{
zend_long count = 0;
zend_string *var_name;
@@ -2138,7 +2127,7 @@ static zend_long php_extract_prefix_same(zend_array *arr, zend_array *symbol_tab
}
/* }}} */
-static zend_long php_extract_ref_prefix_all(zend_array *arr, zend_array *symbol_table, zval *prefix) /* {{{ */
+static zend_long php_extract_ref_prefix_all(zend_array *arr, zend_array *symbol_table, zend_string *prefix) /* {{{ */
{
zend_long count = 0;
zend_string *var_name;
@@ -2185,7 +2174,7 @@ static zend_long php_extract_ref_prefix_all(zend_array *arr, zend_array *symbol_
}
/* }}} */
-static zend_long php_extract_prefix_all(zend_array *arr, zend_array *symbol_table, zval *prefix) /* {{{ */
+static zend_long php_extract_prefix_all(zend_array *arr, zend_array *symbol_table, zend_string *prefix) /* {{{ */
{
zend_long count = 0;
zend_string *var_name;
@@ -2232,7 +2221,7 @@ static zend_long php_extract_prefix_all(zend_array *arr, zend_array *symbol_tabl
}
/* }}} */
-static zend_long php_extract_ref_prefix_invalid(zend_array *arr, zend_array *symbol_table, zval *prefix) /* {{{ */
+static zend_long php_extract_ref_prefix_invalid(zend_array *arr, zend_array *symbol_table, zend_string *prefix) /* {{{ */
{
zend_long count = 0;
zend_string *var_name;
@@ -2286,7 +2275,7 @@ static zend_long php_extract_ref_prefix_invalid(zend_array *arr, zend_array *sym
}
/* }}} */
-static zend_long php_extract_prefix_invalid(zend_array *arr, zend_array *symbol_table, zval *prefix) /* {{{ */
+static zend_long php_extract_prefix_invalid(zend_array *arr, zend_array *symbol_table, zend_string *prefix) /* {{{ */
{
zend_long count = 0;
zend_string *var_name;
@@ -2428,9 +2417,10 @@ static zend_long php_extract_skip(zend_array *arr, zend_array *symbol_table) /*
Imports variables into symbol table from an array */
PHP_FUNCTION(extract)
{
- zval *var_array_param, *prefix = NULL;
+ zval *var_array_param;
zend_long extract_refs;
zend_long extract_type = EXTR_OVERWRITE;
+ zend_string *prefix = NULL;
zend_long count;
zend_array *symbol_table;
@@ -2438,7 +2428,7 @@ PHP_FUNCTION(extract)
Z_PARAM_ARRAY_EX2(var_array_param, 0, 1, 0)
Z_PARAM_OPTIONAL
Z_PARAM_LONG(extract_type)
- Z_PARAM_ZVAL(prefix)
+ Z_PARAM_STR(prefix)
ZEND_PARSE_PARAMETERS_END();
extract_refs = (extract_type & EXTR_REFS);
@@ -2458,8 +2448,7 @@ PHP_FUNCTION(extract)
}
if (prefix) {
- convert_to_string(prefix);
- if (Z_STRLEN_P(prefix) && !php_valid_var_name(Z_STRVAL_P(prefix), Z_STRLEN_P(prefix))) {
+ if (ZSTR_LEN(prefix) && !php_valid_var_name(ZSTR_VAL(prefix), ZSTR_LEN(prefix))) {
php_error_docref(NULL, E_WARNING, "prefix is not a valid identifier");
return;
}
@@ -3229,7 +3218,7 @@ PHP_FUNCTION(array_pop)
}
ZVAL_COPY_DEREF(return_value, val);
- if (!p->key && Z_ARRVAL_P(stack)->nNextFreeElement > 0 && p->h >= (zend_ulong)(Z_ARRVAL_P(stack)->nNextFreeElement - 1)) {
+ if (!p->key && (zend_long)p->h == (Z_ARRVAL_P(stack)->nNextFreeElement - 1)) {
Z_ARRVAL_P(stack)->nNextFreeElement = Z_ARRVAL_P(stack)->nNextFreeElement - 1;
}
@@ -3471,14 +3460,14 @@ PHP_FUNCTION(array_splice)
Returns elements specified by offset and length */
PHP_FUNCTION(array_slice)
{
- zval *input, /* Input array */
- *z_length = NULL, /* How many elements to get */
- *entry; /* An array entry */
- zend_long offset, /* Offset to get elements from */
- length = 0;
- zend_bool preserve_keys = 0; /* Whether to preserve keys while copying to the new array or not */
- int num_in, /* Number of elements in the input array */
- pos; /* Current position in the array */
+ zval *input; /* Input array */
+ zval *entry; /* An array entry */
+ zend_long offset; /* Offset to get elements from */
+ zend_long length = 0; /* How many elements to get */
+ zend_bool length_is_null = 1; /* Whether an explicit length has been omitted */
+ zend_bool preserve_keys = 0; /* Whether to preserve keys while copying to the new array */
+ uint32_t num_in; /* Number of elements in the input array */
+ uint32_t pos; /* Current position in the array */
zend_string *string_key;
zend_ulong num_key;
@@ -3486,7 +3475,7 @@ PHP_FUNCTION(array_slice)
Z_PARAM_ARRAY(input)
Z_PARAM_LONG(offset)
Z_PARAM_OPTIONAL
- Z_PARAM_ZVAL(z_length)
+ Z_PARAM_LONG_EX(length, length_is_null, 1, 0)
Z_PARAM_BOOL(preserve_keys)
ZEND_PARSE_PARAMETERS_END();
@@ -3494,14 +3483,12 @@ PHP_FUNCTION(array_slice)
num_in = zend_hash_num_elements(Z_ARRVAL_P(input));
/* We want all entries from offset to the end if length is not passed or is null */
- if (ZEND_NUM_ARGS() < 3 || Z_TYPE_P(z_length) == IS_NULL) {
+ if (length_is_null) {
length = num_in;
- } else {
- length = zval_get_long(z_length);
}
/* Clamp the offset.. */
- if (offset > num_in) {
+ if (offset > (zend_long) num_in) {
ZVAL_EMPTY_ARRAY(return_value);
return;
} else if (offset < 0 && (offset = (num_in + offset)) < 0) {
@@ -4153,9 +4140,12 @@ static inline zval *array_column_fetch_prop(zval *data, zval *name, zval *rv) /*
/* The has_property check is first performed in "exists" mode (which returns true for
* properties that are null but exist) and then in "has" mode to handle objects that
* implement __isset (which is not called in "exists" mode). */
- if (Z_OBJ_HANDLER_P(data, has_property)(data, name, ZEND_PROPERTY_EXISTS, NULL)
- || Z_OBJ_HANDLER_P(data, has_property)(data, name, ZEND_PROPERTY_ISSET, NULL)) {
- prop = Z_OBJ_HANDLER_P(data, read_property)(data, name, BP_VAR_R, NULL, rv);
+ zend_string *str, *tmp_str;
+
+ str = zval_get_tmp_string(name, &tmp_str);
+ if (Z_OBJ_HANDLER_P(data, has_property)(Z_OBJ_P(data), str, ZEND_PROPERTY_EXISTS, NULL)
+ || Z_OBJ_HANDLER_P(data, has_property)(Z_OBJ_P(data), str, ZEND_PROPERTY_ISSET, NULL)) {
+ prop = Z_OBJ_HANDLER_P(data, read_property)(Z_OBJ_P(data), str, BP_VAR_R, NULL, rv);
if (prop) {
ZVAL_DEREF(prop);
if (prop != rv) {
@@ -4163,6 +4153,7 @@ static inline zval *array_column_fetch_prop(zval *data, zval *name, zval *rv) /*
}
}
}
+ zend_tmp_string_release(tmp_str);
} else if (Z_TYPE_P(data) == IS_ARRAY) {
if (Z_TYPE_P(name) == IS_STRING) {
prop = zend_symtable_find(Z_ARRVAL_P(data), Z_STR_P(name));