diff options
author | Bram Moolenaar <Bram@vim.org> | 2018-03-22 23:04:02 +0100 |
---|---|---|
committer | Bram Moolenaar <Bram@vim.org> | 2018-03-22 23:04:02 +0100 |
commit | 295ac5ab5e840af6051bed5ec9d9acc3c73445de (patch) | |
tree | d858840ae5a9c225d34c5b95a38ef24eeec422d5 /src/evalfunc.c | |
parent | 62b7f6a139a19eb6c79eb428c66a7118e9741b5d (diff) |
patch 8.0.1630: trimming white space is not that easyv8.0.1630
Problem: Trimming white space is not that easy.
Solution: Add the trim() function. (Bukn, closes #1280)
Diffstat (limited to 'src/evalfunc.c')
-rw-r--r-- | src/evalfunc.c | 70 |
1 files changed, 69 insertions, 1 deletions
diff --git a/src/evalfunc.c b/src/evalfunc.c index c025a3486b..780458af95 100644 --- a/src/evalfunc.c +++ b/src/evalfunc.c @@ -430,6 +430,7 @@ static void f_timer_stopall(typval_T *argvars, typval_T *rettv); static void f_tolower(typval_T *argvars, typval_T *rettv); static void f_toupper(typval_T *argvars, typval_T *rettv); static void f_tr(typval_T *argvars, typval_T *rettv); +static void f_trim(typval_T *argvars, typval_T *rettv); #ifdef FEAT_FLOAT static void f_trunc(typval_T *argvars, typval_T *rettv); #endif @@ -899,6 +900,7 @@ static struct fst {"tolower", 1, 1, f_tolower}, {"toupper", 1, 1, f_toupper}, {"tr", 3, 3, f_tr}, + {"trim", 1, 2, f_trim}, #ifdef FEAT_FLOAT {"trunc", 1, 1, f_trunc}, #endif @@ -5539,7 +5541,7 @@ f_getwinpos(typval_T *argvars UNUSED, typval_T *rettv) return; #ifdef FEAT_GUI if (gui.in_use) - gui_mch_get_winpos(&x, &y); + (void)gui_mch_get_winpos(&x, &y); # if defined(HAVE_TGETENT) && defined(FEAT_TERMRESPONSE) else # endif @@ -13203,6 +13205,72 @@ error: rettv->vval.v_string = ga.ga_data; } +/* + * "trim({expr})" function + */ + static void +f_trim(typval_T *argvars, typval_T *rettv) +{ + char_u buf1[NUMBUFLEN]; + char_u buf2[NUMBUFLEN]; + char_u *head = get_tv_string_buf_chk(&argvars[0], buf1); + char_u *mask = NULL; + char_u *tail; + char_u *prev; + 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 = get_tv_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)); +} + #ifdef FEAT_FLOAT /* * "trunc({float})" function |