Skip to content

Commit 295ac5a

Browse files
committed
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 #1280)
1 parent 62b7f6a commit 295ac5a

File tree

4 files changed

+111
-1
lines changed

4 files changed

+111
-1
lines changed

runtime/doc/eval.txt

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2463,6 +2463,7 @@ tolower({expr}) String the String {expr} switched to lowercase
24632463
toupper({expr}) String the String {expr} switched to uppercase
24642464
tr({src}, {fromstr}, {tostr}) String translate chars of {src} in {fromstr}
24652465
to chars in {tostr}
2466+
trim({text}[, {mask}]) String trim characters in {mask} from {text}
24662467
trunc({expr}) Float truncate Float {expr}
24672468
type({name}) Number type of variable {name}
24682469
undofile({name}) String undo file name for {name}
@@ -8659,6 +8660,22 @@ tr({src}, {fromstr}, {tostr}) *tr()*
86598660
echo tr("<blob>", "<>", "{}")
86608661
< returns "{blob}"
86618662

8663+
trim({text}[, {mask}]) *trim()*
8664+
Return {text} as a String where any character in {mask} is
8665+
removed from the beginning and end of {text}.
8666+
If {mask} is not given, {mask} is all characters up to 0x20,
8667+
which includes Tab, space, NL and CR, plus the non-breaking
8668+
space character 0xa0.
8669+
This code deals with multibyte characters properly.
8670+
8671+
Examples: >
8672+
echo trim(" \r\t\t\r RESERVE \t \t\n\x0B\x0B")."_TAIL"
8673+
< returns "RESERVE_TAIL" >
8674+
echo trim("needrmvRESERVEnnneeedddrrmmmmvv", "ednmrv")
8675+
< returns "RESERVE" >
8676+
echo trim("rm<blob1><blob2><any_chars>rrmm<blob1><blob2><blob2>", "rm<blob1><blob2>")
8677+
< returns "any_chas"
8678+
86628679
trunc({expr}) *trunc()*
86638680
Return the largest integral value with magnitude less than or
86648681
equal to {expr} as a |Float| (truncate towards zero).

src/evalfunc.c

Lines changed: 69 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -430,6 +430,7 @@ static void f_timer_stopall(typval_T *argvars, typval_T *rettv);
430430
static void f_tolower(typval_T *argvars, typval_T *rettv);
431431
static void f_toupper(typval_T *argvars, typval_T *rettv);
432432
static void f_tr(typval_T *argvars, typval_T *rettv);
433+
static void f_trim(typval_T *argvars, typval_T *rettv);
433434
#ifdef FEAT_FLOAT
434435
static void f_trunc(typval_T *argvars, typval_T *rettv);
435436
#endif
@@ -899,6 +900,7 @@ static struct fst
899900
{"tolower", 1, 1, f_tolower},
900901
{"toupper", 1, 1, f_toupper},
901902
{"tr", 3, 3, f_tr},
903+
{"trim", 1, 2, f_trim},
902904
#ifdef FEAT_FLOAT
903905
{"trunc", 1, 1, f_trunc},
904906
#endif
@@ -5539,7 +5541,7 @@ f_getwinpos(typval_T *argvars UNUSED, typval_T *rettv)
55395541
return;
55405542
#ifdef FEAT_GUI
55415543
if (gui.in_use)
5542-
gui_mch_get_winpos(&x, &y);
5544+
(void)gui_mch_get_winpos(&x, &y);
55435545
# if defined(HAVE_TGETENT) && defined(FEAT_TERMRESPONSE)
55445546
else
55455547
# endif
@@ -13203,6 +13205,72 @@ f_tr(typval_T *argvars, typval_T *rettv)
1320313205
rettv->vval.v_string = ga.ga_data;
1320413206
}
1320513207

