cvs: php4 /ext/standard array.c basic_functions.c php_array.h
| From: | Rasmus Lerdorf | Date: | Sun, 21 Oct 2001 07:42:36 +0000 |
| Subject: | cvs: php4 /ext/standard array.c basic_functions.c php_array.h | ||
| Groups: | php.cvs | ||
| Request: | Send a blank email to php-cvs+get-7890@lists.php.net to get a copy of this message | ||
rasmus Sun Oct 21 03:42:36 2001 EDT
Modified files:
/php4/ext/standard array.c basic_functions.c php_array.h
Log:
Motivated by bug #13607 I wrote up a simple array_init() function that
lets you quickly create an array and initialize each element to a certain
value.
@ Add array_init() function (Rasmus)
Index: php4/ext/standard/array.c
diff -u php4/ext/standard/array.c:1.141 php4/ext/standard/array.c:1.142
--- php4/ext/standard/array.c:1.141 Sun Oct 7 09:49:08 2001
+++ php4/ext/standard/array.c Sun Oct 21 03:42:35 2001
@@ -21,7 +21,7 @@
+----------------------------------------------------------------------+
*/
-/* $Id: array.c,v 1.141 2001/10/07 13:49:08 jeroen Exp $ */
+/* $Id: array.c,v 1.142 2001/10/21 07:42:35 rasmus Exp $ */
#include "php.h"
#include "php_ini.h"
@@ -516,7 +516,7 @@
}
RETURN_TRUE;
}
-
+/* }}} */
static int array_user_compare(const void *a, const void *b TSRMLS_DC)
{
@@ -1322,6 +1322,55 @@
}
efree(args);
+}
+/* }}} */
+
+/* {{{ proto array array_init(int start_key, int num, mixed val)
+ Create an array containing num elements starting with index start_key each initialized to val */
+PHP_FUNCTION(array_init)
+{
+ zval **start_key, **num, **val, *newval;
+ int i;
+
+ if (ZEND_NUM_ARGS() != 3 || zend_get_parameters_ex(3, &start_key, &num, &val) ==
FAILURE) {
+ WRONG_PARAM_COUNT;
+ }
+
+ /* allocate an array for return */
+ if (array_init(return_value) == FAILURE) {
+ RETURN_FALSE;
+ }
+
+ SEPARATE_ZVAL(val);
+
+ switch(Z_TYPE_PP(start_key)) {
+ case IS_STRING:
+ case IS_LONG:
+ case IS_DOUBLE:
+ convert_to_long_ex(start_key);
+ MAKE_STD_ZVAL(newval);
+ *newval = **val;
+ zval_copy_ctor(newval);
+ add_index_zval(return_value, Z_LVAL_PP(start_key), newval);
+ break;
+ default:
+ php_error(E_WARNING, "Wrong datatype for start key in array_init()");
+ RETURN_FALSE;
+ break;
+ }
+
+ convert_to_long_ex(num);
+ i = Z_LVAL_PP(num) - 1;
+ if(i<0) {
+ php_error(E_WARNING, "Number of elements must be positive in array_init()");
+ RETURN_FALSE;
+ }
+ while(i--) {
+ MAKE_STD_ZVAL(newval);
+ *newval = **val;
+ zval_copy_ctor(newval);
+ add_next_index_zval(return_value, newval);
+ }
}
/* }}} */
Index: php4/ext/standard/basic_functions.c
diff -u php4/ext/standard/basic_functions.c:1.409 php4/ext/standard/basic_functions.c:1.410
--- php4/ext/standard/basic_functions.c:1.409 Tue Oct 16 13:08:11 2001
+++ php4/ext/standard/basic_functions.c Sun Oct 21 03:42:35 2001
@@ -17,7 +17,7 @@
+----------------------------------------------------------------------+
*/
-/* $Id: basic_functions.c,v 1.409 2001/10/16 17:08:11 sniper Exp $ */
+/* $Id: basic_functions.c,v 1.410 2001/10/21 07:42:35 rasmus Exp $ */
#include "php.h"
#include "php_main.h"
@@ -755,6 +755,7 @@
PHP_FE(array_search, NULL)
PHP_FE(extract, NULL)
PHP_FE(compact, NULL)
+ PHP_FE(array_init, NULL)
PHP_FE(range, NULL)
PHP_FE(array_multisort, NULL)
PHP_FE(array_push, first_arg_force_ref)
Index: php4/ext/standard/php_array.h
diff -u php4/ext/standard/php_array.h:1.29 php4/ext/standard/php_array.h:1.30
--- php4/ext/standard/php_array.h:1.29 Fri Oct 5 14:38:53 2001
+++ php4/ext/standard/php_array.h Sun Oct 21 03:42:35 2001
@@ -19,7 +19,7 @@
+----------------------------------------------------------------------+
*/
-/* $Id: php_array.h,v 1.29 2001/10/05 18:38:53 edink Exp $ */
+/* $Id: php_array.h,v 1.30 2001/10/21 07:42:35 rasmus Exp $ */
#ifndef PHP_ARRAY_H
#define PHP_ARRAY_H
@@ -52,6 +52,7 @@
PHP_FUNCTION(array_search);
PHP_FUNCTION(extract);
PHP_FUNCTION(compact);
+PHP_FUNCTION(array_init);
PHP_FUNCTION(range);
PHP_FUNCTION(shuffle);
PHP_FUNCTION(array_multisort);