mirror of
https://github.com/neovim/neovim.git
synced 2025-02-25 18:55:25 -06:00
vim-patch:8.2.2318: Vim9: string and list index work differently
Problem: Vim9: string and list index work differently.
Solution: Make string index work like list index. (closes vim/vim#7643)
e7525c5520
Co-authored-by: Bram Moolenaar <Bram@vim.org>
This commit is contained in:
parent
a44ac26c75
commit
e4a23b6e0b
@ -7523,16 +7523,30 @@ int check_luafunc_name(const char *const str, const bool paren)
|
|||||||
/// "index" is out of range NULL is returned.
|
/// "index" is out of range NULL is returned.
|
||||||
char *char_from_string(const char *str, varnumber_T index)
|
char *char_from_string(const char *str, varnumber_T index)
|
||||||
{
|
{
|
||||||
size_t nbyte = 0;
|
|
||||||
varnumber_T nchar = index;
|
varnumber_T nchar = index;
|
||||||
|
|
||||||
if (str == NULL || index < 0) {
|
if (str == NULL) {
|
||||||
return NULL;
|
return NULL;
|
||||||
}
|
}
|
||||||
size_t slen = strlen(str);
|
size_t slen = strlen(str);
|
||||||
while (nchar > 0 && nbyte < slen) {
|
|
||||||
|
// do the same as for a list: a negative index counts from the end
|
||||||
|
if (index < 0) {
|
||||||
|
int clen = 0;
|
||||||
|
|
||||||
|
for (size_t nbyte = 0; nbyte < slen; clen++) {
|
||||||
|
nbyte += (size_t)utf_ptr2len(str + nbyte);
|
||||||
|
}
|
||||||
|
nchar = clen + index;
|
||||||
|
if (nchar < 0) {
|
||||||
|
// unlike list: index out of range results in empty string
|
||||||
|
return NULL;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
size_t nbyte = 0;
|
||||||
|
for (; nchar > 0 && nbyte < slen; nchar--) {
|
||||||
nbyte += (size_t)utf_ptr2len(str + nbyte);
|
nbyte += (size_t)utf_ptr2len(str + nbyte);
|
||||||
nchar--;
|
|
||||||
}
|
}
|
||||||
if (nbyte >= slen) {
|
if (nbyte >= slen) {
|
||||||
return NULL;
|
return NULL;
|
||||||
|
@ -859,7 +859,7 @@ int tv_list_slice_or_index(list_T *list, bool range, varnumber_T n1_arg, varnumb
|
|||||||
// A list index out of range is an error.
|
// A list index out of range is an error.
|
||||||
if (!range) {
|
if (!range) {
|
||||||
if (verbose) {
|
if (verbose) {
|
||||||
semsg(_(e_list_index_out_of_range_nr), (int64_t)n1);
|
semsg(_(e_list_index_out_of_range_nr), (int64_t)n1_arg);
|
||||||
}
|
}
|
||||||
return FAIL;
|
return FAIL;
|
||||||
}
|
}
|
||||||
|
Loading…
Reference in New Issue
Block a user