1. 程式人生 > >php內置函數分析之str_pad()

php內置函數分析之str_pad()

warn variable pty ber emc right_pad type IT help

 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 }

php內置函數分析之str_pad()