/**
* 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)
/* nothing */;
return sc - s;
} /**
* 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)
/* nothing */;
return sc - s;
} /**
* 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')
/* nothing */;
return tmp;
} /**
* 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) != ) /* src到尽头后不再自加使得dest后面填'\0'了 */
src++;
tmp++;
count--;
}
return dest;
} /**
* 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 - 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++) != ) {
if (--count == ) {
*dest = '\0';
break;
}
}
}
return tmp;
} /**
* strcmp - Compare two strings
* @cs: One string
* @ct: Another string
*/
int strcmp(const char *cs, const char *ct)
{
unsigned char c1, c2; while () {
c1 = *cs++;
c2 = *ct++;
if (c1 != c2)
return c1 < c2 ? - : ;
if (!c1)
break;
}
return ;
} /**
* 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 ? - : ;
if (!c1)
break;
count--;
}
return ;
} /**
* 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;
} /**
* strnchr - Find a character in a length limited string
* @s: The string to be searched
* @count: The number of characters to be searched
* @c: The character to search for
*/
char *strnchr(const char *s, size_t count, int c)
{
for (; count-- && *s != '\0'; ++s)
if (*s == (char)c)
return (char *)s;
return NULL;
} /**
* 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 = ; for (su1 = cs, su2 = ct; < count; ++su1, ++su2, count--)
if ((res = *su1 - *su2) != )
break;
return res;
} /**
* memcpy - Copy one area of memory to another
* @dest: Where to copy to
* @src: Where to copy from
* @count: The size of the area.
*
* You should not use this function to access IO space, use memcpy_toio()
* or memcpy_fromio() instead.
*/
void *memcpy(void *dest, const void *src, size_t count)
{
char *tmp = dest;
const char *s = src; while (count--)
*tmp++ = *s++;
return dest;
} /* Not static to avoid a conflict with the prototype in the Linux headers. */
void *memmove(void *dest, const void *src, size_t size)
{
uint8_t *d = dest;
const uint8_t *s = src;
size_t i;
/* 判断地址高低, 可出现重叠区域 */
if (d < s) {
for (i = ; i < size; ++i)
d[i] = s[i];
} else if (d > s) {
i = size;
while (i-- > )
d[i] = s[i];
} return dest;
} /**
* 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)
{
size_t 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;
} /**
* strnstr - Find the first substring in a length-limited string
* @s1: The string to be searched
* @s2: The string to search for
* @len: the maximum number of characters to search
*/
char *strnstr(const char *s1, const char *s2, size_t len)
{
size_t l2; l2 = strlen(s2);
if (!l2)
return (char *)s1;
while (len >= l2) {
len--;
if (!memcmp(s1, s2, l2))
return (char *)s1;
s1++;
}
return NULL;
}

linux/lib/string.c的更多相关文章

  1. Android开发:Android虚拟机启动错误Can't find 'Linux version ' string in kernel image file

    Android启动出错,虚拟机报错信息如下: Starting emulator for AVD 'test' emulator: ERROR: Can't find 'Linux version ' ...

  2. c++ linux 判断string是中文的 or 英文的 字符串。

    #include <iostream> #include <string.h> #include <stdio.h> #include <stdlib.h&g ...

  3. linux的string操作(字符串截取,长度计算)

    按指定的字符串截取 1.第一种方法: ${varible##*string} 从左向右截取最后一个string后的字符串 ${varible#*string}从左向右截取第一个string后的字符串 ...

  4. Linux c++ string转其他类型

    #include <iostream> #include <sstream> #include <string> using namespace std; temp ...

  5. linux find string in files

    http://blog.csdn.net/duguduchong/article/details/7716908 查找目录下的所有文件中是否含有某个字符串 find .|xargs grep -ri ...

  6. c/cpp中怎样切割字符串,相似于split的功能

    在python中,假设要求当前时间的unix时间戳,我特别喜欢这么用: import time timestr = time.time() timestamp = int(timestr.split( ...

  7. 【C】——strtok()和strtok_r()

    下面的说明摘自于最新的Linux内核2.6.29,说明了strtok()这个函数已经不再使用,由速度更快的strsep()代替 /** linux/lib/string.c** Copyright ( ...

  8. C/C++ 字符串分割: strtok 与 strsep 函数说明

    函数原型: char *strtok(char *s, const char *delim); char *strsep(char **s, const char *delim); 功能:strtok ...

  9. 编写出色的GNU/Linux程序

    http://advancedlinuxprogramming.com提供了本书电子版的免费下载. 1 与执行环境交互 关于参数 C语言程序的main()函数使用两个参数和执行环境交互--(int)a ...

随机推荐

  1. 投资统计查询sql

    select COUNT(*) from YYD_Users_RegInfo where regTime between '2016-07-11 00:00:00' and '2016-07-11 2 ...

  2. 使用jsTree动态加载节点

    因为项目的需要,需要做一个树状菜单,并且节点是动态加载的,也就是只要点击父节点,就会加载该节点下的子节点. 大致的效果实现如下图: 以上的实现就是通过jsTree实现的,一个基于JQuery的树状菜单 ...

  3. 提升web响应速度的思路

    web响应(主要指加载网页类,不包括大文件下载,看视频)的核心瓶颈在于延迟,不在于带宽. 从感性认知的角度,由于存在tcp的慢启动,所以往往速率还未达到带宽值时,访问就已经结束:另外,没有交互就没有延 ...

  4. CSS-长图水平居中

    场景:客户方给我了一张1920px的长图给我,然后告诉我在屏幕不到1920px时候,屏幕显示图片的中心位置,左右边缘可以不要. 当屏幕小于1000px的时候,图片显示中心部分1000px的图片,且可以 ...

  5. 在ubunut下使用pycharm和eclipse进行python远程调试

    我比较喜欢Pycharm,因为这个是JetBrains公司出的python IDE工具,该公司下的java IDE工具--IDEA,无论从界面还是操作上都甩eclipse几条街,但项目组里有些人使用e ...

  6. C语言中的getchar和putchar详解

    首先给出<The_C_Programming_Language>这本书中的例子: #include <stdio.h> int main(){    int c;     c ...

  7. [pjsip]Pjlib中配置文件config.h解析

    config_site.h 这个头文件包含在config.h中,用于引入平台?(site)/用户特定的配置以控制PJLIB的特性,用户需要自己生成这个文件. 譬如说我们要把PJLIB编译成DLL,那么 ...

  8. iOS 获取当前月份的天数(转)

    在这里我很鄙视百度,尼玛 竟然每一个我想要的结果...最后还是用google弄到的.日前又需要自己以后慢慢研究 1. 获取当前月份有多少天 NSCalendar *calendar = [NSCale ...

  9. javascript笔记2-引用类型

    引用类型是一种数据结构,用于将数据和功能组织在一起.它描述的是一类对象所具有的属性和方法.Object是一个基础类型,Array是数组类型,Date是日期类型,RegExp是正则表达式类型,等. Ob ...

  10. <xliff:g>标签

    摘要: 这是Android4.3Mms源码中的strings.xml的一段代码: <!--Settings item desciption for integer auto-delete sms ...