Skip to content

Commit 40b35a8

Browse files
committed
Merge common array_(sum|product) implementation
1 parent 105edec commit 40b35a8

File tree

1 file changed

+17
-62
lines changed

1 file changed

+17
-62
lines changed

ext/standard/array.c

Lines changed: 17 additions & 62 deletions
Original file line numberDiff line numberDiff line change
@@ -5902,8 +5902,8 @@ PHP_FUNCTION(array_rand)
59025902
}
59035903
/* }}} */
59045904

5905-
/* {{{ Returns the sum of the array entries */
5906-
PHP_FUNCTION(array_sum)
5905+
/* Wrapper for array_sum and array_product */
5906+
static void php_array_binop(INTERNAL_FUNCTION_PARAMETERS, const char *op_name, binary_op_type op, zend_long initial)
59075907
{
59085908
HashTable *input;
59095909
zval *entry;
@@ -5914,29 +5914,29 @@ PHP_FUNCTION(array_sum)
59145914

59155915
if (zend_hash_num_elements(input) == 0) {
59165916
php_error_docref(NULL, E_DEPRECATED, "Passing an empty array is deprecated");
5917-
RETURN_LONG(0);
5917+
RETURN_LONG(initial);
59185918
}
59195919

5920-
ZVAL_LONG(return_value, 0);
5920+
ZVAL_LONG(return_value, initial);
59215921
ZEND_HASH_FOREACH_VAL(input, entry) {
5922-
zend_result status = add_function(return_value, return_value, entry);
5922+
zend_result status = op(return_value, return_value, entry);
59235923
if (status == FAILURE) {
59245924
ZEND_ASSERT(EG(exception));
59255925
zend_clear_exception();
59265926
/* BC resources: previously resources were cast to int */
59275927
if (Z_TYPE_P(entry) == IS_RESOURCE) {
59285928
zval tmp;
59295929
ZVAL_LONG(&tmp, Z_RES_HANDLE_P(entry));
5930-
add_function(return_value, return_value, &tmp);
5930+
op(return_value, return_value, &tmp);
59315931
}
59325932
/* BC non numeric strings: previously were cast to 0 */
59335933
if (Z_TYPE_P(entry) == IS_STRING) {
59345934
zval tmp;
59355935
ZVAL_LONG(&tmp, 0);
5936-
add_function(return_value, return_value, &tmp);
5936+
op(return_value, return_value, &tmp);
59375937
}
5938-
php_error_docref(NULL, E_WARNING, "Addition is not supported on type %s",
5939-
zend_zval_type_name(entry));
5938+
php_error_docref(NULL, E_WARNING, "%s is not supported on type %s",
5939+
op_name, zend_zval_type_name(entry));
59405940
}
59415941
} ZEND_HASH_FOREACH_END();
59425942

@@ -5951,69 +5951,24 @@ PHP_FUNCTION(array_sum)
59515951
zend_error(E_WARNING, "Object of class %s could not be converted to int|float",
59525952
ZSTR_VAL(Z_OBJCE_P(return_value)->name));
59535953
zval_ptr_dtor(return_value);
5954-
RETURN_LONG(0);
5954+
RETURN_LONG(initial);
59555955
}
59565956
zval_ptr_dtor(return_value);
59575957
RETURN_COPY_VALUE(&dst);
59585958
}
59595959
}
5960+
5961+
/* {{{ Returns the sum of the array entries */
5962+
PHP_FUNCTION(array_sum)
5963+
{
5964+
php_array_binop(INTERNAL_FUNCTION_PARAM_PASSTHRU, "Addition", add_function, 0);
5965+
}
59605966
/* }}} */
59615967

59625968
/* {{{ Returns the product of the array entries */
59635969
PHP_FUNCTION(array_product)
59645970
{
5965-
HashTable *input;
5966-
zval *entry;
5967-
5968-
ZEND_PARSE_PARAMETERS_START(1, 1)
5969-
Z_PARAM_ARRAY_HT(input)
5970-
ZEND_PARSE_PARAMETERS_END();
5971-
5972-
if (zend_hash_num_elements(input) == 0) {
5973-
php_error_docref(NULL, E_DEPRECATED, "Passing an empty array is deprecated");
5974-
RETURN_LONG(1);
5975-
}
5976-
5977-
ZVAL_LONG(return_value, 1);
5978-
5979-
ZEND_HASH_FOREACH_VAL(input, entry) {
5980-
zend_result status = mul_function(return_value, return_value, entry);
5981-
if (status == FAILURE) {
5982-
ZEND_ASSERT(EG(exception));
5983-
zend_clear_exception();
5984-
/* BC resources: previously resources were cast to int */
5985-
if (Z_TYPE_P(entry) == IS_RESOURCE) {
5986-
zval tmp;
5987-
ZVAL_LONG(&tmp, Z_RES_HANDLE_P(entry));
5988-
mul_function(return_value, return_value, &tmp);
5989-
}
5990-
/* BC non numeric strings: previously were cast to 0 */
5991-
if (Z_TYPE_P(entry) == IS_STRING) {
5992-
zval tmp;
5993-
ZVAL_LONG(&tmp, 0);
5994-
mul_function(return_value, return_value, &tmp);
5995-
}
5996-
php_error_docref(NULL, E_WARNING, "Multiplication is not supported on type %s",
5997-
zend_zval_type_name(entry));
5998-
}
5999-
} ZEND_HASH_FOREACH_END();
6000-
6001-
/* Traversal of array encountered objects that support multiplication */
6002-
if (Z_TYPE_P(return_value) == IS_OBJECT) {
6003-
/* Cannot use convert_scalar_to_number() as we don't know if the cast succeeded */
6004-
zval dst;
6005-
zend_result status = Z_OBJ_HT_P(return_value)->cast_object(Z_OBJ_P(return_value), &dst, _IS_NUMBER);
6006-
6007-
/* Do not type error for BC */
6008-
if (status == FAILURE || (Z_TYPE(dst) != IS_LONG && Z_TYPE(dst) != IS_DOUBLE)) {
6009-
zend_error(E_WARNING, "Object of class %s could not be converted to int|float",
6010-
ZSTR_VAL(Z_OBJCE_P(return_value)->name));
6011-
zval_ptr_dtor(return_value);
6012-
RETURN_LONG(1);
6013-
}
6014-
zval_ptr_dtor(return_value);
6015-
RETURN_COPY_VALUE(&dst);
6016-
}
5971+
php_array_binop(INTERNAL_FUNCTION_PARAM_PASSTHRU, "Multiplication", mul_function, 1);
60175972
}
60185973
/* }}} */
60195974

0 commit comments

Comments
 (0)