一些常用字符串操作函数的内部实现
我整理了一下常用的字符串库函数的内部实现,截自linux内核中的lib/string.c文件,绝对标准的程序,供大家参考。
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
一些常用字符串操作函数的内部实现相关推荐
- C语言常用字符串操作函数大全详解(strstr,strtok,strrchr,strcat,strcmp,strcpy,strerror,strspn,strchr等)
参考:string.h中常用字符串操作函数说明(strstr,strtok,strrchr,strcat,strcmp,strcpy,strerror,strspn,strchr等) 作者:一只青木呀 ...
- C语言的常用字符串操作函数(一)
一直做的是单片机相关的程序设计,所以程序设计上更偏向底层,对于字符串的操作也仅限于液晶屏幕上的显示等工作,想提高下字符串操作的水平,而不是笨拙的数组替换等方式,翻看帖子发现C语言的字符串操作函数竟然这 ...
- java 字符串常用函数_Java学习笔记35:Java常用字符串操作函数
package com.xxx.controller.api; import org.apache.commons.lang.StringUtils; public class Test { publ ...
- Hive常用函数(日期函数,取整函数,字符串操作函数,集合操作函数)
常用函数 常用日期函数 常用取整函数 常用字符串操作函数 集合操作函数 多维分析 常用日期函数 unix_timestamp:返回当前或指定时间的时间戳 select unix_timestamp() ...
- c语言中空格字符怎么表示_C语言中常用的字符串操作函数
作者:陈太浪 出处:https://home.cnblogs.com/u/TomHe789/ C语言中提供了许多的字符串操作函数,常见的字符串操作函数有以下几种: 1.求字符串长度的函数 原型函数:s ...
- 【STL】string详解(string类常用的操作函数、构造函数、赋值操作、子符串的拼接、查找和替换、比较、存取、插入和删除、获取)
目录 1. string容器 简介 2. string类常用的操作函数 3. 构造函数 4. 赋值操作 5. 字符串拼接 6. 字符串查找和替换 7. 字符串比较 8. 字符串存取 9. 字符串插入和 ...
- c语言内存复制函数,【C语言】 字符串操作函数及内存拷贝函数归总
今天在这里把零散的一些常用的字符串操作函数和内存拷贝函数进行一下归总实现. 一 . 字符串操作函数 字符串操作函数有很多,这里我列举一些常用的函数,以及自实现的代码: 字符串拷贝函数: 函数原型:ch ...
- js 字符串操作函数有哪些
js 字符串操作函数有哪些 一.总结 一句话总结:js字符串函数都是字符串对象的方法,是通过调用字符串方法的方式调用,和java,php里面不一样. 1.字符串替换函数怎么用? 这里的正则表示是加双引 ...
- linux 算法函数,数据结构——算法之(012)( linux C 全部字符串操作函数实现)...
数据结构--算法之(012)( linux C 所有字符串操作函数实现) 题目:实现linux C下常用的字符串操作函数 题目分析: 一.面试中可能经常遇到这样的问题:比如strcpy.memcpy. ...
最新文章
- xp本地计算机策略被更改,组策略的使用方法,和XP系统的实用修改窍门
- c库函数-strtol()介绍
- 再举个webstrom 正则应用例子。
- Linux Ctrl+c与ctrl+z的区别
- 使计算机进入休眠状态
- iphone:点击背景隐藏键盘
- pq 中m函数判断嵌套_PowerQuery 进阶之 M 函数学习
- MFC 键盘鼠标钩子
- 常用#免费%代理IP库整理*收藏——实时@更新(大概)
- Unity中更改鼠标光标样式
- matlab语音信号的采集与处理,基于MATLAB的语音信号的采集与处理详解
- 数据结构课程设计(银行叫号机)
- python调用gpu amd_TensorFlow使用AMD GPU实现加速(ROCm/Ubuntu 18.04)
- Python爬虫报错 ImportError: cannot import name Morsel
- Windows11设置登录密码
- java 地图轨迹_百度地图多个坐标连成轨迹
- 华科博士201万,西安交大本科生100万!华为「天才少年」校招薪资曝光
- 浅聊古代————汉朝
- java 静态分析_静态代码分析与代码质量安全
- 阿里百秀移动端首页案例