aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorzeertzjq <zeertzjq@outlook.com>2023-08-19 17:44:19 +0800
committerzeertzjq <zeertzjq@outlook.com>2023-08-19 18:05:46 +0800
commit4c7df98e4eeed20f8a9c461729935b79743d7752 (patch)
treedd49a0e4c1490d1eb660b6321d108d3924f9de62
parentd7ae9ae3e52362da33902c31ecccc79c49d3866e (diff)
downloadrneovim-4c7df98e4eeed20f8a9c461729935b79743d7752.tar.gz
rneovim-4c7df98e4eeed20f8a9c461729935b79743d7752.tar.bz2
rneovim-4c7df98e4eeed20f8a9c461729935b79743d7752.zip
vim-patch:9.0.1515: reverse() does not work for a String
Problem: reverse() does not work for a String. Solution: Implement reverse() for a String. (Yegappan Lakshmanan, closes vim/vim#12179) https://github.com/vim/vim/commit/03ff1c2dde7f15eca5c9baa6dafbda9b49bedc3b vim-patch:9.0.1738: Duplicate code to reverse a string Problem: Duplicate code to reverse a string Solution: Move reverse_text() to strings.c and remove string_reverse(). closes: vim/vim#12847 https://github.com/vim/vim/commit/4dd266cb66d901cf5324f09405cfea3f004bd29f Co-authored-by: Yegappan Lakshmanan <yegappan@yahoo.com>
-rw-r--r--runtime/doc/builtin.txt12
-rw-r--r--runtime/doc/usr_41.txt4
-rw-r--r--runtime/lua/vim/_meta/vimfn.lua12
-rw-r--r--src/nvim/eval.lua12
-rw-r--r--src/nvim/eval/funcs.c7
-rw-r--r--src/nvim/strings.c7
-rw-r--r--test/old/testdir/test_functions.vim17
-rw-r--r--test/old/testdir/test_listdict.vim2
8 files changed, 51 insertions, 22 deletions
diff --git a/runtime/doc/builtin.txt b/runtime/doc/builtin.txt
index 9a662761c4..0ae615ac3f 100644
--- a/runtime/doc/builtin.txt
+++ b/runtime/doc/builtin.txt
@@ -5666,11 +5666,13 @@ resolve({filename}) *resolve()* *E65
path name) and also keeps a trailing path separator.
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: >vim
+ 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: >vim
let revlist = reverse(copy(mylist))
<
diff --git a/runtime/doc/usr_41.txt b/runtime/doc/usr_41.txt
index ac371cf0e3..c8b31b2d5b 100644
--- a/runtime/doc/usr_41.txt
+++ b/runtime/doc/usr_41.txt
@@ -626,6 +626,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
@@ -664,7 +665,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
@@ -731,6 +732,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
diff --git a/runtime/lua/vim/_meta/vimfn.lua b/runtime/lua/vim/_meta/vimfn.lua
index 8ae6dd5f10..030f268f81 100644
--- a/runtime/lua/vim/_meta/vimfn.lua
+++ b/runtime/lua/vim/_meta/vimfn.lua
@@ -6762,11 +6762,13 @@ vim.fn['repeat'] = function(expr, count) end
--- @return any
function vim.fn.resolve(filename) end
---- 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: >vim
+--- 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: >vim
--- let revlist = reverse(copy(mylist))
--- <
---
diff --git a/src/nvim/eval.lua b/src/nvim/eval.lua
index 2b89d34a6d..154023b25f 100644
--- a/src/nvim/eval.lua
+++ b/src/nvim/eval.lua
@@ -8151,11 +8151,13 @@ M.funcs = {
args = 1,
base = 1,
desc = [=[
- 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: >vim
+ 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: >vim
let revlist = reverse(copy(mylist))
<
]=],
diff --git a/src/nvim/eval/funcs.c b/src/nvim/eval/funcs.c
index 9926530d58..b97e19131e 100644
--- a/src/nvim/eval/funcs.c
+++ b/src/nvim/eval/funcs.c
@@ -6209,6 +6209,13 @@ static void f_reverse(typval_T *argvars, typval_T *rettv, EvalFuncData fptr)
tv_blob_set(b, len - i - 1, tmp);
}
tv_blob_set_ret(rettv, b);
+ } else if (argvars[0].v_type == VAR_STRING) {
+ rettv->v_type = VAR_STRING;
+ if (argvars[0].vval.v_string != NULL) {
+ rettv->vval.v_string = reverse_text(argvars[0].vval.v_string);
+ } else {
+ rettv->vval.v_string = NULL;
+ }
} else if (argvars[0].v_type != VAR_LIST) {
semsg(_(e_listblobarg), "reverse()");
} else {
diff --git a/src/nvim/strings.c b/src/nvim/strings.c
index cfc7738ad7..ec770de4a0 100644
--- a/src/nvim/strings.c
+++ b/src/nvim/strings.c
@@ -2168,20 +2168,17 @@ int kv_do_printf(StringBuilder *str, const char *fmt, ...)
///
/// @return the allocated string.
char *reverse_text(char *s)
- FUNC_ATTR_NONNULL_RET
+ FUNC_ATTR_NONNULL_ALL FUNC_ATTR_NONNULL_RET
{
- // Reverse the pattern.
size_t len = strlen(s);
char *rev = xmalloc(len + 1);
- size_t rev_i = len;
- for (size_t s_i = 0; s_i < len; s_i++) {
+ for (size_t s_i = 0, rev_i = len; s_i < len; s_i++) {
const int mb_len = utfc_ptr2len(s + s_i);
rev_i -= (size_t)mb_len;
memmove(rev + rev_i, s + s_i, (size_t)mb_len);
s_i += (size_t)mb_len - 1;
}
rev[len] = NUL;
-
return rev;
}
diff --git a/test/old/testdir/test_functions.vim b/test/old/testdir/test_functions.vim
index c91e989233..dbd1119350 100644
--- a/test/old/testdir/test_functions.vim
+++ b/test/old/testdir/test_functions.vim
@@ -3148,4 +3148,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(v:_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/test/old/testdir/test_listdict.vim b/test/old/testdir/test_listdict.vim
index 354d987c79..3677125d68 100644
--- a/test/old/testdir/test_listdict.vim
+++ b/test/old/testdir/test_listdict.vim
@@ -894,7 +894,7 @@ func Test_reverse_sort_uniq()
END
call 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:")