0
0
mirror of https://github.com/vim/vim.git synced 2025-10-01 04:54:07 -04:00

patch 8.2.2658: :for cannot loop over a string

Problem:    :for cannot loop over a string.
Solution:   Accept a string argument and iterate over its characters.
This commit is contained in:
Bram Moolenaar
2021-03-26 20:41:29 +01:00
parent 522eefd9a2
commit 74e54fcb44
9 changed files with 164 additions and 33 deletions

View File

@@ -2322,6 +2322,25 @@ def Test_for_loop()
res ..= n .. s
endfor
assert_equal('1a2b', res)
# loop over string
res = ''
for c in 'aéc̀d'
res ..= c .. '-'
endfor
assert_equal('a-é-c̀-d-', res)
res = ''
for c in ''
res ..= c .. '-'
endfor
assert_equal('', res)
res = ''
for c in test_null_string()
res ..= c .. '-'
endfor
assert_equal('', res)
enddef
def Test_for_loop_fails()
@@ -2333,10 +2352,17 @@ def Test_for_loop_fails()
CheckDefFailure(['var x = 5', 'for x in range(5)'], 'E1017:')
CheckScriptFailure(['def Func(arg: any)', 'for arg in range(5)', 'enddef', 'defcompile'], 'E1006:')
delfunc! g:Func
CheckDefFailure(['for i in "text"'], 'E1012:')
CheckDefFailure(['for i in xxx'], 'E1001:')
CheckDefFailure(['endfor'], 'E588:')
CheckDefFailure(['for i in range(3)', 'echo 3'], 'E170:')
# wrong type detected at compile time
CheckDefFailure(['for i in {a: 1}', 'echo 3', 'endfor'], 'E1177: For loop on dict not supported')
# wrong type detected at runtime
g:adict = {a: 1}
CheckDefExecFailure(['for i in g:adict', 'echo 3', 'endfor'], 'E1177: For loop on dict not supported')
unlet g:adict
enddef
def Test_for_loop_script_var()