strsep和strtok_r替代strtok
char *strtok(char *str, const char *delim)
会修改数据源。外部加锁才线程安全(strtok执行结束再解锁执行另一个strtok循环知道工作完成)
主要是以互斥访问strtok实现文件中的static外部变量char*old。源码如下。
#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 + ;
}
return token;
}
char *strsep(char **stringp, const char *delim)
会修改数据源。可重入的,注意这里虽然改动stringp的内容,主要是不在使用static静态变量了。
#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' || delim[] == '\0')
{
char ch = delim[]; if (ch == '\0')
end = NULL;
else
{
if (*begin == ch)
end = begin;
else if (*begin == '\0')
end = NULL;
else
end = strchr (begin + , 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;
}
注意事项,会多次出现空字符串的问题,这是因为strsep在处理多于一个的delimit字符是会返回空字\0符串代替NULL(见源码便知),http://blog.csdn.net/striver1205/article/details/25601885
对比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必须是二级指针,不能是指向字符数组的指针!
正确用法:
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类似。
strsep和strtok_r替代strtok的更多相关文章
- 【C】——strtok()和strtok_r()
下面的说明摘自于最新的Linux内核2.6.29,说明了strtok()这个函数已经不再使用,由速度更快的strsep()代替 /** linux/lib/string.c** Copyright ( ...
- C/C++ 字符串分割: strtok 与 strsep 函数说明
函数原型: char *strtok(char *s, const char *delim); char *strsep(char **s, const char *delim); 功能:strtok ...
- 内存及字符串操作篇strlen strchar strcmp strcoll strcpy strdup strstr strtok strspn strrchr bcmp bcopy bzero index memccpy memset
bcmp(比较内存内容) 相关函数 bcmp,strcasecmp,strcmp,strcoll,strncmp,strncasecmp 表头文件 #include<string.h> 定 ...
- C++常用字符串分割方法
一.用strtok函数进行字符串分割 原型: char *strtok(char *str, const char *delim); 功能:分解字符串为一组字符串. 参数说明:str为要分解的字符串, ...
- C对字符串的部分操作
字符串分割(C++) 经常碰到字符串分割的问题,这里总结下,也方便我以后使用. 一.用strtok函数进行字符串分割 原型: char *strtok(char *str, const char ...
- 字符串分割(C++)(转载)
转载出自:http://www.cnblogs.com/MikeZhang/archive/2012/03/24/MySplitFunCPP.html 经常碰到字符串分割的问题,这里总结下,也方便我以 ...
- C++ 分割字符串两种方法
原文地址:http://blog.csdn.net/xjw532881071/article/details/49154911 字符串切割的使用频率还是挺高的,string本身没有提供切割的方法,但可 ...
- 字符串分割(C++)
一.用strtok函数进行字符串分割 原型: char *strtok(char *str, const char *delim); 功能:分解字符串为一组字符串. 参数说明:str为要分解的字符串, ...
- C++常用字符串分割方法(转)
1.用strtok函数进行字符串分割 原型: char *strtok(char *str, const char *delim); 功能:分解字符串为一组字符串. 参数说明:str为要分解的字符串, ...
随机推荐
- C#设计模式(1)——单例模式
一.概念:确保一个类只有一个实例,并提供一个全局访问点. 二.单例模式具备如下几个特点: 1.只有一个实例. 2.能够自我实例化. 3.提供全局访问点. 三.代码实现 1.简单实现 /// < ...
- Centos7 下面安装 MySql 客户端
Workench 是官发发布的Mysql客户端,是Linux下面比较通用的了, 如果使用X界面,可以试着熟悉下. 下载链接: http://cdn.mysql.com//Downloads/MySQL ...
- React.js 官网入门教程 分离文件 操作无法正常显示HelloWord
对着React官网的教程练习操作,在做到分离文件练习时,按照官网步骤来却怎么也无法正常显示HelloWord. 经测试,html文件中内容改为: <!DOCTYPE html><ht ...
- HTML学习笔记——标签
最近开始学习前端的一些知识,了解了一下Html和CSS. HTML:是网页内容的载体,它负责的是网页的内涵,也就是网页要呈现的内容,包括了图片,视频还有文字.是网页要加载的东西: CSS:是样式表现, ...
- PHP 信号管理
.note-content { font-family: "Helvetica Neue", Arial, "Hiragino Sans GB", STHeit ...
- window.onload 和 DOMContentLoaded区别及如何判断dom是否加载完毕
http://blog.allenm.me/2010/02/window-onload-和-domcontentloaded/ 其中使用IE不支持DOMContentLoaded,那么判断IE是否加载 ...
- OpenStack三种类型的NAT转换
SNAT SNAT即源网络地址转换,这个NAT路由修改IP包包头中的源IP地址.SNAT功能通常用于让只具有私有IP地址的主机能够访问外网,比如,多个PC使用路由器共享上网,每个PC都配置了内网IP, ...
- ip地址库 新浪,淘宝
原文连接地址:http://www.9958.pw/post/city_ip function getAddressFromIp($ip){ $urlTaobao = 'http://ip.taoba ...
- 转→js数组遍历 千万不要使用for...in...
看到一篇内容还不错,但是排版实在糟糕, 逼死强迫症患者啊,直接拉下去找原文连接,找到了,但是已经消失了···500错误... 第一次因为实在看不下去一篇博客的排版, 为了排版而转载... 转载地址:h ...
- Beta阶段项目总结
1. 每个成员在beta 阶段的实践和alpha 阶段有何改进? 王文奇:对数据库的操作更为熟练,在java web中实现对数据库的修改更加完善 刘元柱:对javascript,css和servl ...