comparison src/vim9execute.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 b875bbd517a6
children d228ca435f3a
comparison
equal deleted inserted replaced
23603:483d395694b0 23604:1816ea68c022
963 return (long)nbyte; 963 return (long)nbyte;
964 } 964 }
965 965
966 /* 966 /*
967 * Return the slice "str[first:last]" using character indexes. 967 * Return the slice "str[first:last]" using character indexes.
968 * "exclusive" is TRUE for slice().
968 * Return NULL when the result is empty. 969 * Return NULL when the result is empty.
969 */ 970 */
970 char_u * 971 char_u *
971 string_slice(char_u *str, varnumber_T first, varnumber_T last) 972 string_slice(char_u *str, varnumber_T first, varnumber_T last, int exclusive)
972 { 973 {
973 long start_byte, end_byte; 974 long start_byte, end_byte;
974 size_t slen; 975 size_t slen;
975 976
976 if (str == NULL) 977 if (str == NULL)
977 return NULL; 978 return NULL;
978 slen = STRLEN(str); 979 slen = STRLEN(str);
979 start_byte = char_idx2byte(str, slen, first); 980 start_byte = char_idx2byte(str, slen, first);
980 if (start_byte < 0) 981 if (start_byte < 0)
981 start_byte = 0; // first index very negative: use zero 982 start_byte = 0; // first index very negative: use zero
982 if (last == -1) 983 if ((last == -1 && !exclusive) || last == VARNUM_MAX)
983 end_byte = (long)slen; 984 end_byte = (long)slen;
984 else 985 else
985 { 986 {
986 end_byte = char_idx2byte(str, slen, last); 987 end_byte = char_idx2byte(str, slen, last);
987 if (end_byte >= 0 && end_byte < (long)slen) 988 if (!exclusive && end_byte >= 0 && end_byte < (long)slen)
988 // end index is inclusive 989 // end index is inclusive
989 end_byte += MB_CPTR2LEN(str + end_byte); 990 end_byte += MB_CPTR2LEN(str + end_byte);
990 } 991 }
991 992
992 if (start_byte >= (long)slen || end_byte <= start_byte) 993 if (start_byte >= (long)slen || end_byte <= start_byte)
2990 2991
2991 ectx.ec_stack.ga_len -= is_slice ? 2 : 1; 2992 ectx.ec_stack.ga_len -= is_slice ? 2 : 1;
2992 tv = STACK_TV_BOT(-1); 2993 tv = STACK_TV_BOT(-1);
2993 if (is_slice) 2994 if (is_slice)
2994 // Slice: Select the characters from the string 2995 // Slice: Select the characters from the string
2995 res = string_slice(tv->vval.v_string, n1, n2); 2996 res = string_slice(tv->vval.v_string, n1, n2, FALSE);
2996 else 2997 else
2997 // Index: The resulting variable is a string of a 2998 // Index: The resulting variable is a string of a
2998 // single character. If the index is too big or 2999 // single character. If the index is too big or
2999 // negative the result is empty. 3000 // negative the result is empty.
3000 res = char_from_string(tv->vval.v_string, n2); 3001 res = char_from_string(tv->vval.v_string, n2);
3028 } 3029 }
3029 3030
3030 ectx.ec_stack.ga_len -= is_slice ? 2 : 1; 3031 ectx.ec_stack.ga_len -= is_slice ? 2 : 1;
3031 tv = STACK_TV_BOT(-1); 3032 tv = STACK_TV_BOT(-1);
3032 SOURCING_LNUM = iptr->isn_lnum; 3033 SOURCING_LNUM = iptr->isn_lnum;
3033 if (list_slice_or_index(list, is_slice, n1, n2, tv, TRUE) 3034 if (list_slice_or_index(list, is_slice, n1, n2, FALSE,
3034 == FAIL) 3035 tv, TRUE) == FAIL)
3035 goto on_error; 3036 goto on_error;
3036 } 3037 }
3037 break; 3038 break;
3038 3039
3039 case ISN_ANYINDEX: 3040 case ISN_ANYINDEX:
3050 SOURCING_LNUM = iptr->isn_lnum; 3051 SOURCING_LNUM = iptr->isn_lnum;
3051 if (check_can_index(tv, TRUE, TRUE) == FAIL) 3052 if (check_can_index(tv, TRUE, TRUE) == FAIL)
3052 goto on_error; 3053 goto on_error;
3053 var1 = is_slice ? STACK_TV_BOT(-2) : STACK_TV_BOT(-1); 3054 var1 = is_slice ? STACK_TV_BOT(-2) : STACK_TV_BOT(-1);
3054 var2 = is_slice ? STACK_TV_BOT(-1) : NULL; 3055 var2 = is_slice ? STACK_TV_BOT(-1) : NULL;
3055 res = eval_index_inner(tv, is_slice, 3056 res = eval_index_inner(tv, is_slice, var1, var2,
3056 var1, var2, NULL, -1, TRUE); 3057 FALSE, NULL, -1, TRUE);
3057 clear_tv(var1); 3058 clear_tv(var1);
3058 if (is_slice) 3059 if (is_slice)
3059 clear_tv(var2); 3060 clear_tv(var2);
3060 ectx.ec_stack.ga_len -= is_slice ? 2 : 1; 3061 ectx.ec_stack.ga_len -= is_slice ? 2 : 1;
3061 if (res == FAIL) 3062 if (res == FAIL)