memstr

/*********************** internal functions *********************/ /* search for a string in a fixed-length byte string. if partial is true, partial matches are allowed at the end of the buffer. returns NULL if not found, or a pointer to the start of the first match. */ void* my_memstr(char* haystack, int haystacklen, const char* needle, int partial) { int needlen = strlen(needle); int len = haystacklen; char *ptr = haystack; /* iterate through first character matches */ while( (ptr = memchr(ptr, needle[0], len)) ) { /* calculate length after match */ len = haystacklen - (ptr - (char *)haystack); /* done if matches up to capacity of buffer */ if(memcmp(needle, ptr, needlen < len ? needlen : len) == 0 && (partial || len >= needlen)) break; /* next character */ ptr++; len--; } return ptr; }
原文地址:https://www.cnblogs.com/lexus/p/3001106.html