php strncmp 源码分析

strncmp

函数原型

php strncmp 源码分析_第1张图片

源码分析 版本PHP 5.6.40

1、Zend/zend_builtin_functions.c (内置函数)

ZEND_FUNCTION(strncmp)
{
    char *s1, *s2;
    int s1_len, s2_len;
    long len;

    if (zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC, "ssl", &s1, &s1_len, &s2, &s2_len, &len) == FAILURE) {
        return;
    }

    if (len < 0) {
        zend_error(E_WARNING, "Length must be greater than or equal to 0");
        RETURN_FALSE;
    }

    RETURN_LONG(zend_binary_strncmp(s1, s1_len, s2, s2_len, len));
}

2、Zend/zend_operators.c

ZEND_API int zend_binary_strncmp(const char *s1, uint len1, const char *s2, uint len2, uint length) /* {{{ */
{
    int retval;

    if (s1 == s2) {
        return 0;
    }
    retval = memcmp(s1, s2, MIN(length, MIN(len1, len2)));
    if (!retval) {
        return (MIN(length, len1) - MIN(length, len2));
    } else {
        return retval;
    }
}

3、memcmp 用法

php strncmp 源码分析_第2张图片

你可能感兴趣的:(php源码)