Skip to content

Commit 9d8f0e7

Browse files
committed
ext/standard/array.c: Use boolean values and types where appropriate
1 parent 56dd321 commit 9d8f0e7

File tree

2 files changed

+51
-62
lines changed

2 files changed

+51
-62
lines changed

ext/standard/array.c

Lines changed: 49 additions & 60 deletions
Original file line numberDiff line numberDiff line change
@@ -1523,11 +1523,7 @@ PHP_FUNCTION(array_walk_recursive)
15231523
}
15241524
/* }}} */
15251525

1526-
/* void php_search_array(INTERNAL_FUNCTION_PARAMETERS, int behavior)
1527-
* 0 = return boolean
1528-
* 1 = return key
1529-
*/
1530-
static inline void _php_search_array(zval *return_value, zval *value, zval *array, bool strict, int behavior) /* {{{ */
1526+
static inline void _php_search_array(zval *return_value, zval *value, zval *array, bool strict, bool return_as_keys)
15311527
{
15321528
zval *entry; /* pointer to array entry */
15331529
zend_ulong num_idx;
@@ -1538,29 +1534,29 @@ static inline void _php_search_array(zval *return_value, zval *value, zval *arra
15381534
ZEND_HASH_FOREACH_KEY_VAL(Z_ARRVAL_P(array), num_idx, str_idx, entry) {
15391535
ZVAL_DEREF(entry);
15401536
if (Z_TYPE_P(entry) == IS_LONG && Z_LVAL_P(entry) == Z_LVAL_P(value)) {
1541-
if (behavior == 0) {
1542-
RETURN_TRUE;
1543-
} else {
1537+
if (return_as_keys) {
15441538
if (str_idx) {
15451539
RETURN_STR_COPY(str_idx);
15461540
} else {
15471541
RETURN_LONG(num_idx);
15481542
}
1543+
} else {
1544+
RETURN_TRUE;
15491545
}
15501546
}
15511547
} ZEND_HASH_FOREACH_END();
15521548
} else {
15531549
ZEND_HASH_FOREACH_KEY_VAL(Z_ARRVAL_P(array), num_idx, str_idx, entry) {
15541550
ZVAL_DEREF(entry);
15551551
if (fast_is_identical_function(value, entry)) {
1556-
if (behavior == 0) {
1557-
RETURN_TRUE;
1558-
} else {
1552+
if (return_as_keys) {
15591553
if (str_idx) {
15601554
RETURN_STR_COPY(str_idx);
15611555
} else {
15621556
RETURN_LONG(num_idx);
15631557
}
1558+
} else {
1559+
RETURN_TRUE;
15641560
}
15651561
}
15661562
} ZEND_HASH_FOREACH_END();
@@ -1569,42 +1565,42 @@ static inline void _php_search_array(zval *return_value, zval *value, zval *arra
15691565
if (Z_TYPE_P(value) == IS_LONG) {
15701566
ZEND_HASH_FOREACH_KEY_VAL(Z_ARRVAL_P(array), num_idx, str_idx, entry) {
15711567
if (fast_equal_check_long(value, entry)) {
1572-
if (behavior == 0) {
1573-
RETURN_TRUE;
1574-
} else {
1568+
if (return_as_keys) {
15751569
if (str_idx) {
15761570
RETURN_STR_COPY(str_idx);
15771571
} else {
15781572
RETURN_LONG(num_idx);
15791573
}
1574+
} else {
1575+
RETURN_TRUE;
15801576
}
15811577
}
15821578
} ZEND_HASH_FOREACH_END();
15831579
} else if (Z_TYPE_P(value) == IS_STRING) {
15841580
ZEND_HASH_FOREACH_KEY_VAL(Z_ARRVAL_P(array), num_idx, str_idx, entry) {
15851581
if (fast_equal_check_string(value, entry)) {
1586-
if (behavior == 0) {
1587-
RETURN_TRUE;
1588-
} else {
1582+
if (return_as_keys) {
15891583
if (str_idx) {
15901584
RETURN_STR_COPY(str_idx);
15911585
} else {
15921586
RETURN_LONG(num_idx);
15931587
}
1588+
} else {
1589+
RETURN_TRUE;
15941590
}
15951591
}
15961592
} ZEND_HASH_FOREACH_END();
15971593
} else {
15981594
ZEND_HASH_FOREACH_KEY_VAL(Z_ARRVAL_P(array), num_idx, str_idx, entry) {
15991595
if (fast_equal_check_function(value, entry)) {
1600-
if (behavior == 0) {
1601-
RETURN_TRUE;
1602-
} else {
1596+
if (return_as_keys) {
16031597
if (str_idx) {
16041598
RETURN_STR_COPY(str_idx);
16051599
} else {
16061600
RETURN_LONG(num_idx);
16071601
}
1602+
} else {
1603+
RETURN_TRUE;
16081604
}
16091605
}
16101606
} ZEND_HASH_FOREACH_END();
@@ -1613,13 +1609,8 @@ static inline void _php_search_array(zval *return_value, zval *value, zval *arra
16131609

16141610
RETURN_FALSE;
16151611
}
1616-
/* }}} */
16171612

1618-
/* void php_search_array(INTERNAL_FUNCTION_PARAMETERS, int behavior)
1619-
* 0 = return boolean
1620-
* 1 = return key
1621-
*/
1622-
static inline void php_search_array(INTERNAL_FUNCTION_PARAMETERS, int behavior)
1613+
static inline void php_search_array(INTERNAL_FUNCTION_PARAMETERS, bool return_as_keys)
16231614
{
16241615
zval *value, /* value to check for */
16251616
*array; /* array to check in */
@@ -1632,13 +1623,13 @@ static inline void php_search_array(INTERNAL_FUNCTION_PARAMETERS, int behavior)
16321623
Z_PARAM_BOOL(strict)
16331624
ZEND_PARSE_PARAMETERS_END();
16341625

1635-
_php_search_array(return_value, value, array, strict, behavior);
1626+
_php_search_array(return_value, value, array, strict, return_as_keys);
16361627
}
16371628

16381629
/* {{{ Checks if the given value exists in the array */
16391630
PHP_FUNCTION(in_array)
16401631
{
1641-
php_search_array(INTERNAL_FUNCTION_PARAM_PASSTHRU, 0);
1632+
php_search_array(INTERNAL_FUNCTION_PARAM_PASSTHRU, false);
16421633
}
16431634
/* }}} */
16441635

@@ -1649,7 +1640,7 @@ ZEND_FRAMELESS_FUNCTION(in_array, 2)
16491640
Z_FLF_PARAM_ZVAL(1, value);
16501641
Z_FLF_PARAM_ARRAY(2, array);
16511642

1652-
_php_search_array(return_value, value, array, false, 0);
1643+
_php_search_array(return_value, value, array, false, false);
16531644

16541645
flf_clean:;
16551646
}
@@ -1663,15 +1654,15 @@ ZEND_FRAMELESS_FUNCTION(in_array, 3)
16631654
Z_FLF_PARAM_ARRAY(2, array);
16641655
Z_FLF_PARAM_BOOL(3, strict);
16651656

1666-
_php_search_array(return_value, value, array, strict, 0);
1657+
_php_search_array(return_value, value, array, strict, false);
16671658

16681659
flf_clean:;
16691660
}
16701661

16711662
/* {{{ Searches the array for a given value and returns the corresponding key if successful */
16721663
PHP_FUNCTION(array_search)
16731664
{
1674-
php_search_array(INTERNAL_FUNCTION_PARAM_PASSTHRU, 1);
1665+
php_search_array(INTERNAL_FUNCTION_PARAM_PASSTHRU, true);
16751666
}
16761667
/* }}} */
16771668

@@ -3556,7 +3547,7 @@ PHP_FUNCTION(array_shift)
35563547
Z_ARRVAL_P(stack)->nNextFreeElement = k;
35573548
} else {
35583549
uint32_t k = 0;
3559-
int should_rehash = 0;
3550+
bool should_rehash = false;
35603551
Bucket *p;
35613552

35623553
/* Get the first value and copy it into the return value */
@@ -3584,7 +3575,7 @@ PHP_FUNCTION(array_shift)
35843575
if (p->key == NULL) {
35853576
if (p->h != k) {
35863577
p->h = k++;
3587-
should_rehash = 1;
3578+
should_rehash = true;
35883579
} else {
35893580
k++;
35903581
}
@@ -3683,8 +3674,8 @@ PHP_FUNCTION(array_splice)
36833674
HashTable *rem_hash = NULL;
36843675
zend_long offset,
36853676
length = 0;
3686-
bool length_is_null = 1;
3687-
int num_in; /* Number of elements in the input array */
3677+
bool length_is_null = true;
3678+
uint32_t num_in; /* Number of elements in the input array */
36883679

36893680
ZEND_PARSE_PARAMETERS_START(2, 4)
36903681
Z_PARAM_ARRAY_EX(array, 0, 1)
@@ -3917,7 +3908,7 @@ PHP_FUNCTION(array_slice)
39173908
}
39183909
/* }}} */
39193910

3920-
PHPAPI int php_array_merge_recursive(HashTable *dest, HashTable *src) /* {{{ */
3911+
PHPAPI bool php_array_merge_recursive(HashTable *dest, HashTable *src) /* {{{ */
39213912
{
39223913
zval *src_entry, *dest_entry;
39233914
zend_string *string_key;
@@ -3929,14 +3920,13 @@ PHPAPI int php_array_merge_recursive(HashTable *dest, HashTable *src) /* {{{ */
39293920
zval *dest_zval = dest_entry;
39303921
HashTable *thash;
39313922
zval tmp;
3932-
int ret;
39333923

39343924
ZVAL_DEREF(src_zval);
39353925
ZVAL_DEREF(dest_zval);
39363926
thash = Z_TYPE_P(dest_zval) == IS_ARRAY ? Z_ARRVAL_P(dest_zval) : NULL;
39373927
if ((thash && GC_IS_RECURSIVE(thash)) || (src_entry == dest_entry && Z_ISREF_P(dest_entry) && (Z_REFCOUNT_P(dest_entry) % 2))) {
39383928
zend_throw_error(NULL, "Recursion detected");
3939-
return 0;
3929+
return false;
39403930
}
39413931

39423932
ZEND_ASSERT(!Z_ISREF_P(dest_entry) || Z_REFCOUNT_P(dest_entry) > 1);
@@ -3960,20 +3950,20 @@ PHPAPI int php_array_merge_recursive(HashTable *dest, HashTable *src) /* {{{ */
39603950
if (thash) {
39613951
GC_TRY_PROTECT_RECURSION(thash);
39623952
}
3963-
ret = php_array_merge_recursive(Z_ARRVAL_P(dest_zval), Z_ARRVAL_P(src_zval));
3953+
bool was_correctly_merged = php_array_merge_recursive(Z_ARRVAL_P(dest_zval), Z_ARRVAL_P(src_zval));
39643954
if (thash) {
39653955
GC_TRY_UNPROTECT_RECURSION(thash);
39663956
}
3967-
if (!ret) {
3968-
return 0;
3957+
if (!was_correctly_merged) {
3958+
return false;
39693959
}
39703960
} else {
39713961
Z_TRY_ADDREF_P(src_zval);
39723962
zval *zv = zend_hash_next_index_insert(Z_ARRVAL_P(dest_zval), src_zval);
39733963
if (EXPECTED(!zv)) {
39743964
Z_TRY_DELREF_P(src_zval);
39753965
zend_cannot_add_element();
3976-
return 0;
3966+
return false;
39773967
}
39783968
}
39793969
zval_ptr_dtor(&tmp);
@@ -3985,12 +3975,12 @@ PHPAPI int php_array_merge_recursive(HashTable *dest, HashTable *src) /* {{{ */
39853975
zval *zv = zend_hash_next_index_insert(dest, src_entry);
39863976
if (UNEXPECTED(!zv)) {
39873977
zend_cannot_add_element();
3988-
return 0;
3978+
return false;
39893979
}
39903980
zval_add_ref(zv);
39913981
}
39923982
} ZEND_HASH_FOREACH_END();
3993-
return 1;
3983+
return true;
39943984
}
39953985
/* }}} */
39963986

@@ -4000,7 +3990,7 @@ PHPAPI int php_array_merge(HashTable *dest, HashTable *src) /* {{{ */
40003990
zend_string *string_key;
40013991

40023992
if (HT_IS_PACKED(dest) && HT_IS_PACKED(src)) {
4003-
zend_hash_extend(dest, zend_hash_num_elements(dest) + zend_hash_num_elements(src), 1);
3993+
zend_hash_extend(dest, zend_hash_num_elements(dest) + zend_hash_num_elements(src), true);
40043994
ZEND_HASH_FILL_PACKED(dest) {
40053995
ZEND_HASH_PACKED_FOREACH_VAL(src, src_entry) {
40063996
if (UNEXPECTED(Z_ISREF_P(src_entry)) &&
@@ -4029,12 +4019,11 @@ PHPAPI int php_array_merge(HashTable *dest, HashTable *src) /* {{{ */
40294019
}
40304020
/* }}} */
40314021

4032-
PHPAPI int php_array_replace_recursive(HashTable *dest, HashTable *src) /* {{{ */
4022+
PHPAPI bool php_array_replace_recursive(HashTable *dest, HashTable *src) /* {{{ */
40334023
{
40344024
zval *src_entry, *dest_entry, *src_zval, *dest_zval;
40354025
zend_string *string_key;
40364026
zend_ulong num_key;
4037-
int ret;
40384027

40394028
ZEND_HASH_FOREACH_KEY_VAL(src, num_key, string_key, src_entry) {
40404029
src_zval = src_entry;
@@ -4067,7 +4056,7 @@ PHPAPI int php_array_replace_recursive(HashTable *dest, HashTable *src) /* {{{ *
40674056
Z_IS_RECURSIVE_P(src_zval) ||
40684057
(Z_ISREF_P(src_entry) && Z_ISREF_P(dest_entry) && Z_REF_P(src_entry) == Z_REF_P(dest_entry) && (Z_REFCOUNT_P(dest_entry) % 2))) {
40694058
zend_throw_error(NULL, "Recursion detected");
4070-
return 0;
4059+
return false;
40714060
}
40724061

40734062
ZEND_ASSERT(!Z_ISREF_P(dest_entry) || Z_REFCOUNT_P(dest_entry) > 1);
@@ -4081,7 +4070,7 @@ PHPAPI int php_array_replace_recursive(HashTable *dest, HashTable *src) /* {{{ *
40814070
Z_PROTECT_RECURSION_P(src_zval);
40824071
}
40834072

4084-
ret = php_array_replace_recursive(Z_ARRVAL_P(dest_zval), Z_ARRVAL_P(src_zval));
4073+
bool was_replaced_correctly = php_array_replace_recursive(Z_ARRVAL_P(dest_zval), Z_ARRVAL_P(src_zval));
40854074

40864075
if (Z_REFCOUNTED_P(dest_zval)) {
40874076
Z_UNPROTECT_RECURSION_P(dest_zval);
@@ -4090,16 +4079,16 @@ PHPAPI int php_array_replace_recursive(HashTable *dest, HashTable *src) /* {{{ *
40904079
Z_UNPROTECT_RECURSION_P(src_zval);
40914080
}
40924081

4093-
if (!ret) {
4094-
return 0;
4082+
if (!was_replaced_correctly) {
4083+
return false;
40954084
}
40964085
} ZEND_HASH_FOREACH_END();
40974086

4098-
return 1;
4087+
return true;
40994088
}
41004089
/* }}} */
41014090

4102-
static zend_always_inline void php_array_replace_wrapper(INTERNAL_FUNCTION_PARAMETERS, int recursive) /* {{{ */
4091+
static zend_always_inline void php_array_replace_wrapper(INTERNAL_FUNCTION_PARAMETERS, bool recursive) /* {{{ */
41034092
{
41044093
zval *args = NULL;
41054094
zval *arg;
@@ -4149,7 +4138,7 @@ static zend_always_inline void php_array_replace_wrapper(INTERNAL_FUNCTION_PARAM
41494138
}
41504139
/* }}} */
41514140

4152-
static zend_always_inline void php_array_merge_wrapper(INTERNAL_FUNCTION_PARAMETERS, int recursive) /* {{{ */
4141+
static zend_always_inline void php_array_merge_wrapper(INTERNAL_FUNCTION_PARAMETERS, bool recursive) /* {{{ */
41534142
{
41544143
zval *args = NULL;
41554144
zval *arg;
@@ -4272,28 +4261,28 @@ static zend_always_inline void php_array_merge_wrapper(INTERNAL_FUNCTION_PARAMET
42724261
/* {{{ Merges elements from passed arrays into one array */
42734262
PHP_FUNCTION(array_merge)
42744263
{
4275-
php_array_merge_wrapper(INTERNAL_FUNCTION_PARAM_PASSTHRU, 0);
4264+
php_array_merge_wrapper(INTERNAL_FUNCTION_PARAM_PASSTHRU, false);
42764265
}
42774266
/* }}} */
42784267

42794268
/* {{{ Recursively merges elements from passed arrays into one array */
42804269
PHP_FUNCTION(array_merge_recursive)
42814270
{
4282-
php_array_merge_wrapper(INTERNAL_FUNCTION_PARAM_PASSTHRU, 1);
4271+
php_array_merge_wrapper(INTERNAL_FUNCTION_PARAM_PASSTHRU, true);
42834272
}
42844273
/* }}} */
42854274

42864275
/* {{{ Replaces elements from passed arrays into one array */
42874276
PHP_FUNCTION(array_replace)
42884277
{
4289-
php_array_replace_wrapper(INTERNAL_FUNCTION_PARAM_PASSTHRU, 0);
4278+
php_array_replace_wrapper(INTERNAL_FUNCTION_PARAM_PASSTHRU, false);
42904279
}
42914280
/* }}} */
42924281

42934282
/* {{{ Recursively replaces elements from passed arrays into one array */
42944283
PHP_FUNCTION(array_replace_recursive)
42954284
{
4296-
php_array_replace_wrapper(INTERNAL_FUNCTION_PARAM_PASSTHRU, 1);
4285+
php_array_replace_wrapper(INTERNAL_FUNCTION_PARAM_PASSTHRU, true);
42974286
}
42984287
/* }}} */
42994288

@@ -4559,10 +4548,10 @@ PHP_FUNCTION(array_column)
45594548
zval *colval, *data, rv;
45604549
zend_string *column_str = NULL;
45614550
zend_long column_long = 0;
4562-
bool column_is_null = 0;
4551+
bool column_is_null = false;
45634552
zend_string *index_str = NULL;
45644553
zend_long index_long = 0;
4565-
bool index_is_null = 1;
4554+
bool index_is_null = true;
45664555

45674556
ZEND_PARSE_PARAMETERS_START(2, 3)
45684557
Z_PARAM_ARRAY_HT(input)

ext/standard/php_array.h

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -26,8 +26,8 @@ PHP_MINIT_FUNCTION(array);
2626
PHP_MSHUTDOWN_FUNCTION(array);
2727

2828
PHPAPI int php_array_merge(HashTable *dest, HashTable *src);
29-
PHPAPI int php_array_merge_recursive(HashTable *dest, HashTable *src);
30-
PHPAPI int php_array_replace_recursive(HashTable *dest, HashTable *src);
29+
PHPAPI bool php_array_merge_recursive(HashTable *dest, HashTable *src);
30+
PHPAPI bool php_array_replace_recursive(HashTable *dest, HashTable *src);
3131
PHPAPI int php_multisort_compare(const void *a, const void *b);
3232
PHPAPI zend_long php_count_recursive(HashTable *ht);
3333

0 commit comments

Comments
 (0)