尝试将Kohana框架转为C扩展-Kohana_Arr类实现主要代码
尝试将Kohana框架转为C扩展-Kohana_Arr类实现主要代码
朱宗鑫1 发表于7年前
尝试将Kohana框架转为C扩展-Kohana_Arr类实现主要代码
  • 发表于 7年前
  • 阅读 230
  • 收藏 1
  • 点赞 0
  • 评论 0

新睿云服务器60天免费使用,快来体验!>>>   

/* {{{ static Kohana_Arr Kohana_Arr::is_assoc([array array])
 */
PHP_METHOD(Kohana_Arr, is_assoc) {
    zval *array = NULL;

    if (ZEND_NUM_ARGS() != 1) {
        WRONG_PARAM_COUNT;
    }

    if (SUCCESS != zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC, "a", &array)) {
        php_error_docref(NULL TSRMLS_CC, E_WARNING, "Argument #%d should be an array", 1);
        RETURN_FALSE;
    }

    if (kohana_arr_is_assoc(array) == SUCCESS) {
        RETURN_TRUE;
    } else {
        RETURN_FALSE;
    }

    //*return_value = *array;
    RETURN_ZVAL(array, 1, 0);
}
/* }}} */

/* {{{ static Kohana_Arr Kohana_Arr::is_array([mixed value])
 */
PHP_METHOD(Kohana_Arr, is_array) {
    zval *value = NULL;

    if (ZEND_NUM_ARGS() != 1) {
        WRONG_PARAM_COUNT;
    }

    if (SUCCESS != zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC, "z", &value)) {
        RETURN_FALSE;
    }

    if (SUCCESS != kohana_arr_is_array(value)) {
        RETURN_FALSE;
    }
    RETURN_TRUE;
}


/* }}} */



PHP_METHOD(Kohana_Arr, path) {
    zval *array = NULL;
    zval *path = NULL;
    zval *defaultvalue = NULL;
    zval *delim = NULL;
    if (ZEND_NUM_ARGS() < 2 || ZEND_NUM_ARGS() > 4) {
        WRONG_PARAM_COUNT;
    }
    if (SUCCESS != zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC, "zz|zz", &array, &path, &defaultvalue, &delim)) {
        RETURN_ZVAL(defaultvalue, 1, 0);
    }

    if (SUCCESS != kohana_arr_is_array(array)) {
        php_error_docref(NULL TSRMLS_CC, E_WARNING, "Argument #%d should be an array", 1);
        RETURN_ZVAL(defaultvalue, 1, 0);
    }
    if (defaultvalue == NULL) {
        MAKE_STD_ZVAL(defaultvalue);
        ZVAL_NULL(defaultvalue);
    }

    if (delim == NULL) {
        MAKE_STD_ZVAL(delim);
        ZVAL_NULL(delim);
    }
    if (Z_TYPE_P(delim) == IS_NULL) {
        Z_STRLEN_P(delim) = sizeof (KOHANA_ARR_DELIMITER) - 1;
        Z_STRVAL_P(delim) = estrdup(KOHANA_ARR_DELIMITER);
    }
    kohana_arr_path(array, path, delim, defaultvalue, return_value TSRMLS_DC);
}

PHP_METHOD(Kohana_Arr, set_path) {
    zval *array = NULL;
    zval *path = NULL;
    zval *value = NULL;
    zval *delim = NULL;
    if (ZEND_NUM_ARGS() < 3 || ZEND_NUM_ARGS() > 4) {
        WRONG_PARAM_COUNT;
    }

    if (SUCCESS != zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC, "zzz|z", &array, &path, &value, &delim)) {
        php_error_docref(NULL TSRMLS_CC, E_WARNING, "Not enough arguments");
        RETURN_FALSE;
    }

    if (SUCCESS != kohana_arr_is_array(array)) {
        php_error_docref(NULL TSRMLS_CC, E_WARNING, "Argument #%d should be an array", 1);
        RETURN_FALSE;
    }

    if (delim == NULL) {
        MAKE_STD_ZVAL(delim);
        ZVAL_NULL(delim);
    }
    if (Z_TYPE_P(delim) == IS_NULL) {
        Z_STRLEN_P(delim) = sizeof (KOHANA_ARR_DELIMITER) - 1;
        Z_STRVAL_P(delim) = estrdup(KOHANA_ARR_DELIMITER);
    }
    kohana_arr_set_path(array, path, value, delim);
    RETURN_TRUE;
}

PHP_METHOD(Kohana_Arr, range) {
    int step = 0;
    int max = 0;
    if (ZEND_NUM_ARGS() > 2) {
        WRONG_PARAM_COUNT;
    }

    if (SUCCESS != zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC, "|ll", &step, &max)) {
        RETURN_FALSE;
    }

    zval *array = NULL;
    MAKE_STD_ZVAL(array);
    array_init(array);
    if (step < 0) {
        RETURN_ZVAL(array, 1, 0);
    }

    int i;
    for (i = step; i <= max; i += step) {
        add_index_long(array, i, i);
    }

    RETURN_ZVAL(array, 1, 0);
}

