diff options
author | zeertzjq <zeertzjq@outlook.com> | 2023-05-04 17:10:58 +0800 |
---|---|---|
committer | zeertzjq <zeertzjq@outlook.com> | 2023-05-04 17:16:23 +0800 |
commit | dd3d857c390bee1f467eda9ec82c29948672bbff (patch) | |
tree | f93f3db6ff9d7445706ceea87f10927c76d61ee8 | |
parent | 38f9ee63660740ce1976173a8513b57bc41d94a3 (diff) | |
download | rneovim-dd3d857c390bee1f467eda9ec82c29948672bbff.tar.gz rneovim-dd3d857c390bee1f467eda9ec82c29948672bbff.tar.bz2 rneovim-dd3d857c390bee1f467eda9ec82c29948672bbff.zip |
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)
https://github.com/vim/vim/commit/e3c37d8ebf9dbbf210fde4a5fb28eb1f2a492a34
Co-authored-by: Bram Moolenaar <Bram@vim.org>
-rw-r--r-- | runtime/doc/eval.txt | 2 | ||||
-rw-r--r-- | src/nvim/eval.c | 21 |
2 files changed, 23 insertions, 0 deletions
diff --git a/runtime/doc/eval.txt b/runtime/doc/eval.txt index 0c18fd5b4e..9ee38e06d6 100644 --- a/runtime/doc/eval.txt +++ b/runtime/doc/eval.txt @@ -1152,6 +1152,8 @@ text column numbers start with one! Example, to get the byte under the cursor: > :let c = getline(".")[col(".") - 1] +Index zero gives the first byte. Careful: text column numbers start with one! + If the length of the String is less than the index, the result is an empty String. A negative index always results in an empty string (reason: backward compatibility). Use [-1:] to get the last byte. diff --git a/src/nvim/eval.c b/src/nvim/eval.c index 941bb8b2b3..b06c102e2c 100644 --- a/src/nvim/eval.c +++ b/src/nvim/eval.c @@ -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 |