13208+
/*
13209+
* "trim({expr})" function
13210+
*/
13211+
static void
13212+
f_trim(typval_T *argvars, typval_T *rettv)
13213+
{
13214+
char_u buf1[NUMBUFLEN];
13215+
char_u buf2[NUMBUFLEN];
13216+
char_u *head = get_tv_string_buf_chk(&argvars[0], buf1);
13217+
char_u *mask = NULL;
13218+
char_u *tail;
13219+
char_u *prev;
13220+
char_u *p;
13221+
int c1;
13222+
13223+
rettv->v_type = VAR_STRING;
13224+
if (head == NULL)
13225+
{
13226+
rettv->vval.v_string = NULL;
13227+
return;
13228+
}
13229+
13230+
if (argvars[1].v_type == VAR_STRING)
13231+
mask = get_tv_string_buf_chk(&argvars[1], buf2);
13232+
13233+
while (*head != NUL)
13234+
{
13235+
c1 = PTR2CHAR(head);
13236+
if (mask == NULL)
13237+
{
13238+
if (c1 > ' ' && c1 != 0xa0)
13239+
break;
13240+
}
13241+
else
13242+
{
13243+
for (p = mask; *p != NUL; MB_PTR_ADV(p))
13244+
if (c1 == PTR2CHAR(p))
13245+
break;
13246+
if (*p == NUL)
13247+
break;
13248+
}
13249+
MB_PTR_ADV(head);
13250+
}
13251+
13252+
for (tail = head + STRLEN(head); tail > head; tail = prev)
13253+
{
13254+
prev = tail;
13255+
MB_PTR_BACK(head, prev);
13256+
c1 = PTR2CHAR(prev);
13257+
if (mask == NULL)
13258+
{
13259+
if (c1 > ' ' && c1 != 0xa0)
13260+
break;
13261+
}
13262+
else
13263+
{
13264+
for (p = mask; *p != NUL; MB_PTR_ADV(p))
13265+
if (c1 == PTR2CHAR(p))
13266+
break;
13267+
if (*p == NUL)
13268+
break;
13269+
}
13270+
}
13271+
rettv->vval.v_string = vim_strnsave(head, (int)(tail - head));
13272+
}
13273+
1320613274
#ifdef FEAT_FLOAT
1320713275
/*
1320813276
* "trunc({float})" function

src/testdir/test_functions.vim

Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -876,3 +876,26 @@ func Test_shellescape()
876876

877877
let &shell = save_shell
878878
endfunc
879+
880+
func Test_trim()
881+
call assert_equal("Testing", trim(" \t\r\r\x0BTesting \t\n\r\n\t\x0B\x0B"))
882+
call assert_equal("Testing", trim(" \t \r\r\n\n\x0BTesting \t\n\r\n\t\x0B\x0B"))
883+
call assert_equal("RESERVE", trim("xyz \twwRESERVEzyww \t\t", " wxyz\t"))
884+
call assert_equal("wRE \tSERVEzyww", trim("wRE \tSERVEzyww"))
885+
call assert_equal("abcd\t xxxx tail", trim(" \tabcd\t xxxx tail"))
886+
call assert_equal("\tabcd\t xxxx tail", trim(" \tabcd\t xxxx tail", " "))
887+
call assert_equal(" \tabcd\t xxxx tail", trim(" \tabcd\t xxxx tail", "abx"))
888+
call assert_equal("RESERVE", trim("你RESERVE好", "你好"))
889+
call assert_equal("您R E SER V E早", trim("你好您R E SER V E早好你你", "你好"))
890+
call assert_equal("你好您R E SER V E早好你你", trim(" \n\r\r 你好您R E SER V E早好你你 \t \x0B", ))
891+
call assert_equal("您R E SER V E早好你你 \t \x0B", trim(" 你好您R E SER V E早好你你 \t \x0B", " 你好"))
892+
call assert_equal("您R E SER V E早好你你 \t \x0B", trim(" tteesstttt你好您R E SER V E早好你你 \t \x0B ttestt", " 你好tes"))
893+
call assert_equal("您R E SER V E早好你你 \t \x0B", trim(" tteesstttt你好您R E SER V E早好你你 \t \x0B ttestt", " 你你你好好好tttsses"))
894+
call assert_equal("留下", trim("这些些不要这些留下这些", "这些不要"))
895+
call assert_equal("", trim("", ""))
896+
call assert_equal("a", trim("a", ""))
897+
call assert_equal("", trim("", "a"))
898+
899+
let chars = join(map(range(1, 0x20) + [0xa0], {n -> nr2char(n)}), '')
900+
call assert_equal("x", trim(chars . "x" . chars))
901+
endfunc

src/version.c

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -766,6 +766,8 @@ static char *(features[]) =
766766

767767
static int included_patches[] =
768768
{ /* Add new patch number below this line */
769+
/**/
770+
1630,
769771
/**/
770772
1629,
771773
/**/

0 commit comments

Comments
 (0)