]> git.ipfire.org Git - thirdparty/vim.git/commitdiff
patch 9.0.1515: reverse() does not work for a String v9.0.1515
authorYegappan Lakshmanan <yegappan@yahoo.com>
Sat, 6 May 2023 13:08:21 +0000 (14:08 +0100)
committerBram Moolenaar <Bram@vim.org>
Sat, 6 May 2023 13:08:21 +0000 (14:08 +0100)
Problem:    reverse() does not work for a String.
Solution:   Implement reverse() for a String. (Yegappan Lakshmanan,
            closes #12179)

runtime/doc/builtin.txt
runtime/doc/usr_41.txt
src/list.c
src/proto/strings.pro
src/strings.c
src/testdir/test_functions.vim
src/testdir/test_listdict.vim
src/version.c

index dd680069f77164f4972fa5ddd4e50b609cf286d5..beb7ac71843b03bb852c3fdc1c902dff7dc867d1 100644 (file)
@@ -484,7 +484,8 @@ rename({from}, {to})                Number  rename (move) file from {from} to {to}
 repeat({expr}, {count})                List/Blob/String
                                        repeat {expr} {count} times
 resolve({filename})            String  get filename a shortcut points to
-reverse({list})                        List    reverse {list} in-place
+reverse({obj})                 List/Blob/String
+                                       reverse {obj}
 round({expr})                  Float   round off {expr}
 rubyeval({expr})               any     evaluate |Ruby| expression
 screenattr({row}, {col})       Number  attribute at screen position
@@ -7404,11 +7405,13 @@ resolve({filename})                                     *resolve()* *E655*
                        GetName()->resolve()
 
 reverse({object})                                      *reverse()*
-               Reverse the order of items in {object} in-place.
-               {object} can be a |List| or a |Blob|.
-               Returns {object}.
-               Returns zero if {object} is not a List or a Blob.
-               If you want an object to remain unmodified make a copy first: >
+               Reverse the order of items in {object}.  {object} can be a
+               |List|, a |Blob| or a |String|.  For a List and a Blob the
+               items are reversed in-place and {object} is returned.
+               For a String a new String is returned.
+               Returns zero if {object} is not a List, Blob or a String.
+               If you want a List or Blob to remain unmodified make a copy
+               first: >
                        :let revlist = reverse(copy(mylist))
 <              Can also be used as a |method|: >
                        mylist->reverse()
index bc6d8b412376011527110c7b302b2308185b27ad..4e194d33d37dc2455f21db1a11897324357b112b 100644 (file)
@@ -759,6 +759,7 @@ String manipulation:                                        *string-functions*
        strdisplaywidth()       size of string when displayed, deals with tabs
        setcellwidths()         set character cell width overrides
        getcellwidths()         get character cell width overrides
+       reverse()               reverse the order of characters in a string
        substitute()            substitute a pattern match with a string
        submatch()              get a specific match in ":s" and substitute()
        strpart()               get part of a string using byte index
@@ -797,7 +798,7 @@ List manipulation:                                  *list-functions*
        reduce()                reduce a List to a value
        slice()                 take a slice of a List
        sort()                  sort a List
-       reverse()               reverse the order of a List or Blob
+       reverse()               reverse the order of items in a List
        uniq()                  remove copies of repeated adjacent items
        split()                 split a String into a List
        join()                  join List items into a String
@@ -864,6 +865,7 @@ Floating point computation:                         *float-functions*
 Blob manipulation:                                     *blob-functions*
        blob2list()             get a list of numbers from a blob
        list2blob()             get a blob from a list of numbers
+       reverse()               reverse the order of numbers in a blob
 
 Other computation:                                     *bitwise-function*
        and()                   bitwise AND
index ca435267247cb71beebd5133edd17b8ff6460673..7042965ba3c8a43319fc0f093bf8781c21c3db05 100644 (file)
@@ -2999,6 +2999,8 @@ f_reverse(typval_T *argvars, typval_T *rettv)
 
     if (argvars[0].v_type == VAR_BLOB)
        blob_reverse(argvars[0].vval.v_blob, rettv);
+    else if (argvars[0].v_type == VAR_STRING)
+       string_reverse(argvars[0].vval.v_string, rettv);
     else if (argvars[0].v_type != VAR_LIST)
        semsg(_(e_argument_of_str_must_be_list_or_blob), "reverse()");
     else
index a72e1ff5ed82921b0732cae0ffc2a8524243ee97..8924a25715ebe6caf0589e76330d7d1f37e263d0 100644 (file)
@@ -23,6 +23,7 @@ int has_non_ascii(char_u *s);
 char_u *concat_str(char_u *str1, char_u *str2);
 char_u *string_quote(char_u *str, int function);
 long string_count(char_u *haystack, char_u *needle, int ic);
+void string_reverse(char_u *str, typval_T *rettv);
 void string_filter_map(char_u *str, filtermap_T filtermap, typval_T *expr, typval_T *rettv);
 void string_reduce(typval_T *argvars, typval_T *expr, typval_T *rettv);
 void f_byteidx(typval_T *argvars, typval_T *rettv);
index 7d4281dcdc9454144673865822ae941e2942c5eb..90429d3baf6c8b00285c548db9c74a2d2ff86e5b 100644 (file)
@@ -854,6 +854,47 @@ string_count(char_u *haystack, char_u *needle, int ic)
     return n;
 }
 
