summaryrefslogtreecommitdiff
path: root/src
diff options
context:
space:
mode:
authorYegappan Lakshmanan <yegappan@yahoo.com>2023-05-06 14:08:21 +0100
committerBram Moolenaar <Bram@vim.org>2023-05-06 14:08:21 +0100
commit03ff1c2dde7f15eca5c9baa6dafbda9b49bedc3b (patch)
tree9d9a4484cd2fb33dacf9322e648cf2299a88ea7f /src
parent45fcb7928af8ac9bc5685ce7c804b8250866a874 (diff)
downloadvim-git-03ff1c2dde7f15eca5c9baa6dafbda9b49bedc3b.tar.gz
patch 9.0.1515: reverse() does not work for a Stringv9.0.1515
Problem: reverse() does not work for a String. Solution: Implement reverse() for a String. (Yegappan Lakshmanan, closes #12179)
Diffstat (limited to 'src')
-rw-r--r--src/list.c2
-rw-r--r--src/proto/strings.pro1
-rw-r--r--src/strings.c41
-rw-r--r--src/testdir/test_functions.vim17
-rw-r--r--src/testdir/test_listdict.vim2
-rw-r--r--src/version.c2
6 files changed, 64 insertions, 1 deletions
diff --git a/src/list.c b/src/list.c
index ca4352672..7042965ba 100644
--- a/src/list.c
+++ b/src/list.c
@@ -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
diff --git a/src/proto/strings.pro b/src/proto/strings.pro
index a72e1ff5e..8924a2571 100644
--- a/src/proto/strings.pro
+++ b/src/proto/strings.pro
@@ -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);
diff --git a/src/strings.c b/src/strings.c
index 7d4281dcd..90429d3ba 100644
--- a/src/strings.c
+++ b/src/strings.c
@@ -855,6 +855,47 @@ string_count(char_u *haystack, char_u *needle, int ic)
}
/*
+ * 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.
*/
diff --git a/src/testdir/test_functions.vim b/src/testdir/test_functions.vim
index e32c4f5ff..11cfcc953 100644
--- a/src/testdir/test_functions.vim
+++ b/src/testdir/test_functions.vim
@@ -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
diff --git a/src/testdir/test_listdict.vim b/src/testdir/test_listdict.vim
index b550a431f..e29c351f4 100644
--- a/src/testdir/test_listdict.vim
+++ b/src/testdir/test_listdict.vim
@@ -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:")
diff --git a/src/version.c b/src/version.c
index 2d2646198..8ec68dec7 100644
--- a/src/version.c
+++ b/src/version.c
@@ -696,6 +696,8 @@ static char *(features[]) =
static int included_patches[] =
{ /* Add new patch number below this line */
/**/
+ 1515,
+/**/
1514,
/**/
1513,