diff options
author | zeertzjq <zeertzjq@outlook.com> | 2024-03-23 08:07:13 +0800 |
---|---|---|
committer | zeertzjq <zeertzjq@outlook.com> | 2024-03-23 08:10:17 +0800 |
commit | d443b438f69d71635fcc5451556a372567ef8c76 (patch) | |
tree | 59eb083e8c694eda41d4fb5f7a1678bc9526be46 /src/nvim/eval.c | |
parent | e4a23b6e0b4f68c284b9cc479df842f934b5ac05 (diff) | |
download | rneovim-d443b438f69d71635fcc5451556a372567ef8c76.tar.gz rneovim-d443b438f69d71635fcc5451556a372567ef8c76.tar.bz2 rneovim-d443b438f69d71635fcc5451556a372567ef8c76.zip |
vim-patch:8.2.2605: Vim9: string index and slice does not include composing chars
Problem: Vim9: string index and slice does not include composing chars.
Solution: Include composing characters. (issue vim/vim#6563)
https://github.com/vim/vim/commit/0289a093a4d65c6280a3be118d1d3696d1aa74da
Co-authored-by: Bram Moolenaar <Bram@vim.org>
Diffstat (limited to 'src/nvim/eval.c')
-rw-r--r-- | src/nvim/eval.c | 20 |
1 files changed, 11 insertions, 9 deletions
diff --git a/src/nvim/eval.c b/src/nvim/eval.c index 206ff93dfe..9e15ea9369 100644 --- a/src/nvim/eval.c +++ b/src/nvim/eval.c @@ -7519,8 +7519,9 @@ 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. +/// Return the character "str[index]" where "index" is the character index, +/// including composing characters. +/// If "index" is out of range NULL is returned. char *char_from_string(const char *str, varnumber_T index) { varnumber_T nchar = index; @@ -7535,7 +7536,7 @@ char *char_from_string(const char *str, varnumber_T index) int clen = 0; for (size_t nbyte = 0; nbyte < slen; clen++) { - nbyte += (size_t)utf_ptr2len(str + nbyte); + nbyte += (size_t)utfc_ptr2len(str + nbyte); } nchar = clen + index; if (nchar < 0) { @@ -7546,16 +7547,16 @@ char *char_from_string(const char *str, varnumber_T index) size_t nbyte = 0; for (; nchar > 0 && nbyte < slen; nchar--) { - nbyte += (size_t)utf_ptr2len(str + nbyte); + nbyte += (size_t)utfc_ptr2len(str + nbyte); } if (nbyte >= slen) { return NULL; } - return xmemdupz(str + nbyte, (size_t)utf_ptr2len(str + nbyte)); + return xmemdupz(str + nbyte, (size_t)utfc_ptr2len(str + nbyte)); } /// Get the byte index for character index "idx" in string "str" with length -/// "str_len". +/// "str_len". Composing characters are included. /// If going over the end return "str_len". /// If "idx" is negative count from the end, -1 is the last character. /// When going over the start return -1. @@ -7566,7 +7567,7 @@ static ssize_t char_idx2byte(const char *str, size_t str_len, varnumber_T idx) if (nchar >= 0) { while (nchar > 0 && nbyte < str_len) { - nbyte += (size_t)utf_ptr2len(str + nbyte); + nbyte += (size_t)utfc_ptr2len(str + nbyte); nchar--; } } else { @@ -7583,7 +7584,8 @@ static ssize_t char_idx2byte(const char *str, size_t str_len, varnumber_T idx) return (ssize_t)nbyte; } -/// Return the slice "str[first:last]" using character indexes. +/// Return the slice "str[first : last]" using character indexes. Composing +/// characters are included. /// /// @param exclusive true for slice(). /// @@ -7605,7 +7607,7 @@ char *string_slice(const char *str, varnumber_T first, varnumber_T last, bool ex end_byte = char_idx2byte(str, slen, last); if (!exclusive && end_byte >= 0 && end_byte < (ssize_t)slen) { // end index is inclusive - end_byte += utf_ptr2len(str + end_byte); + end_byte += utfc_ptr2len(str + end_byte); } } |