Mercurial > vim
comparison src/vim9execute.c @ 21393:320581a133d9 v8.2.1247
patch 8.2.1247: Vim9: cannot index a character in a string
Commit: https://github.com/vim/vim/commit/bf9d8c3765a5255c0a0b577ca2e25d70a8bcb688
Author: Bram Moolenaar <Bram@vim.org>
Date: Sun Jul 19 17:55:44 2020 +0200
patch 8.2.1247: Vim9: cannot index a character in a string
Problem: Vim9: cannot index a character in a string.
Solution: Add ISN_STRINDEX instruction. (closes https://github.com/vim/vim/issues/6478)
author | Bram Moolenaar <Bram@vim.org> |
---|---|
date | Sun, 19 Jul 2020 18:00:04 +0200 |
parents | f25d007f90ac |
children | 5cb6e676defd |
comparison
equal
deleted
inserted
replaced
21392:6b34289ace89 | 21393:320581a133d9 |
---|---|
2120 --ectx.ec_stack.ga_len; | 2120 --ectx.ec_stack.ga_len; |
2121 STACK_TV_BOT(-1)->vval.v_string = res; | 2121 STACK_TV_BOT(-1)->vval.v_string = res; |
2122 } | 2122 } |
2123 break; | 2123 break; |
2124 | 2124 |
2125 case ISN_INDEX: | 2125 case ISN_STRINDEX: |
2126 { | |
2127 char_u *s; | |
2128 varnumber_T n; | |
2129 char_u *res; | |
2130 | |
2131 // string index: string is at stack-2, index at stack-1 | |
2132 tv = STACK_TV_BOT(-2); | |
2133 if (tv->v_type != VAR_STRING) | |
2134 { | |
2135 emsg(_(e_stringreq)); | |
2136 goto on_error; | |
2137 } | |
2138 s = tv->vval.v_string; | |
2139 | |
2140 tv = STACK_TV_BOT(-1); | |
2141 if (tv->v_type != VAR_NUMBER) | |
2142 { | |
2143 emsg(_(e_number_exp)); | |
2144 goto on_error; | |
2145 } | |
2146 n = tv->vval.v_number; | |
2147 | |
2148 // The resulting variable is a string of a single | |
2149 // character. If the index is too big or negative the | |
2150 // result is empty. | |
2151 if (n < 0 || n >= (varnumber_T)STRLEN(s)) | |
2152 res = NULL; | |
2153 else | |
2154 res = vim_strnsave(s + n, 1); | |
2155 --ectx.ec_stack.ga_len; | |
2156 tv = STACK_TV_BOT(-1); | |
2157 vim_free(tv->vval.v_string); | |
2158 tv->vval.v_string = res; | |
2159 } | |
2160 break; | |
2161 | |
2162 case ISN_LISTINDEX: | |
2126 { | 2163 { |
2127 list_T *list; | 2164 list_T *list; |
2128 varnumber_T n; | 2165 varnumber_T n; |
2129 listitem_T *li; | 2166 listitem_T *li; |
2130 typval_T temp_tv; | 2167 typval_T temp_tv; |
2945 case ISN_ADDLIST: smsg("%4d ADDLIST", current); break; | 2982 case ISN_ADDLIST: smsg("%4d ADDLIST", current); break; |
2946 case ISN_ADDBLOB: smsg("%4d ADDBLOB", current); break; | 2983 case ISN_ADDBLOB: smsg("%4d ADDBLOB", current); break; |
2947 | 2984 |
2948 // expression operations | 2985 // expression operations |
2949 case ISN_CONCAT: smsg("%4d CONCAT", current); break; | 2986 case ISN_CONCAT: smsg("%4d CONCAT", current); break; |
2950 case ISN_INDEX: smsg("%4d INDEX", current); break; | 2987 case ISN_STRINDEX: smsg("%4d STRINDEX", current); break; |
2988 case ISN_LISTINDEX: smsg("%4d LISTINDEX", current); break; | |
2951 case ISN_SLICE: smsg("%4d SLICE %lld", | 2989 case ISN_SLICE: smsg("%4d SLICE %lld", |
2952 current, iptr->isn_arg.number); break; | 2990 current, iptr->isn_arg.number); break; |
2953 case ISN_GETITEM: smsg("%4d ITEM %lld", | 2991 case ISN_GETITEM: smsg("%4d ITEM %lld", |
2954 current, iptr->isn_arg.number); break; | 2992 current, iptr->isn_arg.number); break; |
2955 case ISN_MEMBER: smsg("%4d MEMBER", current); break; | 2993 case ISN_MEMBER: smsg("%4d MEMBER", current); break; |