/**
* 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. 使用@Controller注解为什么要配置<mvc:annotation-driven />

    自己看了官方文档,也到网上查了下,目前理解如下: <mvc:annotation-driven/>相当于注册了DefaultAnnotationHandlerMapping和Annotat ...

  2. 使用反射来编写实体类的XML

    前言: 开发过程中经常需要返回某实体类的列表,公司通常用的都是XML格式的接口,小猪借鉴了公司前辈留下的代码一直是类似这么写的: public static string GetXMLList(ILi ...

  3. 0302IT行业就业&软件工程之我所思和所想

    阅读以下文章 http://www.thea.cn/news/terminal/9/9389.html http://www.shzhidao.cn/system/2015/09/22/0102610 ...

  4. java反射之Class.getMethod与getDeclaredMethods()区别

    Class对象的getMethods和getDeclaredMethods都是获取类对象的方法.但是又有所不同.废话不多说, 先看demo package com.westward; public c ...

  5. 北邮新生排位赛1解题报告a-c

    <div class="page-header" style="padding-bottom: 9px; margin: 20px 0px 30px; border ...

  6. Zabbix页面遇到历史记录的乱码需要修改数据库

    Zabbix页面遇到历史记录的乱码需要修改数据库: 解决办法: 1.将 zabbix 数据库中的表备份: 2.手动删除 zabbix 数据库: 3.重新创建 zabbix 库时手动指定字符集为 utf ...

  7. dedecms5.7安装百度(ueditor)编辑器的方法

    第一步:下载相对应编辑器的版本 第二步:修改inc_func_funcAdmin.php文件 打开include下的inc文件夹内的inc_func_funcAdmin.php找到184行,贴入以下代 ...

  8. JavaScript数字精度上代码。

    /**不能超过 9007199254740992 * floatObj 包含加减乘除四个方法,能确保浮点数运算不丢失精度 * * 我们知道计算机编程语言里浮点数计算会存在精度丢失问题(或称舍入误差), ...

  9. dx wpf的各种坑

    这篇随笔总结dx wpf使用中的各种坑,持续更新~ LookUpEdit里内嵌的DXGrid的名字必须是"PART_GridControl",不能不写.也不能写错.我对比了2个小时 ...

  10. RM报表预览,只有固定的1个订单页面

    明明选了多个记录,预览时,只显示最后一个. 原因: 主项数据的数据集选了报表自带的虚拟数据集了.