From dedf44bacce095737c3cd9457da218033f61b39a Mon Sep 17 00:00:00 2001 From: Dmitry Stogov Date: Tue, 23 Dec 2014 04:29:27 +0300 Subject: [PATCH] Improved array_keys() --- ext/standard/array.c | 47 +++++++++++++++++++++++++++----------------- 1 file changed, 29 insertions(+), 18 deletions(-) diff --git a/ext/standard/array.c b/ext/standard/array.c index fecc1a59aa..b9ccf5bd56 100644 --- a/ext/standard/array.c +++ b/ext/standard/array.c @@ -2677,11 +2677,9 @@ PHP_FUNCTION(array_keys) *entry, /* An entry in the input array */ res, /* Result of comparison */ new_val; /* New value */ - int add_key; /* Flag to indicate whether a key should be added */ zend_bool strict = 0; /* do strict comparison */ zend_ulong num_idx; zend_string *str_idx; - int (*is_equal_func)(zval *, zval *, zval *) = is_equal_function; #ifndef FAST_ZPP if (zend_parse_parameters(ZEND_NUM_ARGS(), "a|zb", &input, &search_value, &strict) == FAILURE) { @@ -2696,34 +2694,47 @@ PHP_FUNCTION(array_keys) ZEND_PARSE_PARAMETERS_END(); #endif - if (strict) { - is_equal_func = is_identical_function; - } - /* Initialize return array */ if (search_value != NULL) { array_init(return_value); + + if (strict) { + ZEND_HASH_FOREACH_KEY_VAL_IND(Z_ARRVAL_P(input), num_idx, str_idx, entry) { + fast_is_identical_function(&res, search_value, entry); + if (Z_TYPE(res) == IS_TRUE) { + if (str_idx) { + ZVAL_STR_COPY(&new_val, str_idx); + } else { + ZVAL_LONG(&new_val, num_idx); + } + zend_hash_next_index_insert_new(Z_ARRVAL_P(return_value), &new_val); + } + } ZEND_HASH_FOREACH_END(); + } else { + ZEND_HASH_FOREACH_KEY_VAL_IND(Z_ARRVAL_P(input), num_idx, str_idx, entry) { + if (fast_equal_check_function(search_value, entry)) { + if (str_idx) { + ZVAL_STR_COPY(&new_val, str_idx); + } else { + ZVAL_LONG(&new_val, num_idx); + } + zend_hash_next_index_insert_new(Z_ARRVAL_P(return_value), &new_val); + } + } ZEND_HASH_FOREACH_END(); + } } else { array_init_size(return_value, zend_hash_num_elements(Z_ARRVAL_P(input))); - } - add_key = 1; - /* Go through input array and add keys to the return array */ - ZEND_HASH_FOREACH_KEY_VAL_IND(Z_ARRVAL_P(input), num_idx, str_idx, entry) { - if (search_value != NULL) { - is_equal_func(&res, search_value, entry); - add_key = zval_is_true(&res); - } - - if (add_key) { + /* Go through input array and add keys to the return array */ + ZEND_HASH_FOREACH_KEY_VAL_IND(Z_ARRVAL_P(input), num_idx, str_idx, entry) { if (str_idx) { ZVAL_STR_COPY(&new_val, str_idx); } else { ZVAL_LONG(&new_val, num_idx); } zend_hash_next_index_insert_new(Z_ARRVAL_P(return_value), &new_val); - } - } ZEND_HASH_FOREACH_END(); + } ZEND_HASH_FOREACH_END(); + } } /* }}} */ -- 2.40.0