一些常用字符串操作函数的内部实现
2017-01-08 11:28
441 查看
我整理了一下常用的字符串库函数的内部实现,截自linux内核中的lib/string.c文件,绝对标准的程序,供大家参考。
memset:
memcpy:
memmove:
memcmp:
strcpy:
strncpy:
strcat:
strncat:
strcmp:
strncmp:
strchr:
strlen:
strnlen:
strsep:
strstr:
from:http://blog.chinaunix.net/uid-22030783-id-2142080.html
memset:
/* * memset - Fill a region of memory with the given value * @s: Pointer to the start of the area. * @c: The byte to fill the area with * @count: The size of the area. */ void *memset(void *s, int c, size_t count) { char *xs = s; while (count--) *xs++ = c; return s; }
memcpy:
/* * memcpy - Copy one area of memory to another * @dest: Where to copy to * @src: Where to copy from * @count: The size of the area. */ void *memcpy(void *dest, const void *src, size_t count) { char *tmp = dest; const char *s = src; while (count--) *tmp++ = *s++; return dest; }
memmove:
/* * memmove - Copy one area of memory to another * @dest: Where to copy to * @src: Where to copy from * @count: The size of the area. * Unlike memcpy(), memmove() copes with overlapping areas. */ void *memmove(void *dest, const void *src, size_t count) { char *tmp; const char *s; if (dest <= src) { tmp = dest; s = src; while (count--) *tmp++ = *s++; } else { tmp = dest; tmp += count; s = src; s += count; while (count--) *--tmp = *--s; } return dest; }
memcmp:
/* * memcmp - Compare two areas of memory * @cs: One area of memory * @ct: Another area of memory * @count: The size of the area. */ int memcmp(const void *cs, const void *ct, size_t count) { const unsigned char *su1, *su2; int res = 0; for (su1 = cs, su2 = ct; 0 < count; ++su1, ++su2, count--) if ((res = *su1 - *su2) != 0) break; return res; }
strcpy:
/* * strcpy - Copy a %NUL terminated string * @dest: Where to copy the string to * @src: Where to copy the string from */ char *strcpy(char *dest, const char *src) { char *tmp = dest; while ((*dest++ = *src++) != '\0'); return tmp; }
strncpy:
/* * strncpy - Copy a length-limited, %NUL-terminated string * @dest: Where to copy the string to * @src: Where to copy the string from * @count: The maximum number of bytes to copy * * The result is not %NUL-terminated if the source exceeds * @count bytes. * * In the case where the length of @src is less than that of * count, the remainder of @dest will be padded with %NUL. */ char *strncpy(char *dest, const char *src, size_t count) { char *tmp = dest; while (count) { if ((*tmp = *src) != 0) src++; tmp++; count--; } return dest; }
strcat:
/* * strcat - Append one %NUL-terminated string to another * @dest: The string to be appended to * @src: The string to append to it */ char *strcat(char *dest, const char *src) { char *tmp = dest; while (*dest) dest++; while ((*dest++ = *src++) != '\0'); return tmp; }
strncat:
/* * strncat - Append a length-limited, %NUL-terminated string to another * @dest: The string to be appended to * @src: The string to append to it * @count: The maximum numbers of bytes to copy * * Note that in contrast to strncpy(), strncat() ensures the result is * terminated. */ char *strncat(char *dest, const char *src, size_t count) { char *tmp = dest; if (count) { while (*dest) dest++; while ((*dest++ = *src++) != 0) { if (--count == 0) { *dest = '\0'; break; } } } return tmp; }
strcmp:
/* * strcmp - Compare two strings * @cs: One string * @ct: Another string */ int strcmp(const char *cs, const char *ct) { unsigned char c1, c2; while (1) { c1 = *cs++; c2 = *ct++; if (c1 != c2) return c1 < c2 ? -1 : 1; if (!c1) break; } return 0; }
strncmp:
/* * strncmp - Compare two length-limited strings * @cs: One string * @ct: Another string * @count: The maximum number of bytes to compare */ int strncmp(const char *cs, const char *ct, size_t count) { unsigned char c1, c2; while (count) { c1 = *cs++; c2 = *ct++; if (c1 != c2) return c1 < c2 ? -1 : 1; if (!c1) break; count--; } return 0; }
strchr:
/* * 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; }
strlen:
/* * strlen - Find the length of a string * @s: The string to be sized */ size_t strlen(const char *s) { const char *sc; for (sc = s; *sc != '\0'; ++sc); return sc - s; }
strnlen:
/* * strnlen - Find the length of a length-limited string * @s: The string to be sized * @count: The maximum number of bytes to search */ size_t strnlen(const char *s, size_t count) { const char *sc; for (sc = s; count-- && *sc != '\0'; ++sc); return sc - s; }
strsep:
/* * strsep - Split a string into tokens * @s: The string to be searched * @ct: The characters to search for * * strsep() updates @s to point after the token, ready for the next call. */ char *strsep(char **s, const char *ct) { char *sbegin = *s; char *end; if (sbegin == NULL) return NULL; end = strpbrk(sbegin, ct); if (end) *end++ = '\0'; *s = end; return sbegin; }
strstr:
/* * 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; }
from:http://blog.chinaunix.net/uid-22030783-id-2142080.html
相关文章推荐
- caffe+报错︱深度学习参数调优杂记+caffe训练时的问题+dropout/batch Normalization
- Matplotlib 中文用户指南 3.3 使用 GridSpec 自定义子图位置
- 算法提高 数的运算
- VHDL语法回顾
- 乐观锁和悲观锁 对多并发处理
- 10个学习Android开发的网站推荐
- 5分钟了解MariaDB创建角色功能
- node-bindings无法在Electron中使用的解决办法
- 【Get深一度】相控阵雷达信号环境模型建立-相控阵雷达原理
- java gui事件处理
- trie树
- 深度学习基础
- 自定义过滤器与struts2过滤器执行顺序(web.xml中的配置顺序)
- 算法提高 判断回文
- node基础15:events模块
- SCALA与IDEA安装
- 关于开启curl扩展问题若干
- 去除inline-block元素间间距的N种方法
- 最大公约数——gcd
- JAVA 根据数据库表内容生产树结构JSON数据的实例代码