PHP_METHOD(Kohana_Arr, get) {
    zval *array = NULL;
    zval *key = NULL;
    zval *defualt = NULL;
    if (ZEND_NUM_ARGS() < 2 || ZEND_NUM_ARGS() > 3) {
        WRONG_PARAM_COUNT;
    }

    if (SUCCESS != zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC, "zz|z", &array, &key, &defualt)) {
        php_error_docref(NULL TSRMLS_CC, E_WARNING, "Not enough arguments");
        RETURN_FALSE;
    }

    if (SUCCESS != kohana_arr_is_array(array)) {
        php_error_docref(NULL TSRMLS_CC, E_WARNING, "Argument #%d should be an array", 1);
        RETURN_FALSE;
    }

    if (defualt == NULL) {
        MAKE_STD_ZVAL(defualt);
        ZVAL_NULL(defualt);
    }

    convert_to_string(key);

    ulong idx = 0;
    zval **findvalue;
    if (ctype_digit(key) == SUCCESS) {
        idx = atol(Z_STRVAL_P(key));
        if (zend_hash_index_find(Z_ARRVAL_P(array), idx, (void **) &findvalue) == SUCCESS) {
            RETURN_ZVAL(*findvalue, 1, 0);
        }
    } else {
        if (zend_hash_find(Z_ARRVAL_P(array), Z_STRVAL_P(key), Z_STRLEN_P(key) + 1, (void **) &findvalue) == SUCCESS) {
            RETURN_ZVAL(*findvalue, 1, 0);
        }
    }
    RETURN_ZVAL(defualt, 1, 0);
}



PHP_METHOD(Kohana_Arr, extract) {
    zval *array = NULL;
    zval *keys = NULL;
    zval *defualt = NULL;
    if (ZEND_NUM_ARGS() < 2 || ZEND_NUM_ARGS() > 3) {
        WRONG_PARAM_COUNT;
    }

    if (SUCCESS != zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC, "zz|z", &array, &keys, &defualt)) {
        php_error_docref(NULL TSRMLS_CC, E_WARNING, "Not enough arguments");
        RETURN_FALSE;
    }

    if (SUCCESS != kohana_arr_is_array(array)) {
        php_error_docref(NULL TSRMLS_CC, E_WARNING, "Argument #%d should be an array", 1);
        RETURN_FALSE;
    }

    if (Z_TYPE_P(keys) != IS_ARRAY) {
        php_error_docref(NULL TSRMLS_CC, E_WARNING, "Argument #%d should be an array", 2);
        RETURN_FALSE;
    }

    if (defualt == NULL) {
        MAKE_STD_ZVAL(defualt);
        ZVAL_NULL(defualt);
    }

    zval *found = NULL;
    MAKE_STD_ZVAL(found);
    array_init(found);

    zval **key;
    HashPosition pos;

    zend_hash_internal_pointer_reset_ex(Z_ARRVAL_P(keys), &pos);
    while (zend_hash_get_current_data_ex(Z_ARRVAL_P(keys), (void **) &key, &pos) == SUCCESS) {
        convert_to_string(*key);

        zval **findvalue;
        if (ctype_digit(*key) == SUCCESS) {
            ulong idx = atol(Z_STRVAL_P(*key));
            if (zend_hash_index_find(Z_ARRVAL_P(array), idx, (void **) &findvalue) == SUCCESS) {
                add_index_zval(found, idx, *findvalue);
            } else {
                add_index_zval(found, idx, defualt);
            }
        } else {
            if (zend_hash_find(Z_ARRVAL_P(array), Z_STRVAL_P(*key), Z_STRLEN_P(*key) + 1, (void **) &findvalue) == SUCCESS) {

                //add_assoc_zval(found, key, *findvalue);
                add_assoc_zval_ex(found, Z_STRVAL_P(*key), Z_STRLEN_P(*key) + 1, *findvalue);
            } else {
                //add_assoc_zval(found, key, defualt);
                add_assoc_zval_ex(found, Z_STRVAL_P(*key), Z_STRLEN_P(*key) + 1, defualt);
            }
        }

        zend_hash_move_forward_ex(Z_ARRVAL_P(keys), &pos);

    }
    RETURN_ZVAL(found, 1, 0);
}

Phalcon 的数组助手就用到了这部分代码

  • 打赏
  • 点赞
  • 收藏
  • 分享
共有 人打赏支持
粉丝 25
博文 28
码字总数 4018
作品 1
×
朱宗鑫1
如果觉得我的文章对您有用,请随意打赏。您的支持将鼓励我继续创作!
* 金额(元)
¥1 ¥5 ¥10 ¥20 其他金额
打赏人
留言
* 支付类型
微信扫码支付
打赏金额:
已支付成功
打赏金额: