]> granicus.if.org Git - vim/commitdiff
patch 8.2.1461: Vim9: string indexes are counted in bytes v8.2.1461
authorBram Moolenaar <Bram@vim.org>
Sat, 15 Aug 2020 16:39:05 +0000 (18:39 +0200)
committerBram Moolenaar <Bram@vim.org>
Sat, 15 Aug 2020 16:39:05 +0000 (18:39 +0200)
Problem:    Vim9: string indexes are counted in bytes.
Solution:   Use character indexes. (closes #6574)

runtime/doc/eval.txt
src/eval.c
src/proto/eval.pro
src/testdir/test_vim9_expr.vim
src/version.c
src/vim9execute.c

index c579ed2faae8f45ede2fbc8e042606d39164c732..0203788dad3b9b28be3374173c7cbd92d90143b3 100644 (file)
@@ -1131,19 +1131,25 @@ Evaluation is always from left to right.
 
 expr8[expr1]           item of String or |List|        *expr-[]* *E111*
                                                        *E909* *subscript*
+In legacy Vim script:
 If expr8 is a Number or String this results in a String that contains the
-expr1'th single byte from expr8.  expr8 is used as a String, expr1 as a
-Number.  This doesn't recognize multi-byte encodings, see `byteidx()` for
-an alternative, or use `split()` to turn the string into a list of characters.
-
-Index zero gives the first byte.  This is like it works in C.  Careful:
-text column numbers start with one!  Example, to get the byte under the
-cursor: >
+expr1'th single byte from expr8.  expr8 is used as a String (a number is
+automatically converted to a String), expr1 as a Number.  This doesn't
+recognize multi-byte encodings, see `byteidx()` for an alternative, or use
+`split()` to turn the string into a list of characters.  Example, to get the
+byte under the cursor: >
        :let c = getline(".")[col(".") - 1]
 
+In Vim9 script:
+If expr8 is a String this results in a String that contains the expr1'th
+single character from expr8.  To use byte indexes use |strpart()|.
+
+Index zero gives the first byte or character.  Careful: text column numbers
+start with one!
+
 If the length of the String is less than the index, the result is an empty
 String.  A negative index always results in an empty string (reason: backward
-compatibility).  Use [-1:] to get the last byte.
+compatibility).  Use [-1:] to get the last byte or character.
 
 If expr8 is a |List| then it results the item at index expr1.  See |list-index|
 for possible index values.  If the index is out of range this results in an
@@ -1157,10 +1163,16 @@ error.
 
 expr8[expr1a : expr1b] substring or sublist            *expr-[:]*
 
-If expr8 is a Number or String this results in the substring with the bytes
-from expr1a to and including expr1b.  expr8 is used as a String, expr1a and
-expr1b are used as a Number.  This doesn't recognize multi-byte encodings, see
-|byteidx()| for computing the indexes.
+If expr8 is a String this results in the substring with the bytes from expr1a
+to and including expr1b.  expr8 is used as a String, expr1a and expr1b are
+used as a Number.
+
+In legacy Vim script the indexes are byte indexes.  This doesn't recognize
+multi-byte encodings, see |byteidx()| for computing the indexes.  If expr8 is
+a Number it is first converted to a String.
+
+In Vim9 script the indexes are character indexes.  To use byte indexes use
+|strpart()|.
 
 If expr1a is omitted zero is used.  If expr1b is omitted the length of the
 string minus one is used.
index 70d5b343dd507940e2184c41798bed82586e036a..5a61a50fa717f4195af5ec5596084f06f3dfef5b 100644 (file)
@@ -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
@@ -5284,6 +5288,30 @@ eval_isdictc(int c)
     return ASCII_ISALNUM(c) || 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
index a528d3e4356a8920c1124e0e54556709bd42229d..c2df7a253be9c479b89c0eb94a7efb8575043a55 100644 (file)
@@ -59,6 +59,7 @@ char_u *find_name_end(char_u *arg, char_u **expr_start, char_u **expr_end, int f
 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);
index 15c26fd620693903f31a1c64ed01ca2275a68e09..182295cc11c57543ac1b6f7b4686fe1659361ece 100644 (file)
@@ -2075,12 +2075,28 @@ def Test_expr7_trailing()
 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()
index 06c6995c619589de3ae2c596bd73d3432eee9d91..135336037f43cdaad2f82a3fea52713427ebee98 100644 (file)
@@ -754,6 +754,8 @@ static char *(features[]) =
 
 static int included_patches[] =
 {   /* Add new patch number below this line */
+/**/
+    1461,
 /**/
     1460,
 /**/
index 2945379b3058ec4323ccc4061e3eb04110f2f744..5131108cc4786ca5d3b822e456fb3d43f050aa0d 100644 (file)
@@ -2233,7 +2233,6 @@ call_def_function(
 
            case ISN_STRINDEX:
                {
-                   char_u      *s;
                    varnumber_T n;
                    char_u      *res;
 
@@ -2245,7 +2244,6 @@ call_def_function(
                        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 @@ call_def_function(
                    // 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;
                }