+/*
+ * Reverse the string in 'str' and set the result in 'rettv'.
+ */
+    void
+string_reverse(char_u *str, typval_T *rettv)
+{
+    rettv->v_type = VAR_STRING;
+    rettv->vval.v_string = NULL;
+    if (str == NULL)
+       return;
+
+    char_u     *rstr = vim_strsave(str);
+    rettv->vval.v_string = rstr;
+    if (rstr == NULL || *str == NUL)
+       return;
+
+    size_t     len = STRLEN(rstr);
+    if (has_mbyte)
+    {
+       char_u *src = str;
+       char_u *dest = rstr + len;
+
+       while (src < str + len)
+       {
+           int clen = mb_ptr2len(src);
+           dest -= clen;
+           mch_memmove(dest, src, (size_t)clen);
+           src += clen;
+       }
+    }
+    else
+    {
+       for (size_t i = 0; i < len / 2; i++)
+       {
+           char tmp = rstr[len - i - 1];
+           rstr[len - i - 1] = rstr[i];
+           rstr[i] = tmp;
+       }
+    }
+}
+
 /*
  * Make a typval_T of the first character of "input" and store it in "output".
  * Return OK or FAIL.
index e32c4f5ffbdc945a52f0beec066dfd8bba41d2fb..11cfcc95365f0d62a1aa0c1c35884451015d09b3 100644 (file)
@@ -3469,4 +3469,21 @@ func Test_delfunc_while_listing()
   call StopVimInTerminal(buf)
 endfunc
 
+" Test for the reverse() function with a string
+func Test_string_reverse()
+  call assert_equal('', reverse(test_null_string()))
+  for [s1, s2] in [['', ''], ['a', 'a'], ['ab', 'ba'], ['abc', 'cba'],
+        \ ['abcd', 'dcba'], ['«-«-»-»', '»-»-«-«'],
+        \ ['🇦', '🇦'], ['🇦🇧', '🇧🇦'], ['🇦🇧🇨', '🇨🇧🇦'],
+        \ ['🇦«🇧-🇨»🇩', '🇩»🇨-🇧«🇦']]
+    call assert_equal(s2, reverse(s1))
+  endfor
+
+  " test in latin1 encoding
+  let save_enc = &encoding
+  set encoding=latin1
+  call assert_equal('dcba', reverse('abcd'))
+  let &encoding = save_enc
+endfunc
+
 " vim: shiftwidth=2 sts=2 expandtab
index b550a431fcf537242be1040150e526c848e97ed2..e29c351f41c5c5ddb5ecea8180a905a4284539a9 100644 (file)
@@ -981,7 +981,7 @@ func Test_reverse_sort_uniq()
   END
   call v9.CheckLegacyAndVim9Success(lines)
 
-  call assert_fails('call reverse("")', 'E899:')
+  call assert_fails('call reverse({})', 'E899:')
   call assert_fails('call uniq([1, 2], {x, y -> []})', 'E745:')
   call assert_fails("call sort([1, 2], function('min'), 1)", "E1206:")
   call assert_fails("call sort([1, 2], function('invalid_func'))", "E700:")
index 2d264619898cd1e395555258498d9013e00d55a6..8ec68dec76f6c6ea248e4838182abe058d17de47 100644 (file)
@@ -695,6 +695,8 @@ static char *(features[]) =
 
 static int included_patches[] =
 {   /* Add new patch number below this line */
+/**/
+    1515,
 /**/
     1514,
 /**/