]> git.ipfire.org Git - thirdparty/vim.git/commitdiff
patch 8.2.4455: accepting one and zero for second sort() argument is strange v8.2.4455
authorBram Moolenaar <Bram@vim.org>
Wed, 23 Feb 2022 13:17:47 +0000 (13:17 +0000)
committerBram Moolenaar <Bram@vim.org>
Wed, 23 Feb 2022 13:17:47 +0000 (13:17 +0000)
Problem:    Accepting one and zero for the second sort() argument is strange.
Solution:   Disallow using one and zero in Vim9 script.

runtime/doc/builtin.txt
runtime/menu.vim
src/evalfunc.c
src/list.c
src/testdir/test_listdict.vim
src/version.c

index e7707ca0d59ce53a95f76e1a52830220fa8a7d78..c3bacf940be8a5465359a2671da0e16c4d1dcd0e 100644 (file)
@@ -539,8 +539,8 @@ sin({expr})                 Float   sine of {expr}
 sinh({expr})                   Float   hyperbolic sine of {expr}
 slice({expr}, {start} [, {end}])  String, List or Blob
                                        slice of a String, List or Blob
-sort({list} [, {func} [, {dict}]])
-                               List    sort {list}, using {func} to compare
+sort({list} [, {how} [, {dict}]])
+                               List    sort {list}, compare with {how}
 sound_clear()                  none    stop playing all sounds
 sound_playevent({name} [, {callback}])
                                Number  play an event sound
@@ -8033,21 +8033,22 @@ slice({expr}, {start} [, {end}])                        *slice()*
                        GetList()->slice(offset)
 
 
-sort({list} [, {func} [, {dict}]])                     *sort()* *E702*
+sort({list} [, {how} [, {dict}]])                      *sort()* *E702*
                Sort the items in {list} in-place.  Returns {list}.
 
                If you want a list to remain unmodified make a copy first: >
                        :let sortedlist = sort(copy(mylist))
 
-<              When {func} is omitted, is empty or zero, then sort() uses the
+<              When {how} is omitted or is an string, then sort() uses the
                string representation of each item to sort on.  Numbers sort
                after Strings, |Lists| after Numbers.  For sorting text in the
                current buffer use |:sort|.
 
-               When {func} is given and it is '1' or 'i' then case is
-               ignored.
+               When {how} is given and it is 'i' then case is ignored.
+               In legacy script, for backwards compatibility, the value one
+               can be used to ignore case.  Zero means to not ignore case.
 
-               When {func} is given and it is 'l' then the current collation
+               When {how} is given and it is 'l' then the current collation
                locale is used for ordering. Implementation details: strcoll()
                is used to compare strings. See |:language| check or set the
                collation locale. |v:collate| can also be used to check the
@@ -8064,19 +8065,19 @@ sort({list} [, {func} [, {dict}]])                      *sort()* *E702*
 <                      ['n', 'o', 'O', 'p', 'z', 'รถ'] ~
                This does not work properly on Mac.
 
-               When {func} is given and it is 'n' then all items will be
+               When {how} is given and it is 'n' then all items will be
                sorted numerical (Implementation detail: this uses the
                strtod() function to parse numbers, Strings, Lists, Dicts and
                Funcrefs will be considered as being 0).
 
-               When {func} is given and it is 'N' then all items will be
+               When {how} is given and it is 'N' then all items will be
                sorted numerical. This is like 'n' but a string containing
                digits will be used as the number they represent.
 
-               When {func} is given and it is 'f' then all items will be
+               When {how} is given and it is 'f' then all items will be
                sorted numerical. All values must be a Number or a Float.
 
-               When {func} is a |Funcref| or a function name, this function
+               When {how} is a |Funcref| or a function name, this function
                is called to compare items.  The function is invoked with two
                items as argument and must return zero if they are equal, 1 or
                bigger if the first one sorts after the second one, -1 or
index 12924af6fb34872bead8d8af8261dc1cdf4cb1b0..602c4d545538c874ab1c8426ef2678ee5f3613a8 100644 (file)
@@ -2,7 +2,7 @@
 " You can also use this as a start for your own set of menus.
 "
 " Maintainer:  Bram Moolenaar <Bram@vim.org>
-" Last Change: 2022 Feb 04
+" Last Change: 2022 Feb 23
 
 " Note that ":an" (short for ":anoremenu") is often used to make a menu work
 " in all modes and avoid side effects from mappings defined by the user.
@@ -378,7 +378,7 @@ def s:SetupColorSchemes()
   n += globpath(&packpath, "pack/*/opt/*/colors/*.vim", 1, 1)
 
   # Ignore case for VMS and windows, sort on name
