Strsep and Strtok_r Replacement strtok

Source: Internet
Author: User
Tags strtok

Char *strtok (char *str, const char *delim)

The data source is modified. External lock is thread safe (strtok execution ends and unlocks another strtok loop knowing work is done)

The static external variable char*old in the Strtok implementation file is primarily accessed with mutexes. The source code is as follows.

#include <string.h>Static Char*olds;#undefStrtok/*Parse S into tokens separated by characters in DELIM.  If S is NULL and 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"*/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 = =' /') {olds=s; returnNULL; }  /*Find The end of the token. */token=s; S=strpbrk (token, delim); if(s = =NULL)/*This token finishes the string. */Olds= __RAWMEMCHR (token,' /'); Else    {      /*Terminate the token and make OLDS point past it. */*s =' /'; Olds= S +1; }  returntoken;}
View Code

Char *strsep (char **stringp, const char *delim)

The data source is modified. Reentrant, note that there is nothing to do with STRINGP's changes, mainly the static variables are not used.

#include <string.h>#undef__strsep#undefStrsepChar*__strsep (Char**STRINGP,Const Char*Delim) {  Char*begin, *end; Begin= *STRINGP; if(Begin = =NULL)returnNULL; /*A frequent case was when the delimiter string contains only one character.  Here we don ' t need to the expensive ' strpbrk ' function and instead work using ' STRCHR '. */  if(delim[0] ==' /'|| delim[1] ==' /')  {      CharCH = delim[0]; if(ch = =' /') End=NULL; Else        {            if(*begin = =ch) End=begin; Else if(*begin = =' /') End=NULL; ElseEnd= STRCHR (begin +1, CH); }  }  Else    /*Find The end of the token. */End=strpbrk (begin, Delim); if(end) {/*Terminate the token and set *STRINGP past NUL character. */*end++ =' /'; *STRINGP =end; }  Else    /*No more delimiters; */*STRINGP =NULL; returnbegin;}
View Code

Note that the problem of empty strings occurs more than once because STRSEP handles more than one delimit character by returning an empty character string instead of NULL (see source), http://blog.csdn.net/striver1205/article/ details/25601885

Strsep even changed the pointer of the original string to point to ... This is not the other two functions. The Man manual mentions:

The Strsep () function is introduced as a replacement for strtok (3) and since the latter cannot handle empty fields.
However, Strtok (3) conforms to c89/c99 and hence are more portable.

Char *strtok_r (char *str, const char *delim, char **saveptr)

The data source is modified. Can be re-entered.

Strsep and Strtok_r Replacement strtok

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.