Linux中strchr与strstr函数实现。
2014-05-20 00:21
591 查看
<span style="font-size:18px;"><strong></strong></span>
</pre><pre code_snippet_id="354054" snippet_file_name="blog_20140520_2_3457534" name="code" class="cpp"><p>#ifndef __HAVE_ARCH_STRCHR</p>/**strchr - Find the first occurrence of a character in a string * @s: The string to be searched * @c: The character to search for */ char *strchr(const char *s, int c)//定义函数接口的方式学一下, { for (; *s != (char)c; ++s) if (*s == '\0') return NULL; return (char *)s; } EXPORT_SYMBOL(strchr);
————————————————————————————————————————————————
上面中把强制类型中放在for中,则每次都要强制一次,可以把char ctemp=(char)c;
for(;ctemp!=*s;++s) if(*s=='\0') return NULL; return (char*) s;
上面中把强制类型中放在for中,则每次都要强制一次,可以把char ctemp=(char)c;
for(;ctemp!=*s;++s) if(*s=='\0') return NULL; return (char*) s;
________________________________________________________
同时还要注意一下for中的++s,虽然效果与s++一样,但效率可能不一样,
在编译器没有优化的情况下s++,表达式是i加1之前的副本,因为要存储这个副本,
所以会多一些开销。单独在for中的s++;是个表达式,这个表达式与i相关的,
即表达式的值为i,但这个表达式之后,i为i+1;
而++i;这个表达式的i是直接加1,也为i本身。表达式的值与i的值相同。
http://blog.csdn.net/zy1691/article/details/4849808
——————————————————————————————————————
static inline char *strchr(const char *s, int c) { char sc, ch = c; for (; (sc = *s++) != ch; ) { if (!sc) return NULL; } return (char *)s - 1; }
——————————————————————————————————————————————
** * strstr - Find the first substring in a %NUL terminated string * @s1: The string to be searched * @s2: The string to search for */ char *strstr(const char *s1, const char *s2) { int l1, l2; l2 = strlen(s2); if (!l2) return (char *)s1; l1 = strlen(s1); while (l1 >= l2) { l1--; if (!memcmp(s1, s2, l2)) return (char *)s1; s1++; } return NULL; } ———————————————————————————————————————————————— 另一种方式 #ifndef HAVE_STRSTR char *strstr(char *s1, char *s2) { /* from libiberty */ char *p; int len = strlen(s2); if (*s2 == '\0') /* everything matches empty string */ return s1; for (p = s1; (p = strchr(p, *s2)) != NULL; p = strchr(p + 1, *s2)) { if (strncmp(p, s2, len) == 0) return (p); } return NULL; } #endif
<pre code_snippet_id="354054" snippet_file_name="blog_20140520_7_5009612">
<pre code_snippet_id="354054" snippet_file_name="blog_20140520_18_5249713" name="code" class="cpp"><pre code_snippet_id="354054" snippet_file_name="blog_20140520_18_5249713">
相关文章推荐
- strchr(),strstr()函数实现
- 自定义实现字符串处理函数 strlen strcmp strcpy strncpy strcat strncat strchr strstr
- 自定义实现字符串处理函数 strlen strcmp strcpy strncpy strcat strncat strchr strstr
- 模拟实现C库函数strcpy,strcat,strstr,strchr,strcmp,memcpy,memmove
- 字符串处理函数strchr和strstr的实现
- 模拟实现C库函数:strchr、strcmp、strstr、memcpy和memmove
- 模拟实现函数库中的常用函数,1实现strcpy 2.实现strcat 3.实现strstr 4.实现strchr 5.实现strcmp 6.实现memcpy 7.实现memmove,8实现memset
- 在linux系统下,简单实现类似windows的_splitpath函数的功能
- 【转】Linux下获取第一个网络接口名称的函数实现
- 抱歉,没分了!!!请教linux下,用C/C++编程,如何知道我要实现某一功能需要用到什么函数
- strstr()函数原型实现
- 解读linux对string.h函数的实现
- C字符串处理函数的实现(Linux)
- strchr()函数的详解与实现
- Linux 平台上getopt函数在Windos平台上的的实现算法
- itoa函数(linux中没有这个函数,因此要自己实现)
- 20、在Linux中实现类似windows中获取配置文件的函数GetProfileString
- pvtrace和Graphviz实现对linux下C程序的函数调用跟踪
- linux下通过dup2标准输出重定向查看系统进程方法(也是popen函数实现的方法)
- strcpy,strcat,strcmp,strstr 实现函数