cvs: php4 /ext/standard array.c
| From: | Rasmus Lerdorf | Date: | Mon, 09 Jul 2001 20:36:47 +0000 |
| Subject: | cvs: php4 /ext/standard array.c | ||
| Groups: | php.cvs | ||
| Request: | Send a blank email to php-cvs+get-6141@lists.php.net to get a copy of this message | ||
rasmus Mon Jul 9 16:36:47 2001 EDT
Modified files:
/php4/ext/standard array.c
Log:
Make range() function smarter
@ - Improve range() function to support range('a','z') and range(9,0)
@ types of ranges. (Rasmus)
Index: php4/ext/standard/array.c
diff -u php4/ext/standard/array.c:1.113 php4/ext/standard/array.c:1.114
--- php4/ext/standard/array.c:1.113 Tue Jun 19 12:03:34 2001
+++ php4/ext/standard/array.c Mon Jul 9 16:36:47 2001
@@ -21,7 +21,7 @@
+----------------------------------------------------------------------+
*/
-/* $Id: array.c,v 1.113 2001/06/19 16:03:34 andi Exp $ */
+/* $Id: array.c,v 1.114 2001/07/09 20:36:47 rasmus Exp $ */
#include "php.h"
#include "php_ini.h"
@@ -1334,29 +1334,52 @@
}
/* }}} */
-/* {{{ 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);
+ }
+ }
+ }
}
/* }}} */