comparison src/eval.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
3716 if (n1 >= len || n2 < 0 || n1 > n2) 3716 if (n1 >= len || n2 < 0 || n1 > n2)
3717 s = NULL; 3717 s = NULL;
3718 else 3718 else
3719 s = vim_strnsave(s + n1, n2 - n1 + 1); 3719 s = vim_strnsave(s + n1, n2 - n1 + 1);
3720 } 3720 }
3721 else if (in_vim9script())
3722 {
3723 s = char_from_string(s, n1);
3724 }
3721 else 3725 else
3722 { 3726 {
3723 // The resulting variable is a string of a single 3727 // The resulting variable is a string of a single
3724 // character. If the index is too big or negative the 3728 // character. If the index is too big or negative the
3725 // result is empty. 3729 // result is empty.
5283 { 5287 {
5284 return ASCII_ISALNUM(c) || c == '_'; 5288 return ASCII_ISALNUM(c) || c == '_';
5285 } 5289 }
5286 5290
5287 /* 5291 /*
5292 * Return the character "str[index]" where "index" is the character index. If
5293 * "index" is out of range NULL is returned.
5294 */
5295 char_u *
5296 char_from_string(char_u *str, varnumber_T index)
5297 {
5298 size_t nbyte = 0;
5299 varnumber_T nchar = index;
5300 size_t slen;
5301
5302 if (str == NULL || index < 0)
5303 return NULL;
5304 slen = STRLEN(str);
5305 while (nchar > 0 && nbyte < slen)
5306 {
5307 nbyte += MB_CPTR2LEN(str + nbyte);
5308 --nchar;
5309 }
5310 if (nbyte >= slen)
5311 return NULL;
5312 return vim_strnsave(str + nbyte, MB_CPTR2LEN(str + nbyte));
5313 }
5314
5315 /*
5288 * Handle: 5316 * Handle:
5289 * - expr[expr], expr[expr:expr] subscript 5317 * - expr[expr], expr[expr:expr] subscript
5290 * - ".name" lookup 5318 * - ".name" lookup
5291 * - function call with Funcref variable: func(expr) 5319 * - function call with Funcref variable: func(expr)
5292 * - method call: var->method() 5320 * - method call: var->method()