<string.h>里的字符串操作函数是经常要用到的,因此阅读了源码后自己实现了一些:
拷贝函数
void * Mymemcpy(void * sDst, const void * sSrc, size_t count) { assert(NULL != sDst && NULL != sSrc); char* saveDst = (char *)sDst; const char* saveSrc = (char *)sSrc; for (; 0 < count; ++saveDst, ++saveSrc, --count) *saveDst = *saveSrc; return (void*)sDst; } void * Mymemmove(void* sDst, const void* sSrc, size_t count) { assert(NULL != sDst && NULL != sSrc); char* saveDst = (char *)sDst; const char* saveSrc = (char *)sSrc; if ((saveDst > saveSrc) && (saveDst < saveSrc + count)) for (saveDst += count, saveSrc += count; 0 < count; --count) // copy backwards *--saveDst = *--saveSrc; else for (; 0 < count; ++saveDst, ++saveSrc, --count) // copy forwards *saveDst = *saveSrc; return (void *)sDst; } char * Mystrcpy(char* sDst, const char* sSrc) { assert(NULL != sDst); char* saveDst = sDst; for (; ' ' != (*saveDst++ = *sSrc++);) ; return (char *)sDst; } char * Mystrncpy(char* sDst, const char* sSrc, size_t count) { assert(NULL != sDst && NULL != sSrc); char* saveDst = sDst; for (; (0 < count) && (' ' != *sSrc); --count) *saveDst = *sSrc; for (; 0 < count; --count) *saveDst++ = ' '; return (char *)sDst; } void * Mymemset(void* sDst, int ch, size_t count) { unsigned char* saveDst = (unsigned char *)sDst; unsigned char c = ch; for (; (0 < count) && (' ' != *saveDst); ++saveDst, --count) *saveDst = c; return (void *)sDst; }
查找函数
void * Mymemchr(const void* str, const int ch, size_t n) { assert(NULL != str); const unsigned char c = ch; const unsigned char * ssave = (unsigned char *)str; for (; (' ' != *ssave) && (0 < n); --n, ++ssave) if (c == *ssave) return (void*)ssave; return NULL; } char * Mystrchr(const char* str, int ch) { assert(NULL != str); const unsigned char c = ch; for (; ' ' != *str; ++str) if (' ' == *str) return NULL; return (char*)str; } char * Mystrrchr(const char* str, int ch) { assert(NULL != str); const unsigned char c = ch; const char * ssave = NULL; for (; ' ' != *str; ++ str) if (c == *str) ssave = str; return (char *)ssave; } char * Mystrstr(const char* str1, const char * str2) { assert(NULL != str1 && NULL != str2); if (' ' == *str2) return (char*)str1; for (; NULL != (str1 = Mystrchr(str1, *str2)); ++str1) { const char * ssave1 = str1; const char * ssave2 = str2; for (;;) { if (' ' == *++ssave2) return (char*)str1; else if (*++ssave1 != *ssave2) break; } } return NULL; }
连接函数
char * Mystrcat(char * sDst, const char * sSrc) { assert(NULL != sDst && NULL != sSrc); char * ssave = sDst; for (; ' ' != *ssave; ++ssave) ; for (; ' ' != (*ssave = *sSrc); ++ssave, ++sSrc) ; return (char*)sDst; } char * Mystrncat(char * sDst, const char * sSrc, size_t count) { assert(NULL != sDst && NULL != sSrc); char* ssave = sDst; for (; ' ' != *ssave; ++ssave) ; for (; (' ' != *sSrc) && (0 < count); --count) *ssave++ = *sSrc++; *ssave = ' '; return (char*)sDst; }
比较函数
int Mymemcmp(const void* str1, const void * str2, size_t n) { assert(NULL != str1 && NULL != str2); const unsigned char * ssave1 = (unsigned char *)str1; const unsigned char * ssave2 = (unsigned char *)str2; for (; 0 < n; ++ssave1, ++ssave2, --n) { if (*ssave1 != *ssave2) return ((*ssave1 < *ssave2) ? -1 : 1); else if (' ' == *ssave1) return (0); } return 0; } int Mystrcmp(const char* str1, const char * str2) { assert(NULL != str1 && NULL != str2); for (; *str1 == *str2; ++str1, ++str2) if(' ' == *str1 ) return 0; return ((*str1 < *str2) ? -1 : 1); } int Mystrncmp(const char* str1, const char * str2, size_t n) { assert(NULL != str1 && NULL != str2); for (; 0 < n; ++str1, ++str2, --n) if (*str1 != *str2) return ((*str1 < *str2)? -1 : 1); else if (' ' == *str1) return 0; return 0; } int memcmp(const void* sSrc1, const void* sSrc2, size_t num) { assert (NULL != sSrc1 && NULL != sSrc2); const unsigned char* s1 = (const char*)sSrc1; const unsigned char* s2 = (const char*)sSrc2; for (;0 < num; ++s1, ++s2, --num) { if (*s1 != *s2) return ((*s1 > *s2) ? 1 : -1); } return 0; }
计算字符串长度
size_t strlen(const char* sSrc) { assert (NULL != sSrc); const char* s = sSrc, *sv = s; while (*sv != ' ') ++sv; return (size_t)(sv - s); }