php内置函数分析之trim()
官方手册中:
类似函数还有两个:ltrim() 和 rtrim()。分别处理字符串的左侧、右侧。
trim()的具体实现位于:ext/standard/string.c
/* {{{ proto string trim(string str [, string character_mask])
Strips whitespace from the beginning and end of a string */
PHP_FUNCTION(trim)
{
php_do_trim(INTERNAL_FUNCTION_PARAM_PASSTHRU, );
}
/* }}} */
/* {{{ php_do_trim
* Base for trim(), rtrim() and ltrim() functions.
*/
static void php_do_trim(INTERNAL_FUNCTION_PARAMETERS, int mode)
{
zend_string *str;
zend_string *what = NULL; ZEND_PARSE_PARAMETERS_START(, )
Z_PARAM_STR(str)
Z_PARAM_OPTIONAL
Z_PARAM_STR(what)
ZEND_PARSE_PARAMETERS_END(); ZVAL_STR(return_value, php_trim(str, (what ? ZSTR_VAL(what) : NULL), (what ? ZSTR_LEN(what) : ), mode));
}
/* }}} */
具体实现:
/* {{{ php_trim()
* mode 1 : trim left
* mode 2 : trim right
* mode 3 : trim left and right
* what indicates which chars are to be trimmed. NULL->default (' \t\n\r\v\0')
*/
PHPAPI zend_string *php_trim(zend_string *str, char *what, size_t what_len, int mode)
{
const char *c = ZSTR_VAL(str);
size_t len = ZSTR_LEN(str);
register size_t i;
size_t trimmed = ;
char mask[]; if (what) {
if (what_len == ) {
char p = *what;
if (mode & ) {
for (i = ; i < len; i++) {
if (c[i] == p) {
trimmed++;
} else {
break;
}
}
len -= trimmed;
c += trimmed;
}
if (mode & ) {
if (len > ) {
i = len - ;
do {
if (c[i] == p) {
len--;
} else {
break;
}
} while (i-- != );
}
}
} else {
php_charmask((unsigned char*)what, what_len, mask); if (mode & ) {
for (i = ; i < len; i++) {
if (mask[(unsigned char)c[i]]) {
trimmed++;
} else {
break;
}
}
len -= trimmed;
c += trimmed;
}
if (mode & ) {
if (len > ) {
i = len - ;
do {
if (mask[(unsigned char)c[i]]) {
len--;
} else {
break;
}
} while (i-- != );
}
}
}
} else {
if (mode & ) {
for (i = ; i < len; i++) {
if ((unsigned char)c[i] <= ' ' &&
(c[i] == ' ' || c[i] == '\n' || c[i] == '\r' || c[i] == '\t' || c[i] == '\v' || c[i] == '\0')) {
trimmed++;
} else {
break;
}
}
len -= trimmed;
c += trimmed;
}
if (mode & ) {
if (len > ) {
i = len - ;
do {
if ((unsigned char)c[i] <= ' ' &&
(c[i] == ' ' || c[i] == '\n' || c[i] == '\r' || c[i] == '\t' || c[i] == '\v' || c[i] == '\0')) {
len--;
} else {
break;
}
} while (i-- != );
}
}
} if (ZSTR_LEN(str) == len) {
return zend_string_copy(str);
} else {
return zend_string_init(c, len, );
}
}
/* }}} */
ltrim()、rtrim() 或 trim()的参数二(what)存在时进入15行处的分支,参数二不存在时进入68行处的分支。
没有参数二时,会去除空格、"\t"、"\n"、"\r"、"\0"、"\v"
存在参数二时,会去除参数二中指定的字符。
其中函数 php_charmask() 用于处理 trim("abc123456fjsklsf", "a..z") 中 字符范围 a...z
static inline int php_charmask(unsigned char *input, size_t len, char *mask)
{
unsigned char *end;
unsigned char c;
int result = SUCCESS; memset(mask, , );
for (end = input+len; input < end; input++) {
c=*input;
if ((input+ < end) && input[] == '.' && input[] == '.'
&& input[] >= c) {
memset(mask+c, , input[] - c + );
input+=;
} else if ((input+ < end) && input[] == '.' && input[] == '.') {
/* Error, try to be as helpful as possible:
(a range ending/starting with '.' won't be captured here) */
if (end-len >= input) { /* there was no 'left' char */
php_error_docref(NULL, E_WARNING, "Invalid '..'-range, no character to the left of '..'");
result = FAILURE;
continue;
}
if (input+ >= end) { /* there is no 'right' char */
php_error_docref(NULL, E_WARNING, "Invalid '..'-range, no character to the right of '..'");
result = FAILURE;
continue;
}
if (input[-] > input[]) { /* wrong order */
php_error_docref(NULL, E_WARNING, "Invalid '..'-range, '..'-range needs to be incrementing");
result = FAILURE;
continue;
}
/* FIXME: better error (a..b..c is the only left possibility?) */
php_error_docref(NULL, E_WARNING, "Invalid '..'-range");
result = FAILURE;
continue;
} else {
mask[c]=;
}
}
return result;
}
第二个参数是字符范围的时候,源码中并没有严格限制成 a..z 这样的格式。
总的流程就是,待处理的字符串左侧(或右侧)逐字符去匹配是否在将要去除的字符中(第二个参数设置的字符 或者 默认的那6个字符),
如果字符匹配到则进行下一个比较,否则中断匹配查找。返回余下字符串。
php内置函数分析之trim()的更多相关文章
- map内置函数分析所得到的思路
map:会根据提供的函数对指定序列做映射. map(func, *iterables) --> map object Make an iterator that computes the fun ...
- php内置函数分析之array_diff_assoc()
static void php_array_diff_key(INTERNAL_FUNCTION_PARAMETERS, int data_compare_type) /* {{{ */ { uint ...
- php内置函数分析之array_combine()
PHP_FUNCTION(array_combine) { HashTable *values, *keys; uint32_t pos_values = ; zval *entry_keys, *e ...
- php内置函数分析之ucwords()
PHP_FUNCTION(ucwords) { zend_string *str; char *delims = " \t\r\n\f\v"; register char *r, ...
- php内置函数分析之strtoupper()、strtolower()
strtoupper(): PHP_FUNCTION(strtoupper) { zend_string *str; ZEND_PARSE_PARAMETERS_START(, ) Z_PARAM_S ...
- php内置函数分析之ucfirst()、lcfirst()
ucfirst($str) 将 str 的首字符(如果首字符是字母)转换为大写字母,并返回这个字符串. 源码位于 ext/standard/string.c /* {{{ php_ucfirst Up ...
- php内置函数分析之str_pad()
PHP_FUNCTION(str_pad) { /* Input arguments */ zend_string *input; /* Input string 输入字符串*/ zend_long ...
- php内置函数分析之array_fill_keys()
PHP_FUNCTION(array_fill_keys) { zval *keys, *val, *entry; if (zend_parse_parameters(ZEND_NUM_ARGS(), ...
- php内置函数分析range()
PHP_FUNCTION(range) { zval *zlow, *zhigh, *zstep = NULL, tmp; , is_step_double = ; double step = 1.0 ...
随机推荐
- telnet测试端口是否打开?
如何测试远程服务器的某个端口是否打开? 例如: telnet 192.168.1.1 5000 返回: Trying 127.0.0.1...Connected to 127.0.0.1.Escape ...
- 单页应用 cookies处理
w Node & 单页应用 来做一个完整用户系统吧! - harryfyodor的前端专栏 - SegmentFaulthttps://segmentfault.com/a/119000000 ...
- First-order logic
w https://en.wikipedia.org/wiki/First-order_logic
- fedora23禁用不需要的服务?--systemd服务单元?
sign up: 签约; 登记, 注册. i'll sign up and go to the front and fight. he persuaded the company to sign up ...
- windows传输文件到linux脚本
安装pscp https://www.chiark.greenend.org.uk/~sgtatham/putty/latest.html cmd脚本 @echo off rem 拷贝的文件名称 se ...
- 用JS实现将十进制转化为二进制
- 测开之路九十五:css进阶之光标和溢出内容处理
光标样式:cursor 准备文字 css 溢出内容处理:overflow,默认溢出部分是显示 先把内容放到盒子里面 正常显示 不显示溢出内容 显示为滚动条 自动处理 css /* 光标样式 */p{ ...
- linux .bashrc文件修改和生效
linux .bashrc文件修改和生效 cd home ==>选择用户文件夹=>ll -la .bashrc 使用man bash命令查看到的联机帮助文件中的相关解释如下: ...
- Mac--PHP已经开启gd扩展验证码不显示
错误显示:Call to undefined function imagettftext() 原因: mac系统中自带的php的gd库中,缺少对freetype的支持,导致图片无法显示. 解决: 1 ...
- ceph部署问题解决
注意:1.ceph-deploy实用程序将输出文件到当前目录.执行ceph-deploy时确保你在这个目录下.2.不要使用sudo调用ceph-deploy,要么以root用户身份运行它,因为它不会发 ...