php strpos 字符串查找函數內部源碼實現

此函數查找字符串從前面開始查找。 php

此函數對應的c函數的代碼爲:ext/standard/string.c html

/* {{{ proto int strpos(string haystack, string needle [, int offset]) 函數

   Finds position of first occurrence of a string within another */
PHP_FUNCTION(strpos)
{
    zval *needle;
    char *haystack;
    char *found = NULL;
    char  needle_char[2];
    long  offset = 0;

    int   haystack_len; spa

    /* " sz|l" 表示一個字符串(若是是一個字符串的話,根據zend_parse_parameters的用法,那麼haystack後面的第二個參數就是個int類型,也就是haystack的長度haystack_len),一個zval,再加一個可選的long型,所以用"sz|l"來表示。 若是想實現可選參數的話,例如一個字符串,一個浮點,再加一個可選的bool型,能夠用"sd|b"來表示。*/
    if (zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC, "sz|l", &haystack, &haystack_len, &needle, &offset) == FAILURE) {
        return;
    }

    if (offset < 0 || offset > haystack_len) {
        php_error_docref(NULL TSRMLS_CC, E_WARNING, "Offset not contained in string");
        RETURN_FALSE;
    }

    if (Z_TYPE_P(needle) == IS_STRING) {
        if (!Z_STRLEN_P(needle)) {
            php_error_docref(NULL TSRMLS_CC, E_WARNING, "Empty needle");
            RETURN_FALSE;
        }

        found = php_memnstr(haystack + offset,
                            Z_STRVAL_P(needle),
                            Z_STRLEN_P(needle),
                            haystack + haystack_len);
    } else {
        if (php_needle_char(needle, needle_char TSRMLS_CC) != SUCCESS) {
            RETURN_FALSE;
        }
        needle_char[1] = 0;

        found = php_memnstr(haystack + offset,
                            needle_char,
                            1,
                            haystack + haystack_len);
    }

    if (found) {
        RETURN_LONG(found - haystack);
    } else {
        RETURN_FALSE;
    }
}

/* }}} */ 指針

其中紅色函數:php_memnstr則是在main/php.h定義的:#define php_memnstr zend_memnstr,那麼就須要找zend_memnstr的位置,經過搜索此函數的位置在:和ext通緝的Zend/zend_operators.h裏面實現的。 htm

//此函數的做用是在haystack中查找needle,若是不存在返回null,若是存在,返回指向haystack中needle頭字符的指針
static inline char *zend_memnstr(char *haystack, char *needle, int needle_len, char *end)
{
        //p表明haystack字符串首地址位置
    char *p = haystack;
    //needle尾字符
    char ne = needle[needle_len-1];
    
         //若是needle中只有1個字符,調用c中memchr查找
    if (needle_len == 1) {
        return (char *)memchr(p, *needle, (end-p));
    }
 
    if (needle_len > end-haystack) {
        return NULL;
    }
    
         //查找頭字符memchr時,最長end - needle_len
    end -= needle_len;
 
    while (p <= end) {
        //在p中前end-p+1(即haystack_len+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;
} 內存

/* 字符串

   Searches at bufferfor the given character, stopping when characteris first found or cnt bytes have been searched through. get

   從buffer所指內存區域的前count個字節查找字符ch,當第一次遇到字符ch時中止查找。若是成功,返回指向字符ch的指針;不然返回NULL。 原型

*/

 

void * my_memchr(const void * buffer,int ch,int count)

{

   while ( count && (*(unsigned char *)buffer != (unsigned char)ch) )

   {

      buffer = (unsigned char *)buffer + 1;

        count--;

   }

   return(count ? (void *)buffer : NULL);

}

/************************************************************************/
/* 字符串函數memcmp
   原型:extern int memcmp(void *buf1, void *buf2, unsigned int count);
   功能:比較內存區域buf1和buf2的前count個字節
   說明:當buf1<buf2時,返回值<0  
        當buf1=buf2時,返回值=0   
      當buf1>buf2時,返回值>0                                        */

源程序:

int memcmp(const void * cs,const void * ct,size_t count)
{
const unsigned char *su1, *su2;
int res = 0;

for( su1 = cs, su2 = ct; 0 < count; ++su1, ++su2, count--)
   if ((res = *su1 - *su2) != 0)
    break;
return res;
}

/************************************************************************/

附上函數zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC, "ss", &r1, &n, &r2, &m)

其中ss的含義在如下類型表格中說明,

類型說明見下表:
Boolean b zend_bool
Long l long
Double d double
String s char*, int
Resource r zval*
Array a zval*
Object o zval*
zval z zval*

因而可知若是類型爲string的話,那麼須要一個字符串的指針和一個int值。

另註釋:轉載自五四陳科學院[http://www.54chen.com]中TSRMLS_CC的含義,請參考如下文章:http://bbs.phpchina.com/thread-185497-1-1.html;

相關文章
相關標籤/搜索