comparison src/vim9execute.c @ 21823:b1f3d8a44ab6 v8.2.1461

patch 8.2.1461: Vim9: string indexes are counted in bytes Commit: https://github.com/vim/vim/commit/e3c37d8ebf9dbbf210fde4a5fb28eb1f2a492a34 Author: Bram Moolenaar <Bram@vim.org> Date: Sat Aug 15 18:39:05 2020 +0200 patch 8.2.1461: Vim9: string indexes are counted in bytes Problem: Vim9: string indexes are counted in bytes. Solution: Use character indexes. (closes https://github.com/vim/vim/issues/6574)
author Bram Moolenaar <Bram@vim.org>
date Sat, 15 Aug 2020 18:45:04 +0200
parents 0deb6f96a5a3
children ccad66ac6c3e
comparison
equal deleted inserted replaced
21822:1ff3fa258bf9 21823:b1f3d8a44ab6
2231 } 2231 }
2232 break; 2232 break;
2233 2233
2234 case ISN_STRINDEX: 2234 case ISN_STRINDEX:
2235 { 2235 {
2236 char_u *s;
2237 varnumber_T n; 2236 varnumber_T n;
2238 char_u *res; 2237 char_u *res;
2239 2238
2240 // string index: string is at stack-2, index at stack-1 2239 // string index: string is at stack-2, index at stack-1
2241 tv = STACK_TV_BOT(-2); 2240 tv = STACK_TV_BOT(-2);
2243 { 2242 {
2244 SOURCING_LNUM = iptr->isn_lnum; 2243 SOURCING_LNUM = iptr->isn_lnum;
2245 emsg(_(e_stringreq)); 2244 emsg(_(e_stringreq));
2246 goto on_error; 2245 goto on_error;
2247 } 2246 }
2248 s = tv->vval.v_string;
2249 2247
2250 tv = STACK_TV_BOT(-1); 2248 tv = STACK_TV_BOT(-1);
2251 if (tv->v_type != VAR_NUMBER) 2249 if (tv->v_type != VAR_NUMBER)
2252 { 2250 {
2253 SOURCING_LNUM = iptr->isn_lnum; 2251 SOURCING_LNUM = iptr->isn_lnum;
2257 n = tv->vval.v_number; 2255 n = tv->vval.v_number;
2258 2256
2259 // The resulting variable is a string of a single 2257 // The resulting variable is a string of a single
2260 // character. If the index is too big or negative the 2258 // character. If the index is too big or negative the
2261 // result is empty. 2259 // result is empty.
2262 if (n < 0 || n >= (varnumber_T)STRLEN(s))
2263 res = NULL;
2264 else
2265 res = vim_strnsave(s + n, 1);
2266 --ectx.ec_stack.ga_len; 2260 --ectx.ec_stack.ga_len;
2267 tv = STACK_TV_BOT(-1); 2261 tv = STACK_TV_BOT(-1);
2262 res = char_from_string(tv->vval.v_string, n);
2268 vim_free(tv->vval.v_string); 2263 vim_free(tv->vval.v_string);
2269 tv->vval.v_string = res; 2264 tv->vval.v_string = res;
2270 } 2265 }
2271 break; 2266 break;
2272 2267