summaryrefslogtreecommitdiff
path: root/ext/standard/array.c
diff options
context:
space:
mode:
authorDmitry Stogov <dmitry@zend.com>2014-12-30 17:04:24 +0300
committerDmitry Stogov <dmitry@zend.com>2014-12-30 17:04:24 +0300
commitc6bc209e5093676c4e5cc33c01c599e373da427c (patch)
tree5773e31610c31947e127cf39a3feef10f63a037c /ext/standard/array.c
parentf9a70c50a477f7ef72ae37cad6cc8c16a2401c4c (diff)
downloadphp-git-c6bc209e5093676c4e5cc33c01c599e373da427c.tar.gz
Simplify array_unshift()
Diffstat (limited to 'ext/standard/array.c')
-rw-r--r--ext/standard/array.c31
1 files changed, 22 insertions, 9 deletions
diff --git a/ext/standard/array.c b/ext/standard/array.c
index aeac685c10..47029cb446 100644
--- a/ext/standard/array.c
+++ b/ext/standard/array.c
@@ -2207,21 +2207,34 @@ PHP_FUNCTION(array_unshift)
{
zval *args, /* Function arguments array */
*stack; /* Input stack */
- HashTable *new_hash; /* New hashtable for the stack */
- HashTable old_hash;
+ HashTable new_hash; /* New hashtable for the stack */
int argc; /* Number of function arguments */
+ int i;
+ zend_string *key;
+ zval *value;
if (zend_parse_parameters(ZEND_NUM_ARGS(), "a/+", &stack, &args, &argc) == FAILURE) {
return;
}
- /* Use splice to insert the elements at the beginning. Destroy old
- * hashtable and replace it with new one */
- new_hash = php_splice(Z_ARRVAL_P(stack), 0, 0, &args[0], argc, NULL);
- old_hash = *Z_ARRVAL_P(stack);
- *Z_ARRVAL_P(stack) = *new_hash;
- FREE_HASHTABLE(new_hash);
- zend_hash_destroy(&old_hash);
+ zend_hash_init(&new_hash, zend_hash_num_elements(Z_ARRVAL_P(stack)) + argc, NULL, ZVAL_PTR_DTOR, 0);
+ for (i = 0; i < argc; i++) {
+ if (Z_REFCOUNTED(args[i])) {
+ Z_ADDREF(args[i]);
+ }
+ zend_hash_next_index_insert_new(&new_hash, &args[i]);
+ }
+ ZEND_HASH_FOREACH_STR_KEY_VAL_IND(Z_ARRVAL_P(stack), key, value) {
+ if (key) {
+ zend_hash_add_new(&new_hash, key, value);
+ } else {
+ zend_hash_next_index_insert_new(&new_hash, value);
+ }
+ } ZEND_HASH_FOREACH_END();
+
+ Z_ARRVAL_P(stack)->pDestructor = NULL;
+ zend_hash_destroy(Z_ARRVAL_P(stack));
+ *Z_ARRVAL_P(stack) = new_hash;
/* Clean up and return the number of elements in the stack */
RETVAL_LONG(zend_hash_num_elements(Z_ARRVAL_P(stack)));