• php内置函数分析之str_pad()


    PHP_FUNCTION(str_pad)
    {
        /* Input arguments */
        zend_string *input;                /* Input string 输入字符串*/
        zend_long pad_length;            /* Length to pad to 填充到多长.*/
    
        /* Helper variables */
        size_t num_pad_chars;        /* Number of padding characters (total - input size) 要填充进去的字符个数*/
        char *pad_str = " "; /* Pointer to padding string */
        size_t pad_str_len = 1; // 填充字符的个数 
        zend_long   pad_type_val = STR_PAD_RIGHT; /* The padding type value 填充类型,左填充、右填充、左右填充。默认右填充*/
        size_t       i, left_pad=0, right_pad=0;
        zend_string *result = NULL;    /* Resulting string 返回值*/
    
        if (zend_parse_parameters(ZEND_NUM_ARGS(), "Sl|sl", &input, &pad_length, &pad_str, &pad_str_len, &pad_type_val) == FAILURE) {
            return;
        }
    
        /* If resulting string turns out to be shorter than input string,
           we simply copy the input and return. */
        /* 如果pad_length(参数2)小于等于输入字符串的长度,则返回原始的输入字符串。*/
        if (pad_length < 0  || (size_t)pad_length <= ZSTR_LEN(input)) {
            RETURN_STRINGL(ZSTR_VAL(input), ZSTR_LEN(input));
        }
    
        /* 填充字符串长度为0,如:str_pad("abc", 10, ""),则Warning级别的错误。
           填充字符串的默认长度为1,即str_pad("abc", 10),的情况下pad_str_len=1。*/
        if (pad_str_len == 0) {
            php_error_docref(NULL, E_WARNING, "Padding string cannot be empty");
            return;
        }
    
        /*pad_type只能为STR_PAD_LEFT, STR_PAD_RIGHT, or STR_PAD_BOTH 或 0, 1, 2*/
        if (pad_type_val < STR_PAD_LEFT || pad_type_val > STR_PAD_BOTH) {
            php_error_docref(NULL, E_WARNING, "Padding type has to be STR_PAD_LEFT, STR_PAD_RIGHT, or STR_PAD_BOTH");
            return;
        }
    
        num_pad_chars = pad_length - ZSTR_LEN(input); // 需要被填充进去的字符的数量
        if (num_pad_chars >= INT_MAX) { // num_pad_chars的最大值是2147483647。#define INT_MAX 2147483647
            php_error_docref(NULL, E_WARNING, "Padding length is too long");
            return;
        }
    
        result = zend_string_safe_alloc(1, ZSTR_LEN(input), num_pad_chars, 0);
        ZSTR_LEN(result) = 0;
    
        /* We need to figure out the left/right padding lengths. */
        switch (pad_type_val) {
            case STR_PAD_RIGHT:
                left_pad = 0;
                right_pad = num_pad_chars;
                break;
    
            case STR_PAD_LEFT:
                left_pad = num_pad_chars;
                right_pad = 0;
                break;
    
            // 左填充数量小于右,left_pad <= right_pad
            case STR_PAD_BOTH:
                left_pad = num_pad_chars / 2;
                right_pad = num_pad_chars - left_pad;
                break;
        }
    
        /* First we pad on the left. */
        /* 左填充:循环添加字符 */
        for (i = 0; i < left_pad; i++)
            ZSTR_VAL(result)[ZSTR_LEN(result)++] = pad_str[i % pad_str_len];
    
        /* Then we copy the input string. */
        /* 左填充完成后,附加输入字符串 */
        memcpy(ZSTR_VAL(result) + ZSTR_LEN(result), ZSTR_VAL(input), ZSTR_LEN(input));
        ZSTR_LEN(result) += ZSTR_LEN(input);
    
        /* Finally, we pad on the right. */
        /* 右填充:循环添加字符串 */
        for (i = 0; i < right_pad; i++)
            ZSTR_VAL(result)[ZSTR_LEN(result)++] = pad_str[i % pad_str_len];
    
        // 添加字符串结束标志''
        ZSTR_VAL(result)[ZSTR_LEN(result)] = '';
    
        // 返回新字符串
        RETURN_NEW_STR(result);
    }
  • 相关阅读:
    BZOJ 3196 二逼平衡树
    BZOJ 4241 历史研究
    Problem 71:Ordered fractions
    矿工安全生产
    Codeforces 771C:Bear and Tree Jumps
    Problem 77:Prime summations
    Problem 69:Totient maximum
    关于Euclid算法
    团体程序设计天梯赛-练习集
    埃蒙的时空航道
  • 原文地址:https://www.cnblogs.com/lxwphp/p/10487983.html
Copyright © 2020-2023  润新知