patch 8.2.1461: Vim9: string indexes are counted in bytes
Problem: Vim9: string indexes are counted in bytes.
Solution: Use character indexes. (closes #6574)
diff --git a/src/eval.c b/src/eval.c
index 70d5b34..5a61a50 100644
--- a/src/eval.c
+++ b/src/eval.c
@@ -3718,6 +3718,10 @@
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 @@
}
/*
+ * 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