• 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];
    
        // 添加字符串结束标志'\0'
        ZSTR_VAL(result)[ZSTR_LEN(result)] = '\0';
    
        // 返回新字符串
        RETURN_NEW_STR(result);
    }
  • 相关阅读:
    bzoj 2442: [Usaco2011 Open]修剪草坪【单调栈】
    bzoj 1579: [Usaco2009 Feb]Revamping Trails 道路升级【分层图+spfa】
    bzoj 1646: [Usaco2007 Open]Catch That Cow 抓住那只牛【bfs】
    bzoj 1677: [Usaco2005 Jan]Sumsets 求和【dp】
    bzoj 1657: [Usaco2006 Mar]Mooo 奶牛的歌声【单调栈】
    洛谷 P4180 【模板】严格次小生成树[BJWC2010]【次小生成树】
    bzoj 1660: [Usaco2006 Nov]Bad Hair Day 乱发节【单调栈】
    bzoj 1726: [Usaco2006 Nov]Roadblocks第二短路【dijskstra】
    bzoj 1631: [Usaco2007 Feb]Cow Party【spfa】
    bzoj 1669: [Usaco2006 Oct]Hungry Cows饥饿的奶牛【dp+树状数组+hash】
  • 原文地址:https://www.cnblogs.com/lxwphp/p/15453540.html
Copyright © 2020-2023  润新知