comparison runtime/doc/eval.txt @ 21823:b1f3d8a44ab6 v8.2.1461

patch 8.2.1461: Vim9: string indexes are counted in bytes Commit: https://github.com/vim/vim/commit/e3c37d8ebf9dbbf210fde4a5fb28eb1f2a492a34 Author: Bram Moolenaar <Bram@vim.org> Date: Sat Aug 15 18:39:05 2020 +0200 patch 8.2.1461: Vim9: string indexes are counted in bytes Problem: Vim9: string indexes are counted in bytes. Solution: Use character indexes. (closes https://github.com/vim/vim/issues/6574)
author Bram Moolenaar <Bram@vim.org>
date Sat, 15 Aug 2020 18:45:04 +0200
parents 22583b9d4efd
children 0db0640e16e0
comparison
equal deleted inserted replaced
21822:1ff3fa258bf9 21823:b1f3d8a44ab6
1129 expr8->(expr1, ...)[expr1] 1129 expr8->(expr1, ...)[expr1]
1130 Evaluation is always from left to right. 1130 Evaluation is always from left to right.
1131 1131
1132 expr8[expr1] item of String or |List| *expr-[]* *E111* 1132 expr8[expr1] item of String or |List| *expr-[]* *E111*
1133 *E909* *subscript* 1133 *E909* *subscript*
1134 In legacy Vim script:
1134 If expr8 is a Number or String this results in a String that contains the 1135 If expr8 is a Number or String this results in a String that contains the
1135 expr1'th single byte from expr8. expr8 is used as a String, expr1 as a 1136 expr1'th single byte from expr8. expr8 is used as a String (a number is
1136 Number. This doesn't recognize multi-byte encodings, see `byteidx()` for 1137 automatically converted to a String), expr1 as a Number. This doesn't
1137 an alternative, or use `split()` to turn the string into a list of characters. 1138 recognize multi-byte encodings, see `byteidx()` for an alternative, or use
1138 1139 `split()` to turn the string into a list of characters. Example, to get the
1139 Index zero gives the first byte. This is like it works in C. Careful: 1140 byte under the cursor: >
1140 text column numbers start with one! Example, to get the byte under the
1141 cursor: >
1142 :let c = getline(".")[col(".") - 1] 1141 :let c = getline(".")[col(".") - 1]
1142
1143 In Vim9 script:
1144 If expr8 is a String this results in a String that contains the expr1'th
1145 single character from expr8. To use byte indexes use |strpart()|.
1146
1147 Index zero gives the first byte or character. Careful: text column numbers
1148 start with one!
1143 1149
1144 If the length of the String is less than the index, the result is an empty 1150 If the length of the String is less than the index, the result is an empty
1145 String. A negative index always results in an empty string (reason: backward 1151 String. A negative index always results in an empty string (reason: backward
1146 compatibility). Use [-1:] to get the last byte. 1152 compatibility). Use [-1:] to get the last byte or character.
1147 1153
1148 If expr8 is a |List| then it results the item at index expr1. See |list-index| 1154 If expr8 is a |List| then it results the item at index expr1. See |list-index|
1149 for possible index values. If the index is out of range this results in an 1155 for possible index values. If the index is out of range this results in an
1150 error. Example: > 1156 error. Example: >
1151 :let item = mylist[-1] " get last item 1157 :let item = mylist[-1] " get last item
1155 error. 1161 error.
1156 1162
1157 1163
1158 expr8[expr1a : expr1b] substring or sublist *expr-[:]* 1164 expr8[expr1a : expr1b] substring or sublist *expr-[:]*
1159 1165
1160 If expr8 is a Number or String this results in the substring with the bytes 1166 If expr8 is a String this results in the substring with the bytes from expr1a
1161 from expr1a to and including expr1b. expr8 is used as a String, expr1a and 1167 to and including expr1b. expr8 is used as a String, expr1a and expr1b are
1162 expr1b are used as a Number. This doesn't recognize multi-byte encodings, see 1168 used as a Number.
1163 |byteidx()| for computing the indexes. 1169
1170 In legacy Vim script the indexes are byte indexes. This doesn't recognize
1171 multi-byte encodings, see |byteidx()| for computing the indexes. If expr8 is
1172 a Number it is first converted to a String.
1173
1174 In Vim9 script the indexes are character indexes. To use byte indexes use
1175 |strpart()|.
1164 1176
1165 If expr1a is omitted zero is used. If expr1b is omitted the length of the 1177 If expr1a is omitted zero is used. If expr1b is omitted the length of the
1166 string minus one is used. 1178 string minus one is used.
1167 1179
1168 A negative number can be used to measure from the end of the string. -1 is 1180 A negative number can be used to measure from the end of the string. -1 is