strtok和strtok_r的使用原理

strtok_r是linux平台下的strtok函数的线程安全版。windows的string.h中并不包含它。要想使用这个函数,上网搜其linux下的实现源码,复制到你的程序中即可。别的方式应该也有,比如使用GNU C Library。我下载了GNU C Library,在其源代码中找到了strtok_r的实现代码,复制过来。

strtok和strtok_r的源代码

这两个函数的实现,由众多的版本。我strtok_r来自于GNU C Library,strtok则调用了strtok_r。因此先给出strtok_r的源代码。

  1. /* 
  2.  * strtok_r.c: 
  3.  * Implementation of strtok_r for systems which don't have it. 
  4.  * 
  5.  * This is taken from the GNU C library and is distributed under the terms of 
  6.  * the LGPL. See copyright notice below. 
  7.  * 
  8.  */  
  9.   
  10. #ifdef HAVE_CONFIG_H   
  11. #include "configuration.h"   
  12. #endif /* HAVE_CONFIG_H */   
  13.   
  14. #ifndef HAVE_STRTOK_R   
  15.   
  16. static const char rcsid[] = "$Id: strtok_r.c,v 1.1 2001/04/24 14:25:34 chris Exp $";  
  17.   
  18. #include <string.h>   
  19.   
  20. #undef strtok_r   
  21.   
  22. /* Parse S into tokens separated by characters in DELIM. 
  23.    If S is NULL, the saved pointer in SAVE_PTR is used as 
  24.    the next starting point.  For example: 
  25.         char s[] = "-abc-=-def"; 
  26.         char *sp; 
  27.         x = strtok_r(s, "-", &sp);      // x = "abc", sp = "=-def" 
  28.         x = strtok_r(NULL, "-=", &sp);  // x = "def", sp = NULL 
  29.         x = strtok_r(NULL, "=", &sp);   // x = NULL 
  30.                 // s = "abc/0-def/0" 
  31. */  
  32. char *strtok_r(char *s, const char *delim, char **save_ptr) {  
  33.     char *token;  
  34.   
  35.     if (s == NULL) s = *save_ptr;  
  36.   
  37.     /* Scan leading delimiters.  */  
  38.     s += strspn(s, delim);  
  39.     if (*s == '/0')   
  40.         return NULL;  
  41.   
  42.     /* Find the end of the token.  */  
  43.     token = s;  
  44.     s = strpbrk(token, delim);  
  45.     if (s == NULL)  
  46.         /* This token finishes the string.  */  
  47.         *save_ptr = strchr(token, '/0');  
  48.     else {  
  49.         /* Terminate the token and make *SAVE_PTR point past it.  */  
  50.         *s = '/0';  
  51.         *save_ptr = s + 1;  
  52.     }  
  53.   
  54.     return token;  
  55. }   

代码整体的流程如下:

(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)函数的最后(无论找到还是没找到)都将返回。

对于函数strtok来说,可以理解为用一个内部的静态变量将strtok_r中的save_ptr给保存起来,对调用者不可见。其代码如下:

  1. char *strtok(char *s, const char *delim)  
  2. {  
  3.     static char *last;  
  4.   
  5.     return strtok_r(s, delim, &last);  
  6. }  

有了上述两个函数的实现代码,再理解(一)(二)中所讲的一些要点也就不困难了。

花那么多篇幅总结这两个函数,一来是因为很对人对于strtok的误解比较深,网上很少有对于其非常详细的讨论,因此总结一份比较全面的材料,是有必要的;二来这也是自己不断学习的一个过程,总结会得到远比两个函数重要很多的信息。


  • 1
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值