strsep和strtok_r替代strtok

char *strtok(char *str, const char *delim)數組

 會修改數據源。外部加鎖才線程安全(strtok執行結束再解鎖執行另外一個strtok循環知道工做完成)安全

主要是以互斥訪問strtok實現文件中的static外部變量char*old。源碼以下。ide

 

#include <string.h>


static char *olds;

#undef strtok

/* 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"
*/
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;
}
View Code

 

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

會修改數據源。可重入的,注意這裏雖然改動stringp的內容,主要是不在使用static靜態變量了。spa

 

#include <string.h>

#undef __strsep
#undef strsep

char *
__strsep (char **stringp, const char *delim)
{
  char *begin, *end;

  begin = *stringp;
  if (begin == NULL)
    return NULL;

  /* A frequent case is when the delimiter string contains only one
     character.  Here we don't need to call the expensive `strpbrk'
     function and instead work using `strchr'.  */
  if (delim[0] == '\0' || delim[1] == '\0')
  {
      char ch = delim[0];

        if (ch == '\0')
            end = NULL;
         else
        {
            if (*begin == ch)
                end = begin;
            else if (*begin == '\0')
                end = NULL;
            else
                end = 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++ = '\0';
      *stringp = end;
  }
  else
    /* No more delimiters; this is the last token.  */
    *stringp = NULL;

  return begin;
}
View Code

注意事項,會屢次出現空字符串的問題,這是由於strsep在處理多於一個的delimit字符是會返回空字\0符串代替NULL(見源碼便知),http://blog.csdn.net/striver1205/article/details/25601885.net

對比strtok,man手冊提到:線程

The strsep() function was introduced as a replacement for strtok(3), since the latter cannot handle empty fields.
However, strtok(3) conforms to C89/C99 and hence is more portable.指針

stringp必須是二級指針,不能是指向字符數組的指針!code

正確用法:orm

char a[]=......;char *p=a;strsep(&p,delim)

錯誤用法:

char a[]=...;strsep(&a,delim);會包參數類型錯誤

錯誤用法:

char *a=」xxxxx」;strsep(&a,delim)//錯誤,注意原串會被修改內容,字符常量會seg fault

 

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

會修改數據源。可重入,理由和strsep相似。

相關文章
相關標籤/搜索