php内部函数

strpos函数

 1 /**
 2 haystack:被比较字串首地址(指向被比较字符串)
 3 needle:源字串首地址(指向源字符串)
 4 needle_len:源字符串长度
 5 end:指向最后一个字符地址的下一个内存地址
 6 **/
 7 static inline char *
 8 zend_memnstr(char *haystack, char *needle, int needle_len, char *end)
 9 {
10     char *p = haystack;               //被比较字符串首地址
11     char ne = needle[needle_len-1];    //源字符串的最后一个字符
12 
13     if (needle_len == 1) {
14         return (char *)memchr(p, *needle, (end-p));   //返回被比较字符串第一次出现的地址
15     }
16 
17     if (needle_len > end-haystack) {    //源字符串超过被比较字符串的长度
18         return NULL;
19     }
20 
21     end -= needle_len;   //end = end - needle_len  //最后比较的位置
22 
23     while (p <= end) {
24         if ((p = (char *)memchr(p, *needle, (end-p+1))) && ne == p[needle_len-1]) {   //比较首尾字符是否相等
25             if (!memcmp(needle, p, needle_len-1)) {     //用来比较needle 和p 所指的内存区间前needle_len-1个字符
26                 return p;
27             }
28         }
29 
30         if (p == NULL) {
31             return NULL;
32         }
33 
34         p++;
35     }
36 
37     return NULL;
38 }
View Code
原文地址:https://www.cnblogs.com/heyijing/p/5404151.html