<2012 12 17> C标准库中一些字符串操作函数的实现

    刚学C语言的时候经常做这样的题,比如实现strcpy  strcat库函数的内部实现,有的公司笔试也会考这些。截自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;

}

 

你可能感兴趣的:(2012)