• 博客访问： 935291
• 博文数量： 70
• 博客积分： 1741
• 博客等级： 上尉
• 技术积分： 2476
• 用 户 组： 普通用户
• 注册时间： 2009-07-05 14:46

2018年（1）

2012年（20）

2011年（49）

2011-08-08 23:48:04

刚学C语言的时候经常做这样的题，比如实现strcpy  strcat库函数的内部实现，有的公司笔试也会考这些，我整理了一下常用的字符串库函数的内部实现，截自linux内核中的lib/string.c文件，绝对标准的程序，供大家参考。

memset:
1. /*
2.  * memset - Fill a region of memory with the given value
3.  * @s: Pointer to the start of the area.
4.  * @c: The byte to fill the area with
5.  * @count: The size of the area.
6.  */
7. void *memset(void *s, int c, size_t count)
8. {
9.     char *xs = s;

10.     while (count--)
11.         *xs++ = c;
12.     return s;
13. }

memcpy:
1. /*
2. * memcpy - Copy one area of memory to another
3. * @dest: Where to copy to
4. * @src: Where to copy from
5. * @count: The size of the area.
6. */
7. void *memcpy(void *dest, const void *src, size_t count)
8. {
9. char *tmp = dest;
10. const char *s = src;
11. while (count--)
12. *tmp++ = *s++;
13. return dest;
14. }

memmove:
1. /*
2.  * memmove - Copy one area of memory to another
3.  * @dest: Where to copy to
4.  * @src: Where to copy from
5.  * @count: The size of the area.
6.  * Unlike memcpy(), memmove() copes with overlapping areas.
7.  */
8. void *memmove(void *dest, const void *src, size_t count)
9. {
10.     char *tmp;
11.     const char *s;

12.     if (dest <= src) {
13.         tmp = dest;
14.         s = src;
15.         while (count--)
16.             *tmp++ = *s++;
17.     } else {
18.         tmp = dest;
19.         tmp += count;
20.         s = src;
21.         s += count;
22.         while (count--)
23.             *--tmp = *--s;
24.     }
25.     return dest;
26. }

memcmp:
1. /*
2.  * memcmp - Compare two areas of memory
3.  * @cs: One area of memory
4.  * @ct: Another area of memory
5.  * @count: The size of the area.
6.  */
7. int memcmp(const void *cs, const void *ct, size_t count)
8. {
9.     const unsigned char *su1, *su2;
10.     int res = 0;

11.     for (su1 = cs, su2 = ct; 0 < count; ++su1, ++su2, count--)
12.         if ((res = *su1 - *su2) != 0)
13.             break;
14.     return res;
15. }

strcpy:
1. /*
2.  * strcpy - Copy a %NUL terminated string
3.  * @dest: Where to copy the string to
4.  * @src: Where to copy the string from
5.  */
6. char *strcpy(char *dest, const char *src)
7. {
8.     char *tmp = dest;

9.     while ((*dest++ = *src++) != '\0');

10.     return tmp;
11. }

strncpy:
1. /*
2.  * strncpy - Copy a length-limited, %NUL-terminated string
3.  * @dest: Where to copy the string to
4.  * @src: Where to copy the string from
5.  * @count: The maximum number of bytes to copy
6.  *
7.  * The result is not %NUL-terminated if the source exceeds
8.  * @count bytes.
9.  *
10.  * In the case where the length of @src is less than that of
11.  * count, the remainder of @dest will be padded with %NUL.
12.  */
13. char *strncpy(char *dest, const char *src, size_t count)
14. {
15.     char *tmp = dest;

16.     while (count) {
17.         if ((*tmp = *src) != 0)
18.             src++;
19.         tmp++;
20.         count--;
21.     }

22.     return dest;
23. }

strcat:
1. /*
2.  * strcat - Append one %NUL-terminated string to another
3.  * @dest: The string to be appended to
4.  * @src: The string to append to it
5.  */
6. char *strcat(char *dest, const char *src)
7. {
8.     char *tmp = dest;

9.     while (*dest)
10.         dest++;
11.     while ((*dest++ = *src++) != '\0');

12.     return tmp;
13. }

