Mercurial > vim
comparison src/list.c @ 23604:1816ea68c022 v8.2.2344
patch 8.2.2344: using inclusive index for slice is not always desired
Commit: https://github.com/vim/vim/commit/6601b62943a19d4f8818c3638440663d67a17b6a
Author: Bram Moolenaar <Bram@vim.org>
Date: Wed Jan 13 21:47:15 2021 +0100
patch 8.2.2344: using inclusive index for slice is not always desired
Problem: Using inclusive index for slice is not always desired.
Solution: Add the slice() method, which has an exclusive index. (closes
#7408)
author | Bram Moolenaar <Bram@vim.org> |
---|---|
date | Wed, 13 Jan 2021 22:00:04 +0100 |
parents | 510088f8c66f |
children | 5d77a7587927 |
comparison
equal
deleted
inserted
replaced
23603:483d395694b0 | 23604:1816ea68c022 |
---|---|
903 | 903 |
904 int | 904 int |
905 list_slice_or_index( | 905 list_slice_or_index( |
906 list_T *list, | 906 list_T *list, |
907 int range, | 907 int range, |
908 long n1_arg, | 908 varnumber_T n1_arg, |
909 long n2_arg, | 909 varnumber_T n2_arg, |
910 int exclusive, | |
910 typval_T *rettv, | 911 typval_T *rettv, |
911 int verbose) | 912 int verbose) |
912 { | 913 { |
913 long len = list_len(list); | 914 long len = list_len(list); |
914 long n1 = n1_arg; | 915 varnumber_T n1 = n1_arg; |
915 long n2 = n2_arg; | 916 varnumber_T n2 = n2_arg; |
916 typval_T var1; | 917 typval_T var1; |
917 | 918 |
918 if (n1 < 0) | 919 if (n1 < 0) |
919 n1 = len + n1; | 920 n1 = len + n1; |
920 if (n1 < 0 || n1 >= len) | 921 if (n1 < 0 || n1 >= len) |
934 list_T *l; | 935 list_T *l; |
935 | 936 |
936 if (n2 < 0) | 937 if (n2 < 0) |
937 n2 = len + n2; | 938 n2 = len + n2; |
938 else if (n2 >= len) | 939 else if (n2 >= len) |
939 n2 = len - 1; | 940 n2 = len - (exclusive ? 0 : 1); |
941 if (exclusive) | |
942 --n2; | |
940 if (n2 < 0 || n2 + 1 < n1) | 943 if (n2 < 0 || n2 + 1 < n1) |
941 n2 = -1; | 944 n2 = -1; |
942 l = list_slice(list, n1, n2); | 945 l = list_slice(list, n1, n2); |
943 if (l == NULL) | 946 if (l == NULL) |
944 return FAIL; | 947 return FAIL; |