patch 9.0.1617: charidx() result is not consistent with byteidx()
Problem: charidx() and utf16idx() result is not consistent with byteidx().
Solution: When the index is equal to the length of the text return the
lenght of the text instead of -1. (Yegappan Lakshmanan,
closes #12503)
diff --git a/src/strings.c b/src/strings.c
index 5aa7152..9e2c331 100644
--- a/src/strings.c
+++ b/src/strings.c
@@ -1054,7 +1054,8 @@
if (in_vim9script()
&& (check_for_string_arg(argvars, 0) == FAIL
- || check_for_number_arg(argvars, 1) == FAIL))
+ || check_for_number_arg(argvars, 1) == FAIL
+ || check_for_opt_bool_arg(argvars, 2) == FAIL))
return;
char_u *str = tv_get_string_chk(&argvars[0]);
@@ -1158,7 +1159,14 @@
for (p = str, len = 0; utf16idx ? idx >= 0 : p <= str + idx; len++)
{
if (*p == NUL)
+ {
+ // If the index is exactly the number of bytes or utf-16 code units
+ // in the string then return the length of the string in
+ // characters.
+ if (utf16idx ? (idx == 0) : (p == (str + idx)))
+ rettv->vval.v_number = len;
return;
+ }
if (utf16idx)
{
idx--;
@@ -1775,7 +1783,14 @@
for (p = str, len = 0; charidx ? idx >= 0 : p <= str + idx; len++)
{
if (*p == NUL)
+ {
+ // If the index is exactly the number of bytes or characters in the
+ // string then return the length of the string in utf-16 code
+ // units.
+ if (charidx ? (idx == 0) : (p == (str + idx)))
+ rettv->vval.v_number = len;
return;
+ }
int clen = ptr2len(p);
int c = (clen > 1) ? utf_ptr2char(p) : *p;
if (c > 0xFFFF)