From: Andrey Hristov Date: Fri, 16 Jul 1999 20:47:20 +0000 (+0000) Subject: A little change in how array_splice/array_slice handle length parameter. X-Git-Tag: php-4.0b1~94 X-Git-Url: https://granicus.if.org/sourcecode?a=commitdiff_plain;h=459ed2b1ad1727f032a3f4bfb799a48a6b38ec01;p=php A little change in how array_splice/array_slice handle length parameter. Makes it compatible with Perl. --- diff --git a/ext/standard/basic_functions.c b/ext/standard/basic_functions.c index 8ab1ef1077..07c672749d 100644 --- a/ext/standard/basic_functions.c +++ b/ext/standard/basic_functions.c @@ -2454,7 +2454,9 @@ HashTable* _phpi_splice(HashTable *in_hash, int offset, int length, offset = 0; /* ..and the length */ - if (length < 0 || offset+length > num_in) + if (length < 0) + length = num_in-offset+length; + else if(offset+length > num_in) length = num_in-offset; /* Create and initialize output hash */ @@ -2702,14 +2704,14 @@ PHP_FUNCTION(array_splice) } /* Get the next two arguments. If length is omitted, - it's assumed to be -1 */ + it's assumed to be until the end of the array */ convert_to_long(args[1]); offset = args[1]->value.lval; if (argc > 2) { convert_to_long(args[2]); length = args[2]->value.lval; } else - length = -1; + length = zend_hash_num_elements(array->value.ht); if (argc == 4) { /* Make sure the last argument, if passed, is an array */ @@ -2782,7 +2784,7 @@ PHP_FUNCTION(array_slice) convert_to_long(length); length_val = length->value.lval; } else - length_val = -1; + length_val = zend_hash_num_elements(input->value.ht); /* Initialize returned array */ array_init(return_value); @@ -2797,7 +2799,9 @@ PHP_FUNCTION(array_slice) offset_val = 0; /* ..and the length */ - if (length_val < 0 || offset_val+length_val > num_in) + if (length_val < 0) + length_val = num_in-offset_val+length_val; + else if(offset_val+length_val > num_in) length_val = num_in-offset_val; if (length_val == 0)