Bug#426118: glibc: documentation-by-comment of strtok() wrong on final state of first argument
On Sat, May 26, 2007 at 02:15:09PM +0200, Lionel Elie Mamane wrote:
> Extract from file string/strtok.c:
>
> /* Parse S into tokens separated by characters in DELIM.
> If S is NULL, the last string strtok() was called with is
> used. For example:
> char s[] = "-abc-=-def";
> x = strtok(s, "-"); // x = "abc"
> x = strtok(NULL, "-="); // x = "def"
> x = strtok(NULL, "="); // x = NULL
> // s = "abc\0-def\0"
> */
>
> This, according to my understanding, says that after this code gets
> executed, the contents of s is "abc\0-def\0" followed by one
> unspecified character (to account for the original length of s, which
> is reduced by one in the value given at the end).
> This is wrong. The contents of s after execution of this code is
> "-abc\0=-def\0" as running the attached program will demonstrate.
You are correct; the final comment should contain your string.
> Note that the behaviour described in the comment would imply that
> strtok is Θ(strlen(s)^2) (quadratic time, and no less, in the
> worst case), because it would in some cases shift the whole string by
> one (or more?) positions to the left, which is a linear operation.
The only thing strtok() is doing to s is replacing some of the characters
with NULs.
Reply to: