#define php_memnstr zend_memnstr
//在haystack中查找needle,如果不存在返回null,如果存在,返回指向haystack中needle头字符的指针
//needle_len needle指向的字符串长度
//end haystack指向的字符串的结尾,长度为 end - haystack
static inline char *zend_memnstr(char *haystack, char *needle, int needle_len, char *end)
{
char *p = haystack;//字符串首地址
char ne = needle[needle_len-1];//needle尾字符
if (needle_len == 1) {//如果needle中只有1个字符,调用c中memchr查找
return (char *)memchr(p, *needle, (end-p));
}
if (needle_len > end-haystack) {
return NULL;
}
end -= needle_len;//查找头字符memchr时,最长end - needle_len
while (p <= end) {
//在p中前end_p+1个字节查找*needle的头字符,如果找到,并且尾字符一致时
if ((p = (char *)memchr(p, *needle, (end-p+1))) && ne == p[needle_len-1]) {
if (!memcmp(needle, p, needle_len-1)) {//中间字符一致
return p;
}
}
if (p == NULL) {
return NULL;
}
p++;//中间字符不一致,p++
}
return NULL;
}