Patch 9.0.1515

4 views
Skip to first unread message

Bram Moolenaar

unread,
May 6, 2023, 9:08:56 AM5/6/23
to vim...@googlegroups.com

Patch 9.0.1515
Problem: reverse() does not work for a String.
Solution: Implement reverse() for a String. (Yegappan Lakshmanan,
closes #12179)
Files: runtime/doc/builtin.txt, runtime/doc/usr_41.txt, src/list.c,
src/strings.c, src/proto/strings.pro,
src/testdir/test_functions.vim, src/testdir/test_listdict.vim


*** ../vim-9.0.1514/runtime/doc/builtin.txt 2023-05-01 22:36:52.866894676 +0100
--- runtime/doc/builtin.txt 2023-05-06 14:00:50.551311650 +0100
***************
*** 484,490 ****
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
round({expr}) Float round off {expr}
rubyeval({expr}) any evaluate |Ruby| expression
screenattr({row}, {col}) Number attribute at screen position
--- 484,491 ----
repeat({expr}, {count}) List/Blob/String
repeat {expr} {count} times
resolve({filename}) String get filename a shortcut points to
! 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
***************
*** 7396,7406 ****
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: >
:let revlist = reverse(copy(mylist))
< Can also be used as a |method|: >
mylist->reverse()
--- 7405,7417 ----
GetName()->resolve()

reverse({object}) *reverse()*
! 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()
*** ../vim-9.0.1514/runtime/doc/usr_41.txt 2023-04-24 21:09:28.125166628 +0100
--- runtime/doc/usr_41.txt 2023-05-06 14:01:57.679266350 +0100
***************
*** 758,763 ****
--- 759,765 ----
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
***************
*** 796,802 ****
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
uniq() remove copies of repeated adjacent items
split() split a String into a List
join() join List items into a String
--- 798,804 ----
reduce() reduce a List to a value
slice() take a slice of a List
sort() sort a List
! 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
***************
*** 863,868 ****
--- 865,871 ----
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
*** ../vim-9.0.1514/src/list.c 2023-04-16 20:53:50.189171575 +0100
--- src/list.c 2023-05-06 13:56:29.707512133 +0100
***************
*** 2999,3004 ****
--- 2999,3006 ----

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
*** ../vim-9.0.1514/src/strings.c 2023-04-24 21:09:28.125166628 +0100
--- src/strings.c 2023-05-06 14:04:58.599153491 +0100
***************
*** 855,860 ****
--- 855,901 ----
}

/*
+ * 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.
*/
*** ../vim-9.0.1514/src/proto/strings.pro 2023-04-24 21:09:28.125166628 +0100
--- src/proto/strings.pro 2023-05-06 14:05:18.859141558 +0100
***************
*** 23,28 ****
--- 23,29 ----
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);
*** ../vim-9.0.1514/src/testdir/test_functions.vim 2023-04-24 21:09:28.125166628 +0100
--- src/testdir/test_functions.vim 2023-05-06 13:56:29.707512133 +0100
***************
*** 3469,3472 ****
--- 3469,3489 ----
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
*** ../vim-9.0.1514/src/testdir/test_listdict.vim 2023-04-01 22:24:15.213814387 +0100
--- src/testdir/test_listdict.vim 2023-05-06 13:56:29.707512133 +0100
***************
*** 981,987 ****
END
call v9.CheckLegacyAndVim9Success(lines)

! 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:")
--- 981,987 ----
END
call v9.CheckLegacyAndVim9Success(lines)

! 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:")
*** ../vim-9.0.1514/src/version.c 2023-05-06 13:01:24.394980077 +0100
--- src/version.c 2023-05-06 13:58:01.587436339 +0100
***************
*** 697,698 ****
--- 697,700 ----
{ /* Add new patch number below this line */
+ /**/
+ 1515,
/**/

--
A)bort, R)etry, B)ang it with a large hammer

/// Bram Moolenaar -- Br...@Moolenaar.net -- http://www.Moolenaar.net \\\
/// \\\
\\\ sponsor Vim, vote for features -- http://www.Vim.org/sponsor/ ///
\\\ help me help AIDS victims -- http://ICCF-Holland.org ///
Reply all
Reply to author
Forward
0 new messages