mirror of
https://github.com/neovim/neovim.git
synced 2025-10-17 15:21:47 +00:00
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)03ff1c2dde
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#128474dd266cb66
Co-authored-by: Yegappan Lakshmanan <yegappan@yahoo.com>
This commit is contained in:
@@ -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))
|
||||
<
|
||||
]=],
|
||||
|
@@ -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 {
|
||||
|
@@ -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;
|
||||
}
|
||||
|
||||
|
Reference in New Issue
Block a user