comparison src/list.c @ 21828:af5db9b6d210

patch 8.2.1463: Vim9: list slice not supported yet Commit: https://github.com/vim/vim/commit/ed5918771fcf9877d8445e74c62ab1ce6b8e28c1 Author: Bram Moolenaar <Bram@vim.org> Date: Sat Aug 15 22:14:53 2020 +0200 patch 8.2.1463: Vim9: list slice not supported yet Problem: Vim9: list slice not supported yet. Solution: Add support for list slicing.
author Bram Moolenaar <Bram@vim.org>
date Sat, 15 Aug 2020 22:15:03 +0200
parents f84625b961a8
children d8422de73113
comparison
equal deleted inserted replaced
21827:fbafd1638f76 21828:af5db9b6d210
884 return NULL; 884 return NULL;
885 } 885 }
886 item = item->li_next; 886 item = item->li_next;
887 } 887 }
888 return l; 888 return l;
889 }
890
891 int
892 list_slice_or_index(
893 list_T *list,
894 int range,
895 long n1_arg,
896 long n2_arg,
897 typval_T *rettv,
898 int verbose)
899 {
900 long len = list_len(list);
901 long n1 = n1_arg;
902 long n2 = n2_arg;
903 typval_T var1;
904
905 if (n1 < 0)
906 n1 = len + n1;
907 if (n1 < 0 || n1 >= len)
908 {
909 // For a range we allow invalid values and return an empty
910 // list. A list index out of range is an error.
911 if (!range)
912 {
913 if (verbose)
914 semsg(_(e_listidx), n1);
915 return FAIL;
916 }
917 n1 = len;
918 }
919 if (range)
920 {
921 list_T *l;
922
923 if (n2 < 0)
924 n2 = len + n2;
925 else if (n2 >= len)
926 n2 = len - 1;
927 if (n2 < 0 || n2 + 1 < n1)
928 n2 = -1;
929 l = list_slice(list, n1, n2);
930 if (l == NULL)
931 return FAIL;
932 clear_tv(rettv);
933 rettv_list_set(rettv, l);
934 }
935 else
936 {
937 // copy the item to "var1" to avoid that freeing the list makes it
938 // invalid.
939 copy_tv(&list_find(list, n1)->li_tv, &var1);
940 clear_tv(rettv);
941 *rettv = var1;
942 }
943 return OK;
889 } 944 }
890 945
891 /* 946 /*
892 * Make a copy of list "orig". Shallow if "deep" is FALSE. 947 * Make a copy of list "orig". Shallow if "deep" is FALSE.
893 * The refcount of the new list is set to 1. 948 * The refcount of the new list is set to 1.