@- Fix behaviour of strtok. Bug 13866 (jmoore)

# I have brought the behaviour of strtok into line with how the
# libc strtok's behave. currently given
# <string1><token><string2><token><token>string>
# three recursive calls to strtok returns <string1>. <string2>, <token><string3>
# it now returns <string1>, <string2>, <string3>. (there was some
# debate in #php.bugs if it should return <string1>, <string2>, false, <string3>
# but php's strtok now behaves the same way as the libc version.
This commit is contained in:
James Moore 2001-11-02 19:19:24 +00:00
parent 288871505b
commit c8896a38ae

View file

@ -923,6 +923,7 @@ PHP_FUNCTION(strtok)
char *token_end; char *token_end;
char *p; char *p;
char *pe; char *pe;
int skipped = 0;
if (ZEND_NUM_ARGS() < 1 || ZEND_NUM_ARGS() > 2 || if (ZEND_NUM_ARGS() < 1 || ZEND_NUM_ARGS() > 2 ||
zend_get_parameters_array_ex(ZEND_NUM_ARGS(), args) == FAILURE) zend_get_parameters_array_ex(ZEND_NUM_ARGS(), args) == FAILURE)
@ -963,12 +964,15 @@ PHP_FUNCTION(strtok)
/* Skip leading delimiters */ /* Skip leading delimiters */
while (STRTOK_TABLE(p)) while (STRTOK_TABLE(p))
{
if (++p >= pe) { if (++p >= pe) {
/* no other chars left */ /* no other chars left */
BG(strtok_last) = NULL; BG(strtok_last) = NULL;
RETVAL_FALSE; RETVAL_FALSE;
goto restore; goto restore;
} }
skipped++;
}
/* We know at this place that *p is no delimiter, so skip it */ /* We know at this place that *p is no delimiter, so skip it */
while (++p < pe) while (++p < pe)
@ -977,7 +981,7 @@ PHP_FUNCTION(strtok)
if (p - BG(strtok_last)) { if (p - BG(strtok_last)) {
return_token: return_token:
RETVAL_STRINGL(BG(strtok_last), p - BG(strtok_last), 1); RETVAL_STRINGL(BG(strtok_last) + skipped, (p - BG(strtok_last)) - skipped, 1);
BG(strtok_last) = p + 1; BG(strtok_last) = p + 1;
} else { } else {
RETVAL_FALSE; RETVAL_FALSE;