1#include<string.h> 2 3staticchar *olds; 4 5#undef strtok 6 7#ifndef STRTOK 8# define STRTOK strtok 9#endif 10 11/* Parse S into tokens separated by characters in DELIM. 12 If S is NULL, the last string strtok() was called with is 13 used. For example: 14 char s[] = "-abc-=-def"; 15 x = strtok(s, "-"); // x = "abc" 16 x = strtok(NULL, "-="); // x = "def" 17 x = strtok(NULL, "="); // x = NULL 18 // s = "abc\0=-def\0" 19*/ 20char * 21STRTOK(char *s, constchar *delim) 22{ 23char *token; 24 25if (s == NULL) 26 s = olds; 27 28/* Scan leading delimiters. */ 29 s += strspn (s, delim); 30if (*s == '\0') 31 { 32 olds = s; 33returnNULL; 34 } 35 36/* Find the end of the token. */ 37 token = s; 38 s = strpbrk (token, delim); 39if (s == NULL) 40/* This token finishes the string. */ 41 olds = __rawmemchr (token, '\0'); 42else 43 { 44/* Terminate the token and make OLDS point past it. */ 45 *s = '\0'; /* 将分隔符所在位置置0,此为TOP2坑 */ 46 olds = s + 1; 47 } 48return token; 49}
/*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; elseif (*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; }
踩坑指南1-不可重入
目前大部分程序都是在多线程环境下运行的,而这也是我们使用strtok容易犯错的原因之一。
我们在上一篇中看到 char buffer[INFO_MAX_SZ]="Aob male 18,Bob male 19,Cob female 20"; 切割的时候仅仅提取出了第一个人的信息。
Nacos /nɑ:kəʊs/ 是 Dynamic Naming and Configuration Service 的首字母简称,一个易于构建 AI Agent 应用的动态服务发现、配置管理和AI智能体管理平台。Nacos 致力于帮助您发现、配置和管理微服务及AI智能体应用。Nacos 提供了一组简单易用的特性集,帮助您快速实现动态服务发现、服务配置、服务元数据、流量管理。Nacos 帮助您更敏捷和容易地构建、交付和管理微服务平台。
Rocky Linux
Rocky Linux(中文名:洛基)是由Gregory Kurtzer于2020年12月发起的企业级Linux发行版,作为CentOS稳定版停止维护后与RHEL(Red Hat Enterprise Linux)完全兼容的开源替代方案,由社区拥有并管理,支持x86_64、aarch64等架构。其通过重新编译RHEL源代码提供长期稳定性,采用模块化包装和SELinux安全架构,默认包含GNOME桌面环境及XFS文件系统,支持十年生命周期更新。