strncat:
1. /*
2.  * strncat - Append a length-limited, %NUL-terminated string to another
3.  * @dest: The string to be appended to
4.  * @src: The string to append to it
5.  * @count: The maximum numbers of bytes to copy
6.  *
7.  * Note that in contrast to strncpy(), strncat() ensures the result is
8.  * terminated.
9.  */
10. char *strncat(char *dest, const char *src, size_t count)
11. {
12.     char *tmp = dest;

13.     if (count) {
14.         while (*dest)
15.             dest++;
16.         while ((*dest++ = *src++) != 0) {
17.             if (--count == 0) {
18.                 *dest = '\0';
19.                 break;
20.             }
21.         }
22.     }

23.     return tmp;
24. }

strcmp:
1. /*
2.  * strcmp - Compare two strings
3.  * @cs: One string
4.  * @ct: Another string
5.  */
6. int strcmp(const char *cs, const char *ct)
7. {
8.     unsigned char c1, c2;

9.     while (1) {
10.         c1 = *cs++;
11.         c2 = *ct++;
12.         if (c1 != c2)
13.             return c1 < c2 ? -1 : 1;
14.         if (!c1)
15.             break;
16.     }

17.     return 0;
18. }

strncmp:
1. /*
2.  * strncmp - Compare two length-limited strings
3.  * @cs: One string
4.  * @ct: Another string
5.  * @count: The maximum number of bytes to compare
6.  */
7. int strncmp(const char *cs, const char *ct, size_t count)
8. {
9.     unsigned char c1, c2;

10.     while (count) {
11.         c1 = *cs++;
12.         c2 = *ct++;
13.         if (c1 != c2)
14.             return c1 < c2 ? -1 : 1;
15.         if (!c1)
16.             break;
17.         count--;
18.     }

19.     return 0;
20. }

strchr:
1. /*
2.  * strchr - Find the first occurrence of a character in a string
3.  * @s: The string to be searched
4.  * @c: The character to search for
5.  */
6. char *strchr(const char *s, int c)
7. {
8.     for (; *s != (char)c; ++s)
9.         if (*s == '\0')
10.             return NULL;

11.     return (char *)s;
12. }

strlen:
1. /*
2.  * strlen - Find the length of a string
3.  * @s: The string to be sized
4.  */
5. size_t strlen(const char *s)
6. {
7.     const char *sc;

8.     for (sc = s; *sc != '\0'; ++sc);

9.     return sc - s;
10. }

strnlen:
1. /*
2.  * strnlen - Find the length of a length-limited string
3.  * @s: The string to be sized
4.  * @count: The maximum number of bytes to search
5.  */
6. size_t strnlen(const char *s, size_t count)
7. {
8.     const char *sc;

9.     for (sc = s; count-- && *sc != '\0'; ++sc);

10.     return sc - s;
11. }

strsep:
1. /*
2.  * strsep - Split a string into tokens
3.  * @s: The string to be searched
4.  * @ct: The characters to search for
5.  *
6.  * strsep() updates @s to point after the token, ready for the next call.
7.  */
8. char *strsep(char **s, const char *ct)
9. {
10.     char *sbegin = *s;
11.     char *end;

12.     if (sbegin == NULL)
13.         return NULL;

14.     end = strpbrk(sbegin, ct);
15.     if (end)
16.         *end++ = '\0';
17.     *s = end;

18.     return sbegin;
19. }

strstr:
1. /*
2.  * strstr - Find the first substring in a %NUL terminated string
3.  * @s1: The string to be searched
4.  * @s2: The string to search for
5.  */
6. char *strstr(const char *s1, const char *s2)
7. {
8.     int l1, l2;

9.     l2 = strlen(s2);
10.     if (!l2)
11.         return (char *)s1;
12.     l1 = strlen(s1);
13.     while (l1 >= l2) {
14.         l1--;
15.         if (!memcmp(s1, s2, l2))
16.             return (char *)s1;
17.         s1++;
18.     }

19.     return NULL;
20. }