php内置函数分析之str_pad()

 1 PHP_FUNCTION(str_pad)
 2 {
 3     /* Input arguments */
 4     zend_string *input;                /* Input string 输入字符串*/
 5     zend_long pad_length;            /* Length to pad to 填充到多长.*/
 6 
 7     /* Helper variables */
 8     size_t num_pad_chars;        /* Number of padding characters (total - input size) 要填充进去的字符个数*/
 9     char *pad_str = " "; /* Pointer to padding string */
10     size_t pad_str_len = 1; // 填充字符的个数 
11     zend_long   pad_type_val = STR_PAD_RIGHT; /* The padding type value 填充类型,左填充、右填充、左右填充。默认右填充*/
12     size_t       i, left_pad=0, right_pad=0;
13     zend_string *result = NULL;    /* Resulting string 返回值*/
14 
15     if (zend_parse_parameters(ZEND_NUM_ARGS(), "Sl|sl", &input, &pad_length, &pad_str, &pad_str_len, &pad_type_val) == FAILURE) {
16         return;
17     }
18 
19     /* If resulting string turns out to be shorter than input string,
20        we simply copy the input and return. */
21     /* 如果pad_length(参数2)小于等于输入字符串的长度,则返回原始的输入字符串。*/
22     if (pad_length < 0  || (size_t)pad_length <= ZSTR_LEN(input)) {
23         RETURN_STRINGL(ZSTR_VAL(input), ZSTR_LEN(input));
24     }
25 
26     /* 填充字符串长度为0,如:str_pad("abc", 10, ""),则Warning级别的错误。
27        填充字符串的默认长度为1,即str_pad("abc", 10),的情况下pad_str_len=1。*/
28     if (pad_str_len == 0) {
29         php_error_docref(NULL, E_WARNING, "Padding string cannot be empty");
30         return;
31     }
32 
33     /*pad_type只能为STR_PAD_LEFT, STR_PAD_RIGHT, or STR_PAD_BOTH 或 0, 1, 2*/
34     if (pad_type_val < STR_PAD_LEFT || pad_type_val > STR_PAD_BOTH) {
35         php_error_docref(NULL, E_WARNING, "Padding type has to be STR_PAD_LEFT, STR_PAD_RIGHT, or STR_PAD_BOTH");
36         return;
37     }
38 
39     num_pad_chars = pad_length - ZSTR_LEN(input); // 需要被填充进去的字符的数量
40     if (num_pad_chars >= INT_MAX) { // num_pad_chars的最大值是2147483647。#define INT_MAX 2147483647
41         php_error_docref(NULL, E_WARNING, "Padding length is too long");
42         return;
43     }
44 
45     result = zend_string_safe_alloc(1, ZSTR_LEN(input), num_pad_chars, 0);
46     ZSTR_LEN(result) = 0;
47 
48     /* We need to figure out the left/right padding lengths. */
49     switch (pad_type_val) {
50         case STR_PAD_RIGHT:
51             left_pad = 0;
52             right_pad = num_pad_chars;
53             break;
54 
55         case STR_PAD_LEFT:
56             left_pad = num_pad_chars;
57             right_pad = 0;
58             break;
59 
60         // 左填充数量小于右,left_pad <= right_pad
61         case STR_PAD_BOTH:
62             left_pad = num_pad_chars / 2;
63             right_pad = num_pad_chars - left_pad;
64             break;
65     }
66 
67     /* First we pad on the left. */
68     /* 左填充:循环添加字符 */
69     for (i = 0; i < left_pad; i++)
70         ZSTR_VAL(result)[ZSTR_LEN(result)++] = pad_str[i % pad_str_len];
71 
72     /* Then we copy the input string. */
73     /* 左填充完成后,附加输入字符串 */
74     memcpy(ZSTR_VAL(result) + ZSTR_LEN(result), ZSTR_VAL(input), ZSTR_LEN(input));
75     ZSTR_LEN(result) += ZSTR_LEN(input);
76 
77     /* Finally, we pad on the right. */
78     /* 右填充:循环添加字符串 */
79     for (i = 0; i < right_pad; i++)
80         ZSTR_VAL(result)[ZSTR_LEN(result)++] = pad_str[i % pad_str_len];
81 
82     // 添加字符串结束标志'\0'
83     ZSTR_VAL(result)[ZSTR_LEN(result)] = '\0';
84 
85     // 返回新字符串
86     RETURN_NEW_STR(result);
87 }

猜你喜欢

转载自www.cnblogs.com/natian-ws/p/9094930.html
今日推荐