from http://www./code/view/1432476194146 2015.05 字節(jié)拷貝函數(shù):
void * memcpy1(void *dest, const void *src, size_t n) { assert(dest!=NULL&&src!=NULL); //有效性檢測 if(dest == src) return dest; else if(dest < src) { const char *c_src = static_cast<const char *>(src); char *c_dest = static_cast<char *>(dest); while (n--) *c_dest++ = *c_src++; } else { const char *c_src = static_cast<const char *>(src) + n - 1; char *c_dest = static_cast<char *>(dest) + n - 1; while (n--) *c_dest-- = *c_src--; } return dest; }字符串比較函數(shù): char *strcpy(char *des, const char *src) { assert(des != NULL && src != NULL); char *pStr = des; while( (*des++ = *src++) != '\0') NULL; return pStr; }字符串比較函數(shù): int strcmp(const char *des, const char *src) { assert(des != NULL && src != NULL); for (;*des && *src && *des == *src; des++, src++) NULL; return *des - *src; }字符串長度函數(shù): int strlen(const char *pStr) { assert(pStr != NULL); int len = 0; while (*pStr++ != '\0') len++; return len; }字符串連接函數(shù): char *strcat(char *des, const char *src) { assert(*des != NULL && *src != NULL); char *pStr = des; while (*des != '\0') des++; while ((*des++ = *src++) != '\0') NULL; return pStr; }字符串查找函數(shù): char *myStrstr(const char *des, const char *src) { assert(des != NULL && src != NULL); char *d = des; char *s = src; for (; d != '\0'; des++) { for (d = des, s = src; (s != '\0') && (*d == *s); d++, s++) NULL; if (*s == '\0') return des; } return NULL; } |
|