Zend/zend_operators.h:
static inline char * zend_memnstr(char *haystack, char *needle, int needle_len, char *end)
{
//字符首指針
char *p = haystack;
//最後一個字符
char ne = needle[needle_len-1];
//減小查詢範圍,判斷needle_len應該小於end還算比較巧妙哦
end -= needle_len;
while (p <= end) {
//在數組的前n個字節中搜索字符 memchr(p, *needle, (end-p+1))
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++;
}
return NULL;
}
php函數strpos、explode都用到了此函數,具體代碼可以查看:ext/standard/string.c