Patch 8.2.0868
Problem:    trim() always trims both ends.
Solution:   Add an argument to only trim the beginning or end. (Yegappan
            Lakshmanan, closes #6126)
Files:      runtime/doc/eval.txt, src/evalfunc.c,
            src/testdir/test_functions.vim


*** ../vim-8.2.0867/runtime/doc/eval.txt        2020-05-31 22:06:48.081779441 
+0200
--- runtime/doc/eval.txt        2020-05-31 22:14:04.072041904 +0200
***************
*** 2892,2898 ****
  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}
--- 2898,2905 ----
  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} [, {dir}]])
!                               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}
***************
*** 10235,10247 ****
                Can also be used as a |method|: >
                        GetText()->tr(from, to)
  
! 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("   some text ")
--- 10246,10267 ----
                Can also be used as a |method|: >
                        GetText()->tr(from, to)
  
! trim({text} [, {mask} [, {dir}]])                             *trim()*
                Return {text} as a String where any character in {mask} is
!               removed from the beginning and/or 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.
! 
!               The optional {dir} argument specifies where to remove the
!               characters:
!                       0       remove from the beginning and end of {text}
!                       1       remove only at the beginning of {text}
!                       2       remove only at the end of {text}
!               When omitted both ends are trimmed.
! 
!               This function deals with multibyte characters properly.
  
                Examples: >
                        echo trim("   some text ")
***************
*** 10249,10255 ****
                        echo trim("  \r\t\t\r RESERVE \t\n\x0B\xA0") . "_TAIL"
  <             returns "RESERVE_TAIL" >
                        echo trim("rm<Xrm<>X>rrm", "rm<>")
! <             returns "Xrm<>X" (characters in the middle are not removed)
  
                Can also be used as a |method|: >
                        GetText()->trim()
--- 10269,10277 ----
                        echo trim("  \r\t\t\r RESERVE \t\n\x0B\xA0") . "_TAIL"
  <             returns "RESERVE_TAIL" >
                        echo trim("rm<Xrm<>X>rrm", "rm<>")
! <             returns "Xrm<>X" (characters in the middle are not removed) >
!                       echo trim("  vim  ", " ", 2)
! <             returns "  vim"
  
                Can also be used as a |method|: >
                        GetText()->trim()
*** ../vim-8.2.0867/src/evalfunc.c      2020-05-31 15:41:53.159138610 +0200
--- src/evalfunc.c      2020-05-31 22:09:55.209032336 +0200
***************
*** 976,982 ****
      {"tolower",               1, 1, FEARG_1,    ret_string,   f_tolower},
      {"toupper",               1, 1, FEARG_1,    ret_string,   f_toupper},
      {"tr",            3, 3, FEARG_1,    ret_string,   f_tr},
!     {"trim",          1, 2, FEARG_1,    ret_string,   f_trim},
      {"trunc",         1, 1, FEARG_1,    ret_float,    FLOAT_FUNC(f_trunc)},
      {"type",          1, 1, FEARG_1,    ret_number,   f_type},
      {"undofile",      1, 1, FEARG_1,    ret_string,   f_undofile},
--- 976,982 ----
      {"tolower",               1, 1, FEARG_1,    ret_string,   f_tolower},
      {"toupper",               1, 1, FEARG_1,    ret_string,   f_toupper},
      {"tr",            3, 3, FEARG_1,    ret_string,   f_tr},
!     {"trim",          1, 3, FEARG_1,    ret_string,   f_trim},
      {"trunc",         1, 1, FEARG_1,    ret_float,    FLOAT_FUNC(f_trunc)},
      {"type",          1, 1, FEARG_1,    ret_number,   f_type},
      {"undofile",      1, 1, FEARG_1,    ret_string,   f_undofile},
***************
*** 8637,8689 ****
      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 = 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));
--- 8637,8714 ----
      char_u    *prev;
      char_u    *p;
      int               c1;
+     int               dir = 0;
  
      rettv->v_type = VAR_STRING;
+     rettv->vval.v_string = NULL;
      if (head == NULL)
        return;
  
      if (argvars[1].v_type == VAR_STRING)
+     {
        mask = tv_get_string_buf_chk(&argvars[1], buf2);
  
!       if (argvars[2].v_type != VAR_UNKNOWN)
        {
!           int error = 0;
! 
!           // leading or trailing characters to trim
!           dir = (int)tv_get_number_chk(&argvars[2], &error);
!           if (error)
!               return;
!           if (dir < 0 || dir > 2)
!           {
!               semsg(_(e_invarg2), tv_get_string(&argvars[2]));
!               return;
!           }
        }
!     }
! 
!     if (dir == 0 || dir == 1)
!     {
!       // Trim leading characters
!       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);
        }
      }
  
!     tail = head + STRLEN(head);
!     if (dir == 0 || dir == 2)
      {
!       // Trim trailing characters
!       for (; 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));
*** ../vim-8.2.0867/src/testdir/test_functions.vim      2020-05-30 
18:37:51.031344270 +0200
--- src/testdir/test_functions.vim      2020-05-31 22:09:55.213032320 +0200
***************
*** 1497,1502 ****
--- 1497,1509 ----
    call assert_equal("a", trim("a", ""))
    call assert_equal("", trim("", "a"))
  
+   call assert_equal("vim", trim("  vim  ", " ", 0))
+   call assert_equal("vim  ", trim("  vim  ", " ", 1))
+   call assert_equal("  vim", trim("  vim  ", " ", 2))
+   call assert_fails('eval trim("  vim  ", " ", [])', 'E745:')
+   call assert_fails('eval trim("  vim  ", " ", -1)', 'E475:')
+   call assert_fails('eval trim("  vim  ", " ", 3)', 'E475:')
+ 
    let chars = join(map(range(1, 0x20) + [0xa0], {n -> n->nr2char()}), '')
    call assert_equal("x", trim(chars . "x" . chars))
  
*** ../vim-8.2.0867/src/version.c       2020-05-31 22:06:48.085779425 +0200
--- src/version.c       2020-05-31 22:11:37.284625756 +0200
***************
*** 748,749 ****
--- 748,751 ----
  {   /* Add new patch number below this line */
+ /**/
+     868,
  /**/

-- 
hundred-and-one symptoms of being an internet addict:
236. You start saving URL's in your digital watch.

 /// Bram Moolenaar -- [email protected] -- 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    ///

-- 
-- 
You received this message from the "vim_dev" maillist.
Do not top-post! Type your reply below the text you are replying to.
For more information, visit http://www.vim.org/maillist.php

--- 
You received this message because you are subscribed to the Google Groups 
"vim_dev" group.
To unsubscribe from this group and stop receiving emails from it, send an email 
to [email protected].
To view this discussion on the web visit 
https://groups.google.com/d/msgid/vim_dev/202005312021.04VKL7bC741761%40masaka.moolenaar.net.

Raspunde prin e-mail lui