summaryrefslogtreecommitdiffstats
path: root/src/eval.c
diff options
context:
space:
mode:
authorBram Moolenaar <Bram@vim.org>2020-08-15 18:39:05 +0200
committerBram Moolenaar <Bram@vim.org>2020-08-15 18:39:05 +0200
commite3c37d8ebf9dbbf210fde4a5fb28eb1f2a492a34 (patch)
tree2f3c7e958f2f422c9165ab686ade094e24f93fc0 /src/eval.c
parent451c2e3536a3cb77d07faf3cb2b834512e174351 (diff)
patch 8.2.1461: Vim9: string indexes are counted in bytesv8.2.1461
Problem: Vim9: string indexes are counted in bytes. Solution: Use character indexes. (closes #6574)
Diffstat (limited to 'src/eval.c')
-rw-r--r--src/eval.c28
1 files changed, 28 insertions, 0 deletions
diff --git a/src/eval.c b/src/eval.c
index 70d5b343dd..5a61a50fa7 100644
--- a/src/eval.c
+++ b/src/eval.c
@@ -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
@@ -5285,6 +5289,30 @@ eval_isdictc(int 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
* - ".name" lookup