最近很是烦躁,没做什么事,随便花了点时间把 C 库中的 string.h 中的几个函数实现了下,不多说了,直接贴代码吧,可能会存在错误和不好的算法,希望大家见谅:
测试函数写的乱七八糟,就不贴在这里了,有些函数是不需要实现差错控制的,使用不当,程序崩溃那是自己的事,我实现的这些个函数可能和经典的实现存在较大的差距,不过,我说过了,这仅仅是个人觉得好玩才随便写的,我也没那么多的时间去一一验证和调试。
dlut_string.h
/*
* dlut_string.h
*
* Created on: 2013年12月18日
* Author: DLUTBruceZhang
*/
#ifndef DLUT_STRING_H_
#define DLUT_STRING_H_
#include <malloc.h>
#include <stdio.h>
#define dlut_strcmpi(buf1, buf2) dlut_stricmp(buf1, buf2)
void * dlut_memcpy(void *, const void *, unsigned int);
int dlut_memcmp(const void *, const void *, unsigned int);
void * dlut_memset(void *, int, unsigned int);
char * dlut_strset(char *, int);
char * dlut_strcpy(char *, const char *);
char * dlut_strcat(char *, const char *);
int dlut_strcmp(const char *, const char *);
unsigned int dlut_strlen(const char *);
void * dlut_memccpy(void *, const void *, int, unsigned int);
void * dlut_memchr(void *, int, unsigned int);
int dlut_memicmp(const void *, const void *, unsigned int);
void * dlut_memmove(void *, const void *, unsigned int);
char * dlut_strchr(const char *, int);
int dlut_stricmp(const char *, const char *);
int dlut_strcoll(const char *, const char *);
int dlut_stricoll(const char *, const char *);
int dlut_strncoll(const char *, const char *, unsigned int);
int dlut_strnicoll(const char *, const char *, unsigned int);
unsigned int dlut_strcspn(const char *, const char *);
char * dlut_strdup(const char *);
char * dlut_strerror(const char *);
char * dlut_strlwr(char *);
char * dlut_strncat(char *, const char *, unsigned int);
int dlut_strncmp(const char *, const char *, unsigned int);
int dlut_strnicmp(const char *, const char *, unsigned int);
char * dlut_strncpy(char *, const char *, unsigned int);
char * dlut_strnset(char *, int , unsigned int);
char * dlut_strpbrk(const char *, const char *);
char * dlut_strrchr(const char *, int);
char * dlut_strrev(char *);
unsigned int dlut_strspn(const char *, const char *);
char * dlut_strstr(const char *, const char *);
char * dlut_strtok(char *, const char *);
char * dlut_strupr(char *);
int dlut_bcmp(const void *, const void *, unsigned int);
void dlut_bcopy(const void *, void *, unsigned int);
void dlut_bzero(void *, unsigned int);
#endif /* DLUT_STRING_H_ */
dlut_string.c
/*
* dlut_string.c
*
* Created on: 2013年12月18日
* Author: DLUTBruceZhang
*/
#include "dlut_string.h"
void * dlut_memcpy(void *dst, const void *src, unsigned int size)
{
void *dst_ = dst;
if (src == 0 || dst == 0 || !size)
return dst_;
while (size-- && *(char *)src)
{
*(char *)dst++ = *(char *)src++;
}
return dst_;
}
int dlut_memcmp(const void *buf1, const void *buf2, unsigned int size)
{
char *buf1_ = (char *)buf1;
char *buf2_ = (char *)buf2;
if (!size)
return 0;
while (size-- && *buf1_ == *buf2_)
{
buf1_++;
buf2_++;
}
return *buf1_ - *buf2_;
}
void * dlut_memset(void *buf, int v, unsigned int size)
{
void *buf_ = buf;
if (!size)
return buf_;
while (size--)
{
*(char *)buf = (char)v;
buf = (char *)buf + 1;
}
return buf_;
}
char * dlut_strset(char *buf, int v)
{
char *start = buf;
while (*buf)
{
*buf++ = (char)v;
}
return start;
}
char * dlut_strcpy(char *dst, const char *src)
{
char *start = dst;
while (*src)
{
*dst++ = *src++;
}
return start;
}
char * dlut_strcat(char *buf, const char *sub)
{
char *start = buf;
while (*buf)
buf++;
while (*sub)
{
*buf++ = *sub++;
}
return start;
}
int dlut_strcmp(const char *buf1, const char *buf2)
{
while (*buf1 == *buf2)
{
buf1++;
buf2++;
}
return *buf1 - *buf2;
}
unsigned int dlut_strlen(const char *buf)
{
int i = 0;
while (*buf)
{
buf++;
i++;
}
return i;
}
void * dlut_memccpy(void *dst, const void *src, int v, unsigned int size)
{
while (size-- && (int)*(char *)src != v)
{
*(char *)dst++ = *(char *)src++;
}
if (*(char *)src == v)
return (void *)++src;
else
return 0;
}
void * dlut_memchr(void *str, int v, unsigned int size)
{
while (size-- && (int)*(char *)str != v)
str++;
return (int)*(char *)str == v ? str : 0;
}
#define DLUT_TO_LOWER(c) (c >= 'A' && c <= 'Z') ? (c + 'a' - 'A') : c
int dlut_memicmp(const void *buf1, const void *buf2, unsigned int size)
{
char buf1_;
char buf2_;
while (size-- &&
((buf1_ = DLUT_TO_LOWER(*(char *)buf1)) == (buf2_ = DLUT_TO_LOWER(*(char *)buf2))))
{
buf1 = (char *)buf1 + 1;
buf2 = (char *)buf2 + 1;
}
return (int)*(char *)buf1 - (int)*(char *)buf2;
}
/*
* this function is similar to dlut_memcpy, but you need know,
* dlut_memmove support memory overlapping, its method is here:
* 1.if src address is equal dst address, dont't do any thing
* 2.if src address is less than dst address, do reverse copy
* 3.if src address is greater than dst address, do forward copy
*
* */
void * dlut_memmove(void *dst, const void *src, unsigned int size)
{
void *dst_ = dst;
if (src > dst)
{
while (size--)
{
*(char *)dst++ = *(char *)src++;
}
}
else if (src < dst)
{
src = (char *)src + size - 1;
dst = (char *)dst + size - 1;
while (size--)
{
*(char *)dst-- = *(char *)src--;
}
}
return dst_;
}
char * dlut_strchr(const char *str, int v)
{
while (*str && (int)*str != v)
str++;
return (char *)str;
}
/*you need know '\0' and '\0' is equal too*/
int dlut_stricmp(const char *buf1, const char *buf2)
{
while ((DLUT_TO_LOWER(*buf1)) == (DLUT_TO_LOWER(*buf2))
&& *buf1 && *buf2)
{
buf1++;
buf2++;
}
return (int)(*buf1 - *buf2);
}
int dlut_strcoll(const char *buf1, const char *buf2)
{
while (*buf1 == *buf2)
{
buf1++;
buf2++;
}
return *buf1 - *buf2;
}
int dlut_stricoll(const char *buf1, const char *buf2)
{
while ((DLUT_TO_LOWER(*buf1)) == (DLUT_TO_LOWER(*buf2))
&& *buf1 && *buf2)
{
buf1++;
buf2++;
}
return (int)(*buf1 - *buf2);
}
int dlut_strncoll(const char *buf1, const char *buf2, unsigned int size)
{
while (size-- && *buf1 == *buf2 && *buf1 && *buf2)
{
buf1++;
buf2++;
}
return *buf1 - *buf2;
}
int dlut_strnicoll(const char *buf1, const char *buf2, unsigned int size)
{
while (size-- && *buf1 && *buf2 && ((DLUT_TO_LOWER(*buf1)) == (DLUT_TO_LOWER(*buf2))))
{
buf1++;
buf2++;
}
return *buf1 - *buf2;
}
unsigned int dlut_strcspn(const char *buf1, const char *buf2)
{
const char *start;
int count = 0;
while (*buf2)
{
start = buf1;
count = 0;
while (*start)
{
if (*start == *buf2)
return count;
else
{
start++;
count++;
}
}
buf2++;
}
return count;
}
char * dlut_strdup(const char *str)
{
char *dup;
dup = (char *)malloc(dlut_strlen(str));
dlut_strcpy(dup, str);
return dup;
}
char * dlut_strerror(const char *str)
{
return 0;
}
char * dlut_strlwr(char *str)
{
char *start = str;
while (*str)
{
if ((*str >= 'A') && (*str <= 'Z'))
{
*str = *str + 'a' - 'A';
}
str++;
}
return start;
}
char * dlut_strncat(char *dst, const char *src, unsigned int size)
{
char *start = dst;
while (*dst)
dst++;
while (size-- && *dst)
{
*dst++ = *src++;
}
*dst = '\0';
return start;
}
int dlut_strncmp(const char *buf1, const char *buf2, unsigned int size)
{
while (--size && *buf1 && *buf2 && *buf1 == *buf2)
{
buf1++;
buf2++;
}
return *buf1 - *buf2;
}
int dlut_strnicmp(const char *buf1, const char *buf2, unsigned int size)
{
while (--size && *buf1 && *buf2 && ((DLUT_TO_LOWER(*buf1)) == (DLUT_TO_LOWER(*buf2))))
{
buf1++;
buf2++;
}
if (((DLUT_TO_LOWER(*buf1)) == (DLUT_TO_LOWER(*buf2))))
return 0;
else
return *buf1 - *buf2;
}
char * dlut_strncpy(char *buf1, const char *buf2, unsigned int size)
{
char *start = buf1;
while (size-- && *buf2)
{
*buf1++ = *buf2++;
}
*buf1 = '\0';
return start;
}
char * dlut_strnset(char *str, int v, unsigned int size)
{
char *start = str;
while(size-- && *str)
{
*str++ = (char)v;
}
return start;
}
char * dlut_strpbrk(const char *buf1, const char *buf2)
{
char *start;
while (*buf2)
{
start = (char *)buf1;
while (*start)
{
if (*start == *buf2)
{
return start;
}
start++;
}
buf2++;
}
return start;
}
char * dlut_strrchr(const char *str, int v)
{
const char *high = str;
while (*high)
high++;
--high;
while (high != str - 1)
{
if (*high == (char)v)
return (char *)high;
else
high--;
}
return 0;
}
char * dlut_strrev(char *str)
{
char *start = str;
char *low = str;
char *high = str;
char c;
while (*high)
high++;
--high;
while (low < high)
{
c = *low;
*low++ = *high;
*high-- = c;
}
return start;
}
/*
* map有32个字节。就是256个BIT,可把map看成是一个2维数组[32][8],
每个ASCII码(设为c)有8BIT,把它分为2部分,低3位构成下标j(通过c & 7得到),高5位构成下标i(通过c>>3得到),
这样在map[[i][j]中置1代表该字符存在。
算法中先根据control构成map,然后把string中每个字符用同样方法计算出i和j,如果map[i][j]为1,则count加1 。
* */
unsigned int dlut_strspn(const char *buf1, const char *buf2)
{
unsigned int count = 0;
const char *buf1_ = buf1;
const char *buf2_ = buf2;
char map[32];
for (count = 0; count != 32; count++)
map[count] = 0;
while (*buf2_)
{
map[*buf2_ >> 3] |= (1 << (*buf2_ & 7));
buf2_++;
}
if (*buf1)
{
count = 0;
while ((map[*buf1_ >> 3] & (1 << (*buf1_ & 7))) && *buf1_)
{
count++;
buf1_++;
}
return count;
}
return 0;
}
char * dlut_strstr(const char *buf1, const char *buf2)
{
char *buf1_;
char *buf2_;
char *start;
while(*buf1)
{
start = (char *)buf1;
buf1_ = (char *)buf1;
for (buf2_ = (char *)buf2; *buf2_; buf2_++)
{
if (*buf1_ == *buf2_)
{
if (*(buf2_ + 1) == '\0')
{
return start;
}
else
{
buf1_++;
}
}
else
break;
}
buf1++;
}
return 0;
}
char * dlut_strtok(char *str, const char *delim)
{
static char *reserved;
char *start;
if (str == 0)
str = reserved;
if (str == 0)
return 0;
start = str;
while (*str)
{
if (*delim && *delim == *str)
{
*str = '\0';
reserved = str + 1;
return start;
}
str++;
}
reserved = 0;
return start;
}
char * dlut_strupr(char *str)
{
char *start = str;
if (str == 0)
return 0;
while (*str)
{
if (*str >= 'a' && *str <= 'z')
{
*str = *str - 'a' + 'A';
}
str++;
}
return start;
}
int dlut_bcmp(const void *buf1, const void *buf2, unsigned int size)
{
char *buf1_ = (char *)buf1;
char *buf2_ = (char *)buf2;
while (size-- && *buf1_ == *buf2_)
{
buf1_++;
buf2_++;
}
return *buf1_ - *buf2_;
}
void dlut_bcopy(const void *src, void *dst, unsigned int size)
{
char *src_ = (char *)src;
char *dst_ = (char *)dst;
while (size--)
{
*dst_++ = *src_++;
}
return;
}
void dlut_bzero(void *str, unsigned int size)
{
char *str_ = (char *)str;
while (size--)
{
*str_++ = 0;
}
return;
}