-  var names = sort(map(n, 'substitute(v:val, "\\c.*[/\\\\:\\]]\\([^/\\\\:]*\\)\\.vim", "\\1", "")'), 1)
+  var names = sort(map(n, 'substitute(v:val, "\\c.*[/\\\\:\\]]\\([^/\\\\:]*\\)\\.vim", "\\1", "")'), 'i')
 
   # define all the submenu entries
   var idx = 100
index e02b757e95f159d5655dd6c807cb14a179d4d51d..cea002f11ce5d54fa0f0de3d8d7b087a003bc79a 100644 (file)
@@ -603,7 +603,6 @@ arg_sort_how(type_T *type, type_T *decl_type UNUSED, argcontext_T *context)
 {
     if (type->tt_type == VAR_STRING
            || type->tt_type == VAR_PARTIAL
-           || type->tt_type == VAR_NUMBER  // 1 means ignore case
            || type == &t_unknown
            || type == &t_any)
        return OK;
index 578d6f11554be8473f20ac85699d2d01d5957573..88f3102530fac781df7e4201d36cb9fdb104a0a9 100644 (file)
@@ -2198,7 +2198,8 @@ do_sort_uniq(typval_T *argvars, typval_T *rettv, int sort)
     if (in_vim9script()
            && (check_for_list_arg(argvars, 0) == FAIL
                || (argvars[1].v_type != VAR_UNKNOWN
-                   && check_for_opt_dict_arg(argvars, 2) == FAIL)))
+                   && (check_for_string_or_func_arg(argvars, 1) == FAIL
+                             || check_for_opt_dict_arg(argvars, 2) == FAIL))))
        return;
 
     if (argvars[0].v_type != VAR_LIST)
index 5337254fdb591bf201bad20309fe57b0b4b2a183..89b7f041acf98e47cd84be26f64110f6e78ff24a 100644 (file)
@@ -949,7 +949,7 @@ func Test_reverse_sort_uniq()
         call assert_equal([-1, 'one', 'two', 'three', 'four', 1.0e-15, 0.22, 7, 9, 12, 18, 22, 255], sort(copy(l), 'n'))
 
         LET l = [7, 9, 18, 12, 22, 10.0e-16, -1, 0xff, 0, -0, 0.22, 'bar', 'BAR', 'Bar', 'Foo', 'FOO', 'foo', 'FOOBAR', {}, []]
-        call assert_equal(['bar', 'BAR', 'Bar', 'Foo', 'FOO', 'foo', 'FOOBAR', -1, 0, 0, 0.22, 1.0e-15, 12, 18, 22, 255, 7, 9, [], {}], sort(copy(l), 1))
+        call assert_equal(['bar', 'BAR', 'Bar', 'Foo', 'FOO', 'foo', 'FOOBAR', -1, 0, 0, 0.22, 1.0e-15, 12, 18, 22, 255, 7, 9, [], {}], sort(copy(l), 'i'))
         call assert_equal(['bar', 'BAR', 'Bar', 'Foo', 'FOO', 'foo', 'FOOBAR', -1, 0, 0, 0.22, 1.0e-15, 12, 18, 22, 255, 7, 9, [], {}], sort(copy(l), 'i'))
         call assert_equal(['BAR', 'Bar', 'FOO', 'FOOBAR', 'Foo', 'bar', 'foo', -1, 0, 0, 0.22, 1.0e-15, 12, 18, 22, 255, 7, 9, [], {}], sort(copy(l)))
       endif
@@ -961,6 +961,16 @@ func Test_reverse_sort_uniq()
   call assert_fails("call sort([1, 2], function('min'), 1)", "E715:")
   call assert_fails("call sort([1, 2], function('invalid_func'))", "E700:")
   call assert_fails("call sort([1, 2], function('min'))", "E118:")
+
+  let lines =<< trim END
+    call sort(['a', 'b'], 0)
+  END
+  call v9.CheckDefAndScriptFailure(lines, 'E1256: String or function required for argument 2')
+
+  let lines =<< trim END
+    call sort(['a', 'b'], 1)
+  END
+  call v9.CheckDefAndScriptFailure(lines, 'E1256: String or function required for argument 2')
 endfunc
 
 " reduce a list, blob or string
index 5da1833d2526dd6e221cfcba0c583cb3a20afe46..b5d93fb2a7a71935a2bd8780fbcb6769655dfa42 100644 (file)
@@ -750,6 +750,8 @@ static char *(features[]) =
 
 static int included_patches[] =
 {   /* Add new patch number below this line */
+/**/
+    4455,
 /**/
     4454,
 /**/