/* {{{ php_explode
*/
-PHPAPI void php_explode(char *delim, uint delim_len, char *str, uint str_len, zend_uchar str_type, zval *return_value, int limit)
+PHPAPI void php_explode(char *delim, uint delim_len, char *str, uint str_len, zend_uchar str_type, zval *return_value, long limit)
{
char *p1, *p2, *endp;
add_next_index_stringl(return_value, p1, p2-p1, 1);
p1 = p2 + delim_len;
} while ( (p2 = php_memnstr(p1, delim, delim_len, endp)) != NULL &&
- (limit == -1 || --limit > 1) );
+ --limit > 1 );
if ( p1 <= endp ) {
add_next_index_stringl(return_value, p1, endp-p1, 1);
/* {{{ php_explode_negative_limit
*/
-PHPAPI void php_explode_negative_limit(char *delim, uint delim_len, char *str, uint str_len, zend_uchar str_type, zval *return_value, int limit)
+PHPAPI void php_explode_negative_limit(char *delim, uint delim_len, char *str, uint str_len, zend_uchar str_type, zval *return_value, long limit)
{
-#define EXPLODE_ALLOC_STEP 50
+#define EXPLODE_ALLOC_STEP 64
char *p1, *p2, *endp;
- int allocated = EXPLODE_ALLOC_STEP, found = 0, i = 0, to_return = 0;
- char **positions = safe_emalloc(allocated, sizeof(char *), 0);
endp = str + str_len;
p1 = str;
by doing nothing we return empty array
*/
} else {
+ int allocated = EXPLODE_ALLOC_STEP, found = 0;
+ long i, to_return;
+ char **positions = emalloc(allocated * sizeof(char *));
+
positions[found++] = p1;
do {
if ( found >= allocated ) {
add_next_index_stringl(return_value, positions[i],
(positions[i+1]-delim_len) - positions[i], 1);
}
+ efree(positions);
}
- efree(positions);
#undef EXPLODE_ALLOC_STEP
}
/* }}} */
/* {{{ php_u_explode
* Unicode capable version of php_explode()
*/
-static void php_u_explode(UChar *delim, uint delim_len, UChar *str, uint str_len, zval *return_value, int limit)
+static void php_u_explode(UChar *delim, uint delim_len, UChar *str, uint str_len, zval *return_value, long limit)
{
UChar *p1, *p2, *endp;
add_next_index_unicodel(return_value, p1, p2-p1, 1);
p1 = (UChar *)p2 + delim_len;
} while ((p2 = zend_u_memnstr(p1, delim, delim_len, endp)) != NULL &&
- (limit == -1 || --limit > 1) );
+ --limit > 1 );
if ( p1 <= endp ) {
add_next_index_unicodel(return_value, p1, endp-p1, 1);
/* {{{ php_u_explode_negative_limit
* Unicode capable version of php_explode_negative_limit()
*/
-static void php_u_explode_negative_limit(UChar *delim, uint delim_len, UChar *str, uint str_len, zval *return_value, int limit)
+static void php_u_explode_negative_limit(UChar *delim, uint delim_len, UChar *str, uint str_len, zval *return_value, long limit)
{
-#define EXPLODE_ALLOC_STEP 50
+#define EXPLODE_ALLOC_STEP 64
UChar *p1, *p2, *endp;
- int allocated = EXPLODE_ALLOC_STEP, found = 0, i = 0, to_return = 0;
- UChar **positions = safe_emalloc(allocated, sizeof(UChar *), 0);
endp = str + str_len;
p1 = str;
by doing nothing we return empty array
*/
} else {
+ int allocated = EXPLODE_ALLOC_STEP, found = 0;
+ long i, to_return;
+ UChar **positions = emalloc(allocated * sizeof(UChar *));
+
positions[found++] = p1;
do {
if ( found >= allocated ) {
add_next_index_unicodel(return_value, positions[i],
(positions[i+1]-delim_len) - positions[i], 1);
}
+ efree(positions);
}
- efree(positions);
#undef EXPLODE_ALLOC_STEP
}
/* }}} */
void *str, *delim;
int str_len, delim_len;
zend_uchar str_type, delim_type;
- long limit = -1;
- int argc = ZEND_NUM_ARGS();
+ long limit = LONG_MAX; /* No limit */
- if (zend_parse_parameters(argc TSRMLS_CC, "TT|l", &delim, &delim_len, &delim_type,
+ if (zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC, "TT|l", &delim, &delim_len, &delim_type,
&str, &str_len, &str_type, &limit) == FAILURE) {
return;
}
array_init(return_value);
if ( str_len == 0 ) {
- if (limit >= 0 || argc == 2) {
+ if (limit >= 0) {
if ( str_type == IS_UNICODE ) {
add_next_index_unicodel(return_value, USTR_MAKE(""), sizeof("")-1, 0);
} else {
return;
}
-
- if (limit == 0 || limit == 1) {
+ if (limit > 1) {
if ( str_type == IS_UNICODE ) {
- add_index_unicodel(return_value, 0, (UChar *)str, str_len, 1);
+ php_u_explode((UChar *)delim, delim_len, (UChar *)str, str_len, return_value, limit);
} else {
- add_index_stringl(return_value, 0, (char *)str, str_len, 1);
+ php_explode((char *)delim, delim_len, (char *)str, str_len, str_type, return_value, limit);
}
- } else if (limit < -1 && argc == 3) {
+ } else if (limit < 0) {
if ( str_type == IS_UNICODE ) {
php_u_explode_negative_limit((UChar *)delim, delim_len, (UChar *)str, str_len, return_value, limit);
} else {
}
} else {
if ( str_type == IS_UNICODE ) {
- php_u_explode((UChar *)delim, delim_len, (UChar *)str, str_len, return_value, limit);
+ add_index_unicodel(return_value, 0, (UChar *)str, str_len, 1);
} else {
- php_explode((char *)delim, delim_len, (char *)str, str_len, str_type, return_value, limit);
+ add_index_stringl(return_value, 0, (char *)str, str_len, 1);
}
}
}