jon Thu Nov 14 00:46:10 2002 EDT
Modified files:
/php4/ext/standard array.c
Log:
@- Added an optional "step" parameter to range(). (Jon)
Index: php4/ext/standard/array.c
diff -u php4/ext/standard/array.c:1.199 php4/ext/standard/array.c:1.200
--- php4/ext/standard/array.c:1.199 Wed Nov 13 08:31:32 2002
+++ php4/ext/standard/array.c Thu Nov 14 00:46:10 2002
@@ -21,7 +21,7 @@
+----------------------------------------------------------------------+
*/
-/* $Id: array.c,v 1.199 2002/11/13 13:31:32 john Exp $ */
+/* $Id: array.c,v 1.200 2002/11/14 05:46:10 jon Exp $ */
#include "php.h"
#include "php_ini.h"
@@ -1411,48 +1411,60 @@
}
/* }}} */
-/* {{{ proto array range(mixed low, mixed high)
+/* {{{ proto array range(mixed low, mixed high[, int step])
Create an array containing the range of integers or characters from low to high
(inclusive) */
PHP_FUNCTION(range)
{
- zval **zlow, **zhigh;
-
- if (ZEND_NUM_ARGS() != 2 || zend_get_parameters_ex(2, &zlow, &zhigh) ==
FAILURE) {
- WRONG_PARAM_COUNT;
+ zval *zlow, *zhigh;
+ long step = 1;
+
+ if (zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC, "zz|l", &zlow,
+ &zhigh, &step) == FAILURE) {
+ RETURN_FALSE;
}
- /* allocate an array for return */
+ /* We only want positive step values. */
+ if (step < 0) {
+ step *= -1;
+ }
+
+ /* Initialize the return_value as an array. */
if (array_init(return_value) == FAILURE) {
RETURN_FALSE;
}
- if (Z_TYPE_PP(zlow)==IS_STRING && Z_TYPE_PP(zhigh)==IS_STRING) {
+ /* If the range is given as strings, generate an array of characters. */
+ if (Z_TYPE_P(zlow) == IS_STRING && Z_TYPE_P(zhigh) == IS_STRING) {
char *low, *high;
- convert_to_string_ex(zlow);
- convert_to_string_ex(zhigh);
- low = Z_STRVAL_PP(zlow);
- high = Z_STRVAL_PP(zhigh);
- if (*low>*high) {
- for (; *low >= *high; (*low)--) {
+
+ convert_to_string_ex(&zlow);
+ convert_to_string_ex(&zhigh);
+ low = Z_STRVAL_P(zlow);
+ high = Z_STRVAL_P(zhigh);
+
+ if (*low > *high) { /* Negative steps */
+ for (; *low >= *high; (*low) -= step) {
add_next_index_stringl(return_value, low, 1, 1);
- }
- } else {
- for (; *low <= *high; (*low)++) {
+ }
+ } else { /* Positive steps */
+ for (; *low <= *high; (*low) += step) {
add_next_index_stringl(return_value, low, 1, 1);
- }
+ }
}
} else {
int low, high;
- convert_to_long_ex(zlow);
- convert_to_long_ex(zhigh);
- low = Z_LVAL_PP(zlow);
- high = Z_LVAL_PP(zhigh);
- if (low > high) {
- for (; low >= high; low--) {
+
+ convert_to_long_ex(&zlow);
+ convert_to_long_ex(&zhigh);
+ low = Z_LVAL_P(zlow);
+ high = Z_LVAL_P(zhigh);
+
+ if (low > high) { /* Negative steps */
+ for (; low >= high; low -= step) {
add_next_index_long(return_value, low);
}
- } else {
- for (; low <= high; low++) {
+ } else { /* Positive steps */
+ for (; low <= high; low += step) {
add_next_index_long(return_value, low);
}
}
--
PHP CVS Mailing List (http://www.php.net/)
To unsubscribe, visit: http://www.php.net/unsub.php