zoukankan      html  css  js  c++  java
  • in_array和array_search源码分析

    /* in_array和array_search进入的函数是一样的,调用php_search_array传的behavior来标识 */
    /* {{{ Checks if the given value exists in the array */
    PHP_FUNCTION(in_array)
    {
        php_search_array(INTERNAL_FUNCTION_PARAM_PASSTHRU, 0);
    }
    /* }}} */
    
    /* {{{ Searches the array for a given value and returns the corresponding key if successful */
    PHP_FUNCTION(array_search)
    {
        php_search_array(INTERNAL_FUNCTION_PARAM_PASSTHRU, 1);
    }
    /* }}} */
    /* void php_search_array(INTERNAL_FUNCTION_PARAMETERS, int behavior)
     * 0 = return boolean
     * 1 = return key
     */
    static inline void php_search_array(INTERNAL_FUNCTION_PARAMETERS, int behavior) /* {{{ */
    {
      /* 首先三个参数 */ zval
    *value, /* value to check for */ *array, /* array to check in */ *entry; /* pointer to array entry */ zend_ulong num_idx; zend_string *str_idx; zend_bool strict = 0; /* strict comparison or not */
      /* 参数检验 */ ZEND_PARSE_PARAMETERS_START(
    2, 3) Z_PARAM_ZVAL(value) Z_PARAM_ARRAY(array) Z_PARAM_OPTIONAL Z_PARAM_BOOL(strict) ZEND_PARSE_PARAMETERS_END(); if (strict) { if (Z_TYPE_P(value) == IS_LONG) {
          /* 遍历hastTable */ ZEND_HASH_FOREACH_KEY_VAL_IND(Z_ARRVAL_P(array), num_idx, str_idx, entry) { ZVAL_DEREF(entry);
    if (Z_TYPE_P(entry) == IS_LONG && Z_LVAL_P(entry) == Z_LVAL_P(value)) { if (behavior == 0) { RETURN_TRUE; } else { if (str_idx) { RETVAL_STR_COPY(str_idx); } else { RETVAL_LONG(num_idx); } return; } } } ZEND_HASH_FOREACH_END(); } else { ZEND_HASH_FOREACH_KEY_VAL_IND(Z_ARRVAL_P(array), num_idx, str_idx, entry) { ZVAL_DEREF(entry); if (fast_is_identical_function(value, entry)) { if (behavior == 0) { RETURN_TRUE; } else { if (str_idx) { RETVAL_STR_COPY(str_idx); } else { RETVAL_LONG(num_idx); } return; } } } ZEND_HASH_FOREACH_END(); } } else { if (Z_TYPE_P(value) == IS_LONG) { ZEND_HASH_FOREACH_KEY_VAL_IND(Z_ARRVAL_P(array), num_idx, str_idx, entry) { if (fast_equal_check_long(value, entry)) { if (behavior == 0) { RETURN_TRUE; } else { if (str_idx) { RETVAL_STR_COPY(str_idx); } else { RETVAL_LONG(num_idx); } return; } } } ZEND_HASH_FOREACH_END(); } else if (Z_TYPE_P(value) == IS_STRING) { ZEND_HASH_FOREACH_KEY_VAL_IND(Z_ARRVAL_P(array), num_idx, str_idx, entry) { if (fast_equal_check_string(value, entry)) { if (behavior == 0) { RETURN_TRUE; } else { if (str_idx) { RETVAL_STR_COPY(str_idx); } else { RETVAL_LONG(num_idx); } return; } } } ZEND_HASH_FOREACH_END(); } else { ZEND_HASH_FOREACH_KEY_VAL_IND(Z_ARRVAL_P(array), num_idx, str_idx, entry) { if (fast_equal_check_function(value, entry)) { if (behavior == 0) { RETURN_TRUE; } else { if (str_idx) { RETVAL_STR_COPY(str_idx); } else { RETVAL_LONG(num_idx); } return; } } } ZEND_HASH_FOREACH_END(); } } RETURN_FALSE; } /* }}} */
  • 相关阅读:
    C++如何对接sqlitepp
    c++11中的condition_variable和之前的pthread_cond_timedwait的不同之处
    浏览器设置代理模式后的报文是怎么样的?
    C++11中令人吐血的"移动语义"和"新的右值引用"
    MYSQL的事务及锁操作
    Microsoft Word —— 使用宏脚本将所有表格添加边框
    Navicat——如何导出数据字典
    Redis——配置详解
    keepalived——tengine集群主备均有VIP
    Docker——如何修改运行中容器的映射端口
  • 原文地址:https://www.cnblogs.com/nr-zhang/p/14638077.html
Copyright © 2011-2022 走看看