vim-patch:8.0.1630: trimming white space is not that easy

Problem:    Trimming white space is not that easy.
Solution:   Add the trim() function. (Bukn, closes vim/vim#1280)
295ac5ab5e
This commit is contained in:
Jan Edmund Lazo
2018-08-13 11:34:59 -04:00
parent e346c01c31
commit 163680a58e
4 changed files with 104 additions and 0 deletions

View File

@@ -17218,6 +17218,69 @@ error:
return;
}
// "trim({expr})" function
static void f_trim(typval_T *argvars, typval_T *rettv, FunPtr fptr)
{
char buf1[NUMBUFLEN];
char buf2[NUMBUFLEN];
const char_u *head = (const char_u *)tv_get_string_buf_chk(&argvars[0], buf1);
const char_u *mask = NULL;
const char_u *tail;
const char_u *prev;
const char_u *p;
int c1;
rettv->v_type = VAR_STRING;
if (head == NULL) {
rettv->vval.v_string = NULL;
return;
}
if (argvars[1].v_type == VAR_STRING) {
mask = (const char_u *)tv_get_string_buf_chk(&argvars[1], buf2);
}
while (*head != NUL) {
c1 = PTR2CHAR(head);
if (mask == NULL) {
if (c1 > ' ' && c1 != 0xa0) {
break;
}
} else {
for (p = mask; *p != NUL; MB_PTR_ADV(p)) {
if (c1 == PTR2CHAR(p)) {
break;
}
}
if (*p == NUL) {
break;
}
}
MB_PTR_ADV(head);
}
for (tail = head + STRLEN(head); tail > head; tail = prev) {
prev = tail;
MB_PTR_BACK(head, prev);
c1 = PTR2CHAR(prev);
if (mask == NULL) {
if (c1 > ' ' && c1 != 0xa0) {
break;
}
} else {
for (p = mask; *p != NUL; MB_PTR_ADV(p)) {
if (c1 == PTR2CHAR(p)) {
break;
}
}
if (*p == NUL) {
break;
}
}
}
rettv->vval.v_string = vim_strnsave(head, (int)(tail - head));
}
/*
* "type(expr)" function
*/