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
diff --git a/src/proto/eval.pro b/src/proto/eval.pro
index a528d3e..c2df7a2 100644
--- a/src/proto/eval.pro
+++ b/src/proto/eval.pro
@@ -59,6 +59,7 @@
int eval_isnamec(int c);
int eval_isnamec1(int c);
int eval_isdictc(int c);
+char_u *char_from_string(char_u *str, varnumber_T index);
int handle_subscript(char_u **arg, typval_T *rettv, evalarg_T *evalarg, int verbose);
int item_copy(typval_T *from, typval_T *to, int deep, int copyID);
void echo_one(typval_T *rettv, int with_space, int *atstart, int *needclr);
diff --git a/src/testdir/test_vim9_expr.vim b/src/testdir/test_vim9_expr.vim
index 15c26fd..182295c 100644
--- a/src/testdir/test_vim9_expr.vim
+++ b/src/testdir/test_vim9_expr.vim
@@ -2075,12 +2075,28 @@
enddef
def Test_expr7_subscript()
- let text = 'abcdef'
- assert_equal('', text[-1])
- assert_equal('a', text[0])
- assert_equal('e', text[4])
- assert_equal('f', text[5])
- assert_equal('', text[6])
+ let lines =<< trim END
+ let text = 'abcdef'
+ assert_equal('', text[-1])
+ assert_equal('a', text[0])
+ assert_equal('e', text[4])
+ assert_equal('f', text[5])
+ assert_equal('', text[6])
+
+ text = 'ábçdëf'
+ assert_equal('', text[-999])
+ assert_equal('', text[-1])
+ assert_equal('á', text[0])
+ assert_equal('b', text[1])
+ assert_equal('ç', text[2])
+ assert_equal('d', text[3])
+ assert_equal('ë', text[4])
+ assert_equal('f', text[5])
+ assert_equal('', text[6])
+ assert_equal('', text[999])
+ END
+ CheckDefSuccess(lines)
+ CheckScriptSuccess(['vim9script'] + lines)
enddef
def Test_expr7_subscript_linebreak()
diff --git a/src/version.c b/src/version.c
index 06c6995..1353360 100644
--- a/src/version.c
+++ b/src/version.c
@@ -755,6 +755,8 @@
static int included_patches[] =
{ /* Add new patch number below this line */
/**/
+ 1461,
+/**/
1460,
/**/
1459,
diff --git a/src/vim9execute.c b/src/vim9execute.c
index 2945379..5131108 100644
--- a/src/vim9execute.c
+++ b/src/vim9execute.c
@@ -2233,7 +2233,6 @@
case ISN_STRINDEX:
{
- char_u *s;
varnumber_T n;
char_u *res;
@@ -2245,7 +2244,6 @@
emsg(_(e_stringreq));
goto on_error;
}
- s = tv->vval.v_string;
tv = STACK_TV_BOT(-1);
if (tv->v_type != VAR_NUMBER)
@@ -2259,12 +2257,9 @@
// The resulting variable is a string of a single
// character. If the index is too big or negative the
// result is empty.
- if (n < 0 || n >= (varnumber_T)STRLEN(s))
- res = NULL;
- else
- res = vim_strnsave(s + n, 1);
--ectx.ec_stack.ga_len;
tv = STACK_TV_BOT(-1);
+ res = char_from_string(tv->vval.v_string, n);
vim_free(tv->vval.v_string);
tv->vval.v_string = res;
}