To: vim_dev@googlegroups.com Subject: Patch 8.0.1630 Fcc: outbox From: Bram Moolenaar Mime-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit ------------ Patch 8.0.1630 Problem: Trimming white space is not that easy. Solution: Add the trim() function. (Bukn, closes #1280) Files: src/evalfunc.c, runtime/doc/eval.txt, src/testdir/test_functions.vim *** ../vim-8.0.1629/src/evalfunc.c 2018-03-16 20:46:52.670189987 +0100 --- src/evalfunc.c 2018-03-22 22:41:08.494261375 +0100 *************** *** 430,435 **** --- 430,436 ---- 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,904 **** --- 900,906 ---- {"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,5545 **** return; #ifdef FEAT_GUI if (gui.in_use) ! gui_mch_get_winpos(&x, &y); # if defined(HAVE_TGETENT) && defined(FEAT_TERMRESPONSE) else # endif --- 5541,5547 ---- return; #ifdef FEAT_GUI if (gui.in_use) ! (void)gui_mch_get_winpos(&x, &y); # if defined(HAVE_TGETENT) && defined(FEAT_TERMRESPONSE) else # endif *************** *** 13203,13208 **** --- 13205,13276 ---- 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 *** ../vim-8.0.1629/runtime/doc/eval.txt 2018-03-20 18:35:31.162086009 +0100 --- runtime/doc/eval.txt 2018-03-22 22:16:09.682781365 +0100 *************** *** 2458,2463 **** --- 2463,2469 ---- toupper({expr}) String the String {expr} switched to uppercase tr({src}, {fromstr}, {tostr}) String translate chars of {src} in {fromstr} to chars in {tostr} + trim({text}[, {mask}]) String trim characters in {mask} from {text} trunc({expr}) Float truncate Float {expr} type({name}) Number type of variable {name} undofile({name}) String undo file name for {name} *************** *** 8601,8606 **** --- 8660,8681 ---- echo tr("", "<>", "{}") < returns "{blob}" + trim({text}[, {mask}]) *trim()* + Return {text} as a String where any character in {mask} is + removed from the beginning and end of {text}. + If {mask} is not given, {mask} is all characters up to 0x20, + which includes Tab, space, NL and CR, plus the non-breaking + space character 0xa0. + This code deals with multibyte characters properly. + + Examples: > + echo trim(" \r\t\t\r RESERVE \t \t\n\x0B\x0B")."_TAIL" + < returns "RESERVE_TAIL" > + echo trim("needrmvRESERVEnnneeedddrrmmmmvv", "ednmrv") + < returns "RESERVE" > + echo trim("rmrrmm", "rm") + < returns "any_chas" + trunc({expr}) *trunc()* Return the largest integral value with magnitude less than or equal to {expr} as a |Float| (truncate towards zero). *** ../vim-8.0.1629/src/testdir/test_functions.vim 2017-12-22 21:03:04.866173775 +0100 --- src/testdir/test_functions.vim 2018-03-22 22:48:20.471782489 +0100 *************** *** 876,878 **** --- 876,901 ---- let &shell = save_shell endfunc + + func Test_trim() + call assert_equal("Testing", trim(" \t\r\r\x0BTesting \t\n\r\n\t\x0B\x0B")) + call assert_equal("Testing", trim(" \t \r\r\n\n\x0BTesting \t\n\r\n\t\x0B\x0B")) + call assert_equal("RESERVE", trim("xyz \twwRESERVEzyww \t\t", " wxyz\t")) + call assert_equal("wRE \tSERVEzyww", trim("wRE \tSERVEzyww")) + call assert_equal("abcd\t xxxx tail", trim(" \tabcd\t xxxx tail")) + call assert_equal("\tabcd\t xxxx tail", trim(" \tabcd\t xxxx tail", " ")) + call assert_equal(" \tabcd\t xxxx tail", trim(" \tabcd\t xxxx tail", "abx")) + call assert_equal("RESERVE", trim("你RESERVE好", "你好")) + call assert_equal("您R E SER V E早", trim("你好您R E SER V E早好你你", "你好")) + call assert_equal("你好您R E SER V E早好你你", trim(" \n\r\r 你好您R E SER V E早好你你 \t \x0B", )) + call assert_equal("您R E SER V E早好你你 \t \x0B", trim(" 你好您R E SER V E早好你你 \t \x0B", " 你好")) + call assert_equal("您R E SER V E早好你你 \t \x0B", trim(" tteesstttt你好您R E SER V E早好你你 \t \x0B ttestt", " 你好tes")) + call assert_equal("您R E SER V E早好你你 \t \x0B", trim(" tteesstttt你好您R E SER V E早好你你 \t \x0B ttestt", " 你你你好好好tttsses")) + call assert_equal("留下", trim("这些些不要这些留下这些", "这些不要")) + call assert_equal("", trim("", "")) + call assert_equal("a", trim("a", "")) + call assert_equal("", trim("", "a")) + + let chars = join(map(range(1, 0x20) + [0xa0], {n -> nr2char(n)}), '') + call assert_equal("x", trim(chars . "x" . chars)) + endfunc *** ../vim-8.0.1629/src/version.c 2018-03-22 21:44:02.697764145 +0100 --- src/version.c 2018-03-22 22:52:36.222304977 +0100 *************** *** 768,769 **** --- 768,771 ---- { /* Add new patch number below this line */ + /**/ + 1630, /**/ -- A fine is a tax for doing wrong. A tax is a fine for doing well. /// Bram Moolenaar -- Bram@Moolenaar.net -- http://www.Moolenaar.net \\\ /// sponsor Vim, vote for features -- http://www.Vim.org/sponsor/ \\\ \\\ an exciting new programming language -- http://www.Zimbu.org /// \\\ help me help AIDS victims -- http://ICCF-Holland.org ///