]> granicus.if.org Git - php/commitdiff
@- Fix behaviour of strtok. Bug 13866 (jmoore)
authorJames Moore <jmoore@php.net>
Fri, 2 Nov 2001 19:19:24 +0000 (19:19 +0000)
committerJames Moore <jmoore@php.net>
Fri, 2 Nov 2001 19:19:24 +0000 (19:19 +0000)
# 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.

ext/standard/string.c

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