头文件
#include <stdio.h> //copying void * m_memcpy ( void * destination, const void * source, size_t num ); void * m_memmove ( void * destination, const void * source, size_t num ); void * m_memccpy ( void * destination, const void * source, int c, size_t num ); //not standard char * m_strcpy ( char * destination, const char * source ); char * m_strncpy ( char * destination, const char * source, size_t num ); //Concatenation char * m_strcat ( char * destination, const char * source ); char * m_strncat ( char * destination, char * source, size_t num ); //Comparison int m_memcmp ( const void * ptr1, const void * ptr2, size_t num ); int m_strcmp ( const char * str1, const char * str2 ); int m_strcoll ( const char * str1, const char * str2 ); //similar to m_strcmp, not implemented int m_strncmp ( const char * str1, const char * str2, size_t num ); size_t m_strxfrm ( char * destination, const char * source, size_t num ); //not implemented //Searching void * m_memchr ( void * ptr, int value, size_t num ); char * m_strchr ( char * str, int character ); size_t m_strcspn ( const char * str1, const char * str2 ); char * m_strpbrk ( char * str1, const char * str2 ); char * m_strrchr ( char * str, int character ); size_t m_strspn ( const char * str1, const char * str2 ); char * m_strstr ( char * str1, const char * str2 ); char * m_strtok ( char * str, const char * delimiters ); //Others void * m_memset ( void * ptr, int value, size_t num ); size_t m_strlen ( const char * str ); char * strerror ( int errnum ); //not implemented
实现函数
#include <stdio.h> #include <assert.h> #include <string.h> #include "m_string.h" #define MAX_LEN 1000 /* * memcpy()、 memmove()和memccpy() * 这三个函数的功能均是将某个内存块复制到另一个内存块。 * memmove()与memcpy()一样都是用来拷贝src所指的内存内容前n个字节到dest所指的地址上。 * 不同的是,当src和dest所指的内存区域重叠时,memmove()仍然可以正确的处理,不过执行效率上会比使用memcpy()略慢些。 * 第三个函数的功能也是复制内存,但是如果遇到某个特定值时立即停止复制。 */ void * m_memmove ( void * destination, const void * source, size_t num ) { assert(NULL!=destination && NULL!=source); char *des = (char *)destination; const char *src = (const char *)source; if(des < src) { while(num--) { *des++ = *src++; } } else { des += num; src += num; while(num--) { //*des-- = *src--; *--des = *--src; } } return destination; } /* * 功能:由src所指内存区域复制不多于count个字节到dest所指内存区域,如果遇到字符c则停止复制。 * 返回值:如果c没有被复制,则返回NULL,否则,返回一个指向紧接着dest区域后的字符的指针。 */ void * m_memccpy ( void * destination, const void * source, int c, size_t num ) { assert(NULL!=destination && NULL!=source); char *des = (char *)destination; const char *src = (const char *)source; while(num--) { if(*src == (char)c) { return des+1; } *des++ = *src++; } return NULL; } // char * m_strcpy ( char * destination, const char * source ) { assert(NULL!=destination && NULL!=source); char *des = destination; while(*source) { *des++ = *source++; } *des = 0; return destination; } // char * m_strncpy ( char * destination, const char * source, size_t num ) { assert(NULL!=destination && NULL!=source); char *des = destination; while(num--) { *des++ = *source; if(*source) { ++source; } } return destination; } // char * m_strcat ( char * destination, const char * source ) { assert(NULL!=destination && NULL!=source); char *des = destination; while(*des) ++des; while(*source) { *des++ = *source++; } *des = 0; return destination; } // char * m_strncat ( char * destination, char * source, size_t num ) { assert(NULL!=destination && NULL!=source); char *des = destination; while(*des) ++des; while(num--) { *des++ = *source; if(*source) { ++source; } else { break; } } *des = 0; return destination; } // int m_memcmp ( const void * ptr1, const void * ptr2, size_t num ) { assert(NULL!=ptr1 && NULL!=ptr2); const char *p1 = (const char *)ptr1; const char *p2 = (const char *)ptr2; int res = 0; while(num-- && !res) { res = *p1++ - *p2++; } return res; } /* * This function starts comparing the first character of each string. * If they are equal to each other, it continues with the following pairs * until the characters differ or until a terminanting null-character is reached. */ int m_strcmp ( const char * str1, const char * str2 ) { assert(NULL!=str1 && NULL!=str2); while (*str1 && *str2 && *str1 == *str2) { ++ str1; ++ str2; } return (*str1 - *str2); //ingenious, '\0' is also compared } // int m_strncmp ( const char * str1, const char * str2, size_t num ) { assert(NULL!=str1 && NULL!=str2); int res = 0; while (num-- && !res) { res = *str1 - *str2; if(!*str1 || !*str2) { return res; } ++ str1; ++ str2; } return res; } void * m_memchr ( void * ptr, int value, size_t num ) { assert(NULL != ptr); char *pos = (char *)ptr; while(num--) { if(*pos == (char)value) { return (void *)pos; } ++ pos; } return NULL; } // char * m_strchr ( char * str, int character ) { assert(NULL != str); while(*str) { if(*str == (char)character) { return str; } ++ str; } return (*str == (char)character) ? str : NULL; } // size_t m_strcspn ( const char * str1, const char * str2 ) { assert(NULL!=str1 && NULL!=str2); const char *pos = str1; const char *keys; while(*pos) { keys = str2; while(*keys) { if(*pos == *keys++) { return pos - str1; } } ++ pos; } return pos - str1; } // char * m_strpbrk ( char * str1, const char * str2 ) { assert(NULL!=str1 && NULL!=str2); char *pos = str1; const char *keys; while(*pos) { keys = str2; while(*keys) { if(*pos == *keys++) { return pos; } } ++ pos; } return NULL; } // char * m_strrchr ( char * str, int character ) { assert(NULL != str); char *pos = str; while(*pos) ++pos; -- pos; while(pos >= str) { if(*pos == (char)character) { return pos; } -- pos; } return NULL; } // size_t m_strspn ( const char * str1, const char * str2 ) { assert(NULL!=str1 && NULL!=str2); const char *pos = str1; const char *keys; while(*str1) { keys = str2; while(*keys) { if(*pos == *keys) { break; } ++ keys; } if(*keys == '\0') break; ++ pos; } return pos-str1; } // char * m_strstr ( char * str1, const char * str2 ) { assert(NULL!=str1 && NULL!=str2); size_t len2 = m_strlen(str2); for(; *str1; ++str1) { if(!m_strncmp(str1, str2, len2)) { return str1; } } return 0; } // char * m_strtok ( char * str, const char * delimiters ) { assert(NULL!=str && NULL!=delimiters); static char *start; char *end; if(str) { start = str; } while(*start) { if(m_IsDelimiter(*start, delimiters)) { ++ start; } else { break; } } if(*start) { end = start + 1; while(*end) { if(m_IsDelimiter(*end, delimiters)) { *end = '\0'; char *pos = start; start = end + 1; return pos; } ++ end; } if('\0' == *end) { char *pos = start; start = end; return pos; } } return 0; } int m_IsDelimiter(const char c, const char * delimiters) { assert(NULL != delimiters); while(*delimiters) { if(c == *delimiters++) { return 1; } } return 0; } // void * m_memset ( void * ptr, int value, size_t num ) { assert(NULL != ptr); char *p = (char *)ptr; while(num--) { *p++ = (char)value; } return ptr; } // size_t m_strlen ( const char * str ) { assert(NULL != str); const char *pos = str; while(*pos) ++pos; return pos - str; }