aboutsummaryrefslogtreecommitdiff
path: root/src
diff options
context:
space:
mode:
authorzeertzjq <zeertzjq@outlook.com>2023-08-19 18:33:44 +0800
committerGitHub <noreply@github.com>2023-08-19 18:33:44 +0800
commitd9b094660944969b5160f2d22f0c2e8627e10d92 (patch)
tree9fd8d4342f77ed25e5abba5f697a827b3f3de38b /src
parentd7ae9ae3e52362da33902c31ecccc79c49d3866e (diff)
parentfcd729f22c658826024467801ae4ba0a92a8fabc (diff)
downloadrneovim-d9b094660944969b5160f2d22f0c2e8627e10d92.tar.gz
rneovim-d9b094660944969b5160f2d22f0c2e8627e10d92.tar.bz2
rneovim-d9b094660944969b5160f2d22f0c2e8627e10d92.zip
Merge pull request #24787 from zeertzjq/vim-9.0.1515
vim-patch:9.0.{1515,1540,1738}
Diffstat (limited to 'src')
-rw-r--r--src/nvim/eval.lua12
-rw-r--r--src/nvim/eval/funcs.c15
-rw-r--r--src/nvim/eval/typval.c17
-rw-r--r--src/nvim/strings.c7
4 files changed, 37 insertions, 14 deletions
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..250b5c5556 100644
--- a/src/nvim/eval/funcs.c
+++ b/src/nvim/eval/funcs.c
@@ -6199,6 +6199,10 @@ static void f_resolve(typval_T *argvars, typval_T *rettv, EvalFuncData fptr)
/// "reverse({list})" function
static void f_reverse(typval_T *argvars, typval_T *rettv, EvalFuncData fptr)
{
+ if (tv_check_for_string_or_list_or_blob_arg(argvars, 0) == FAIL) {
+ return;
+ }
+
if (argvars[0].v_type == VAR_BLOB) {
blob_T *const b = argvars[0].vval.v_blob;
const int len = tv_blob_len(b);
@@ -6209,9 +6213,14 @@ 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_LIST) {
- semsg(_(e_listblobarg), "reverse()");
- } else {
+ } 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) {
list_T *const l = argvars[0].vval.v_list;
if (!value_check_lock(tv_list_locked(l), N_("reverse() argument"),
TV_TRANSLATE)) {
diff --git a/src/nvim/eval/typval.c b/src/nvim/eval/typval.c
index 5f7bd98298..ba2c23ffe8 100644
--- a/src/nvim/eval/typval.c
+++ b/src/nvim/eval/typval.c
@@ -81,6 +81,8 @@ static const char e_blob_required_for_argument_nr[]
= N_("E1238: Blob required for argument %d");
static const char e_invalid_value_for_blob_nr[]
= N_("E1239: Invalid value for blob: %d");
+static const char e_string_list_or_blob_required_for_argument_nr[]
+ = N_("E1252: String, List or Blob required for argument %d");
static const char e_string_or_function_required_for_argument_nr[]
= N_("E1256: String or function required for argument %d");
static const char e_non_null_dict_required_for_argument_nr[]
@@ -4350,7 +4352,20 @@ int tv_check_for_string_or_list_arg(const typval_T *const args, const int idx)
return OK;
}
-/// Check for an optional string or list argument at 'idx'
+/// Give an error and return FAIL unless "args[idx]" is a string, a list or a blob.
+int tv_check_for_string_or_list_or_blob_arg(const typval_T *const args, const int idx)
+ FUNC_ATTR_NONNULL_ALL FUNC_ATTR_WARN_UNUSED_RESULT FUNC_ATTR_PURE
+{
+ if (args[idx].v_type != VAR_STRING
+ && args[idx].v_type != VAR_LIST
+ && args[idx].v_type != VAR_BLOB) {
+ semsg(_(e_string_list_or_blob_required_for_argument_nr), idx + 1);
+ return FAIL;
+ }
+ return OK;
+}
+
+/// Check for an optional string or list argument at "idx"
int tv_check_for_opt_string_or_list_arg(const typval_T *const args, const int idx)
FUNC_ATTR_NONNULL_ALL FUNC_ATTR_WARN_UNUSED_RESULT FUNC_ATTR_PURE
{
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;
}