php内置函数分析之ucwords()
1 PHP_FUNCTION(ucwords) 2 { 3 zend_string *str; 4 char *delims = " \t\r\n\f\v"; 5 register char *r, *r_end; 6 size_t delims_len = 6; 7 char mask[256]; 8 9 ZEND_PARSE_PARAMETERS_START(1, 2) 10 Z_PARAM_STR(str) 11 Z_PARAM_OPTIONAL 12 Z_PARAM_STRING(delims, delims_len) 13 ZEND_PARSE_PARAMETERS_END(); 14 15 if (!ZSTR_LEN(str)) { 16 RETURN_EMPTY_STRING(); 17 } 18 19 php_charmask((unsigned char *)delims, delims_len, mask); // 初始化mask数组,并对上述6个字符执行类似这样的操作:char c='\t';mask[c]=1; 20 21 ZVAL_STRINGL(return_value, ZSTR_VAL(str), ZSTR_LEN(str)); //初始化函数返回值return_value 22 r = Z_STRVAL_P(return_value); //新字符串首地址 23 24 *r = toupper((unsigned char) *r); //将新字符串首字符大写转换 25 for (r_end = r + Z_STRLEN_P(return_value) - 1; r < r_end; ) {//一次遍历每个字符 26 if (mask[(unsigned char)*r++]) { // 字符如果是上述6个之一,则该字符后面的字符进行大写转换 27 *r = toupper((unsigned char) *r); 28 } 29 } 30 //结束之后,将返回return_value 31 }