You are not logged in.

#1 2011-06-08 02:34:32

neoanima
Member
Registered: 2008-05-08
Posts: 40

[SOLVED]glibc2.13 strtok implement


char *
strtok (s, delim)
     char *s;
     const char *delim;
{
  char *token;

  if (s == NULL)
    s = olds;

  /* Scan leading delimiters.  */
s += strspn (s, delim);
  if (*s == '\0')
    {
      olds = s;
      return NULL;
    }

  /* Find the end of the token.  */
  token = s;
  s = strpbrk (token, delim);
  if (s == NULL)
    /* This token finishes the string.  */
    olds = __rawmemchr (token, '\0');
  else
    {
      /* Terminate the token and make OLDS point past it.  */
      *s = '\0';
      olds = s + 1;
    }
  return token;
}

i don't understand bolded line:  s += strspn (s, delim);
the function returns a size_t type wile s is a chr* type from invoking strtok by user,
if compiling this, no warning?
thks!

Last edited by neoanima (2011-06-09 10:45:59)

Offline

#2 2011-06-08 04:11:52

rockin turtle
Member
From: Montana, USA
Registered: 2009-10-22
Posts: 227

Re: [SOLVED]glibc2.13 strtok implement

Note the '+=' operator.

strspn returns the length of the beginning of the string s that contains only characters contained in delim.

For instance if s = "  frobincate" (2 leading spaces) and delim = " \t\n" then strspn would return 2; as the first 2 characters of s (more techincally, the string that s points to) are contained in delim, but the 3rd character 'f' is not.  After the  line s += strspn(s, delim) is executed, s would be incremented to point to the 'f'.  In other words, after the statement, s would be 'frobincate",

Last edited by rockin turtle (2011-06-08 04:18:41)

Offline

#3 2011-06-08 04:37:54

tavianator
Member
From: Waterloo, ON, Canada
Registered: 2007-08-21
Posts: 859
Website

Re: [SOLVED]glibc2.13 strtok implement

In C, "s += n" means "s = s + n".  If s is a pointer, "s + n" means "the address n objects after s," or "&s[n]".  Actually, "s[n]" directly translates to "*(s + n)", which is why you can write "n[ s]" for the same effect (although don't because it's confusing).

Offline

#4 2011-06-08 08:24:39

neoanima
Member
Registered: 2008-05-08
Posts: 40

Re: [SOLVED]glibc2.13 strtok implement

ah, i see. big_smile with warm-hearted guys like you, it is easier to learn programming.
now i know how this statement move the pointer to the first char not in the delim.

thanks!

Last edited by neoanima (2011-06-08 08:25:08)

Offline

#5 2011-06-08 15:38:34

tavianator
Member
From: Waterloo, ON, Canada
Registered: 2007-08-21
Posts: 859
Website

Re: [SOLVED]glibc2.13 strtok implement

You're welcome.  Please mark the thread [SOLVED].

Offline

Board footer

Powered by FluxBB