diff options
author | Rasmus Lerdorf <rasmus@php.net> | 2001-07-09 20:36:47 +0000 |
---|---|---|
committer | Rasmus Lerdorf <rasmus@php.net> | 2001-07-09 20:36:47 +0000 |
commit | 45d71e2951ed23aa6ceb0edf6af6ae4e1eebc088 (patch) | |
tree | e3339f781f9acccf10545cf792ac2c1a36787d36 | |
parent | 89a73df39cb5e5d25d3a7989f616a13b1c93496f (diff) | |
download | php-git-45d71e2951ed23aa6ceb0edf6af6ae4e1eebc088.tar.gz |
Make range() function smarter
@ - Improve range() function to support range('a','z') and range(9,0)
@ types of ranges. (Rasmus)
-rw-r--r-- | ext/standard/array.c | 43 |
1 files changed, 33 insertions, 10 deletions
diff --git a/ext/standard/array.c b/ext/standard/array.c index 8c27c7eeba..4dd3b1fe14 100644 --- a/ext/standard/array.c +++ b/ext/standard/array.c @@ -1334,29 +1334,52 @@ PHP_FUNCTION(compact) } /* }}} */ -/* {{{ proto array range(int low, int high) - Create an array containing the range of integers from low to high (inclusive) */ +/* {{{ proto array range(mixed low, mixed high) + Create an array containing the range of integers or characters from low to high (inclusive) */ PHP_FUNCTION(range) { zval **zlow, **zhigh; - int low, high; if (ZEND_NUM_ARGS() != 2 || zend_get_parameters_ex(2,&zlow,&zhigh) == FAILURE) { WRONG_PARAM_COUNT; } - convert_to_long_ex(zlow); - convert_to_long_ex(zhigh); - low = Z_LVAL_PP(zlow); - high = Z_LVAL_PP(zhigh); /* allocate an array for return */ if (array_init(return_value) == FAILURE) { RETURN_FALSE; } - for (; low <= high; low++) { - add_next_index_long(return_value, low); - } + if(Z_TYPE_PP(zlow)==IS_STRING && Z_TYPE_PP(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)--) { + add_next_index_stringl(return_value, low, 1, 1); + } + } else { + for (; *low <= *high; (*low)++) { + 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--) { + add_next_index_long(return_value, low); + } + } else { + for (; low <= high; low++) { + add_next_index_long(return_value, low); + } + } + } } /* }}} */ |