vim-patch:8.2.1461: Vim9: string indexes are counted in bytes

Problem:    Vim9: string indexes are counted in bytes.
Solution:   Use character indexes. (closes vim/vim#6574)

e3c37d8ebf

Co-authored-by: Bram Moolenaar <Bram@vim.org>
This commit is contained in:
zeertzjq
2023-05-04 17:10:58 +08:00
parent 38f9ee6366
commit dd3d857c39
2 changed files with 23 additions and 0 deletions

View File

@@ -7264,6 +7264,27 @@ int check_luafunc_name(const char *const str, const bool paren)
return (int)(p - str);
}
/// Return the character "str[index]" where "index" is the character index. If
/// "index" is out of range NULL is returned.
char *char_from_string(char *str, varnumber_T index)
{
size_t nbyte = 0;
varnumber_T nchar = index;
if (str == NULL || index < 0) {
return NULL;
}
size_t slen = strlen(str);
while (nchar > 0 && nbyte < slen) {
nbyte += (size_t)utf_ptr2len(str + nbyte);
nchar--;
}
if (nbyte >= slen) {
return NULL;
}
return xstrnsave(str + nbyte, (size_t)utf_ptr2len(str + nbyte));
}
/// Handle:
/// - expr[expr], expr[expr:expr] subscript
/// - ".name" lookup