Mercurial > vim
diff 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 |
line wrap: on
line diff
--- a/src/eval.c +++ b/src/eval.c @@ -3718,6 +3718,10 @@ eval_index( else s = vim_strnsave(s + n1, n2 - n1 + 1); } + else if (in_vim9script()) + { + s = char_from_string(s, n1); + } else { // The resulting variable is a string of a single @@ -5285,6 +5289,30 @@ eval_isdictc(int c) } /* + * Return the character "str[index]" where "index" is the character index. If + * "index" is out of range NULL is returned. + */ + char_u * +char_from_string(char_u *str, varnumber_T index) +{ + size_t nbyte = 0; + varnumber_T nchar = index; + size_t slen; + + if (str == NULL || index < 0) + return NULL; + slen = STRLEN(str); + while (nchar > 0 && nbyte < slen) + { + nbyte += MB_CPTR2LEN(str + nbyte); + --nchar; + } + if (nbyte >= slen) + return NULL; + return vim_strnsave(str + nbyte, MB_CPTR2LEN(str + nbyte)); +} + +/* * Handle: * - expr[expr], expr[expr:expr] subscript * - ".name" lookup