0
0
mirror of https://github.com/vim/vim.git synced 2025-09-24 03:44:06 -04:00

patch 8.2.1461: Vim9: string indexes are counted in bytes

Problem:    Vim9: string indexes are counted in bytes.
Solution:   Use character indexes. (closes #6574)
This commit is contained in:
Bram Moolenaar
2020-08-15 18:39:05 +02:00
parent 451c2e3536
commit e3c37d8ebf
6 changed files with 78 additions and 24 deletions

View File

@@ -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
@@ -5284,6 +5288,30 @@ eval_isdictc(int c)
return ASCII_ISALNUM(c) || 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