乐趣区

关于php:php-strncmp-源码分析

strncmp

函数原型

源码剖析 版本 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 用法

退出移动版