Skip to content

Commit 45d71e2

Browse files
committed
Make range() function smarter
@ - Improve range() function to support range('a','z') and range(9,0) @ types of ranges. (Rasmus)
1 parent 89a73df commit 45d71e2

File tree

1 file changed

+33
-10
lines changed

1 file changed

+33
-10
lines changed

ext/standard/array.c

Lines changed: 33 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -1334,29 +1334,52 @@ PHP_FUNCTION(compact)
13341334
}
13351335
/* }}} */
13361336

1337-
/* {{{ proto array range(int low, int high)
1338-
Create an array containing the range of integers from low to high (inclusive) */
1337+
/* {{{ proto array range(mixed low, mixed high)
1338+
Create an array containing the range of integers or characters from low to high (inclusive) */
13391339
PHP_FUNCTION(range)
13401340
{
13411341
zval **zlow, **zhigh;
1342-
int low, high;
13431342

13441343
if (ZEND_NUM_ARGS() != 2 || zend_get_parameters_ex(2,&zlow,&zhigh) == FAILURE) {
13451344
WRONG_PARAM_COUNT;
13461345
}
1347-
convert_to_long_ex(zlow);
1348-
convert_to_long_ex(zhigh);
1349-
low = Z_LVAL_PP(zlow);
1350-
high = Z_LVAL_PP(zhigh);
13511346

13521347
/* allocate an array for return */
13531348
if (array_init(return_value) == FAILURE) {
13541349
RETURN_FALSE;
13551350
}
13561351

1357-
for (; low <= high; low++) {
1358-
add_next_index_long(return_value, low);
1359-
}
1352+
if(Z_TYPE_PP(zlow)==IS_STRING && Z_TYPE_PP(zhigh)==IS_STRING) {
1353+
char *low, *high;
1354+
convert_to_string_ex(zlow);
1355+
convert_to_string_ex(zhigh);
1356+
low = Z_STRVAL_PP(zlow);
1357+
high = Z_STRVAL_PP(zhigh);
1358+
if(*low>*high) {
1359+
for (; *low >= *high; (*low)--) {
1360+
add_next_index_stringl(return_value, low, 1, 1);
1361+
}
1362+
} else {
1363+
for (; *low <= *high; (*low)++) {
1364+
add_next_index_stringl(return_value, low, 1, 1);
1365+
}
1366+
}
1367+
} else {
1368+
int low, high;
1369+
convert_to_long_ex(zlow);
1370+
convert_to_long_ex(zhigh);
1371+
low = Z_LVAL_PP(zlow);
1372+
high = Z_LVAL_PP(zhigh);
1373+
if(low>high) {
1374+
for (; low >= high; low--) {
1375+
add_next_index_long(return_value, low);
1376+
}
1377+
} else {
1378+
for (; low <= high; low++) {
1379+
add_next_index_long(return_value, low);
1380+
}
1381+
}
1382+
}
13601383
}
13611384
/* }}} */
13621385

0 commit comments

Comments
 (0)