Skip to content

Commit 5131c14

Browse files
committed
patch 7.4.1464
Problem: When the argument of sort() is zero or empty it fails. Solution: Make zero work as documented. (suggested by Yasuhiro Matsumoto)
1 parent a6b8976 commit 5131c14

File tree

3 files changed

+22
-2
lines changed

3 files changed

+22
-2
lines changed

src/eval.c

Lines changed: 12 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -19195,11 +19195,21 @@ do_sort_uniq(typval_T *argvars, typval_T *rettv, int sort)
1919519195
goto theend; /* type error; errmsg already given */
1919619196
if (i == 1)
1919719197
info.item_compare_ic = TRUE;
19198-
else
19198+
else if (argvars[1].v_type != VAR_NUMBER)
1919919199
info.item_compare_func = get_tv_string(&argvars[1]);
19200+
else if (i != 0)
19201+
{
19202+
EMSG(_(e_invarg));
19203+
goto theend;
19204+
}
1920019205
if (info.item_compare_func != NULL)
1920119206
{
19202-
if (STRCMP(info.item_compare_func, "n") == 0)
19207+
if (*info.item_compare_func == NUL)
19208+
{
19209+
/* empty string means default sort */
19210+
info.item_compare_func = NULL;
19211+
}
19212+
else if (STRCMP(info.item_compare_func, "n") == 0)
1920319213
{
1920419214
info.item_compare_func = NULL;
1920519215
info.item_compare_numeric = TRUE;

src/testdir/test_sort.vim

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -35,3 +35,11 @@ func Test_sort_nested()
3535
" test ability to call sort() from a compare function
3636
call assert_equal([1, 3, 5], sort([3, 1, 5], 'Compare1'))
3737
endfunc
38+
39+
func Test_sort_default()
40+
" docs say omitted, empty or zero argument sorts on string representation.
41+
call assert_equal(["2", 1, 3.3], sort([3.3, 1, "2"]))
42+
call assert_equal(["2", 1, 3.3], sort([3.3, 1, "2"], ''))
43+
call assert_equal(["2", 1, 3.3], sort([3.3, 1, "2"], 0))
44+
call assert_fails('call sort([3.3, 1, "2"], 3)', "E474")
45+
endfunc

src/version.c

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -743,6 +743,8 @@ static char *(features[]) =
743743

744744
static int included_patches[] =
745745
{ /* Add new patch number below this line */
746+
/**/
747+
1464,
746748
/**/
747749
1463,
748750
/**/

0 commit comments

Comments
 (0)