读书人

strtok跟strtok_r源码解析

发布时间: 2013-01-28 11:49:56 作者: rapoo

strtok和strtok_r源码解析

  • /* * strtok_r.c:
  • * Implementation of strtok_r for systems which don't have it. *
  • * This is taken from the GNU C library and is distributed under the terms of * the LGPL. See copyright notice below.
  • * */
  • #ifdef HAVE_CONFIG_H
  • #include "configuration.h" #endif /* HAVE_CONFIG_H */
  • #ifndef HAVE_STRTOK_R
  • static const char rcsid[] = "$Id: strtok_r.c,v 1.1 2001/04/24 14:25:34 chris Exp $";
  • #include <string.h>
  • #undef strtok_r
  • /* Parse S into tokens separated by characters in DELIM.
  • If S is NULL, the saved pointer in SAVE_PTR is used as the next starting point. For example:
  • char s[] = "-abc-=-def"; char *sp;
  • x = strtok_r(s, "-", &sp); // x = "abc", sp = "=-def" x = strtok_r(NULL, "-=", &sp); // x = "def", sp = NULL
  • x = strtok_r(NULL, "=", &sp); // x = NULL // s = "abc/0-def/0"
  • */ char *strtok_r(char *s, const char *delim, char **save_ptr) {
  • char *token;
  • if (s == NULL) s = *save_ptr;
  • /* Scan leading delimiters. */ s += strspn(s, delim); // 返回字符串中第一个不在指定字符串中出现的字符下标,去掉了以空字符开头的 //字串的情况
  • if (*s == '/0') return NULL;
  • /* Find the end of the token. */
  • token = s; s = strpbrk(token, delim); // 返回s1中第一个满足条件的字符的指针
  • if (s == NULL) /* This token finishes the string. */
  • *save_ptr = strchr(token, '/0'); else {
  • /* Terminate the token and make *SAVE_PTR point past it. */ *s = '/0';
  • *save_ptr = s + 1; }
  • return token;
  • }
    主要是对其中函数理解 s += strspn(s, delim);

    strspn(返回字符串中第一个不在指定字符串中出现的字符下标)

      表头文件 #include<string.h>

      定义函数 size_t strspn (const char *s,const char * accept);

      函数说明 strspn()从参数s 字符串的开头计算连续的字符,而这些字符都完全是accept 所指字符串中的字符。简单的说,若strspn()返回的数值为n,则代表字符串s 开头连续有n 个字符都是属于字符串accept内的字符。

      返回值 返回字符串s开头连续包含字符串accept内的字符数目。

    因此,这段代码的意思是,首先屏蔽s开头匹配 delim 中的字符部分

    s = strpbrk(token, delim);

    用法:#include <string.h>

      功能:依次检验字符串s1中的字符,当被检验字符在字符串s2中也包含时,则停止检验,并返回该字符位置,空字符NULL不包括在内。

      说明:返回s1中第一个满足条件的字符的指针,如果没有匹配字符则返回空指针NULL。

      用途:在源字符串(s1)中找出最先含有搜索字符串(s2)中任一字符的位置并返回,若找不到则返回空指针。

      原型:extern char *strpbrk(char *s1, char *s2);

    *save_ptr = strchr(token, '/0');

    原型:extern char *strchr(const char *s,char c);

      const char *strchr(const char* _Str,int _Val)

      char *strchr(char* _Str,int _Ch)

      头文件:#include <string.h>

      功能:查找字符串s中首次出现字符c的位置

      说明:返回首次出现c的位置的指针,如果s中不存在c则返回NULL。

      返回值:Returns the address of the first occurrence of the character in the string if successful, or NULL otherwise

    理解,当 s 为空是,让save_ptr 指针指向字符结尾符'\0',下次再调用strtok_r时,直接返回NULL;

    代码整体的流程如下:

    (1)判断参数s是否为NULL,如果是NULL就以传递进来的save_ptr作为起始分解位置;若不是NULL,则以s开始切分。

    (2)跳过待分解字符串开始的所有分界符。

    (3)判断当前待分解的位置是否为'/0',若是则返回NULL(联系到(一)中所说对返回值为NULL的解释);不是则继续。

    (4)保存当前的待分解串的指针token,调用strpbrk在token中找分界符:如果找不到,则将save_ptr赋值为待分解串尾部'/0'所在的位置,token没有发生变化;若找的到则将分界符所在位置赋值为'/0',token相当于被截断了(提取出来),save_ptr指向分界符的下一位。

    (5)函数的最后(无论找到还是没找到)都将返回。


  • 读书人网 >编程

    热点推荐