summaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
authorzeertzjq <zeertzjq@outlook.com>2024-03-23 08:23:48 +0100
committerChristian Brabandt <cb@256bit.org>2024-03-23 08:25:41 +0100
commitad38769030b5fa86aa0e8f1f0b4266690dfad4c9 (patch)
tree2047fc01b8c70326d9b87b47a575e7e5f2141b62
parentd990bf08d85d83e14fc51fd99a66ebe2f36d2fcd (diff)
patch 9.1.0199: Not enough tests for the slice() functionv9.1.0199
Problem: Not enough tests for the slice() function. Solution: Test with multibyte chars, and in both Legacy and Vim9 script. Update docs to be clearer about how it treats composing chars. (zeertzjq) closes: #14275 Signed-off-by: zeertzjq <zeertzjq@outlook.com> Signed-off-by: Christian Brabandt <cb@256bit.org>
-rw-r--r--runtime/doc/builtin.txt11
-rw-r--r--src/testdir/test_functions.vim51
-rw-r--r--src/testdir/test_vim9_builtin.vim20
-rw-r--r--src/version.c2
4 files changed, 59 insertions, 25 deletions
diff --git a/runtime/doc/builtin.txt b/runtime/doc/builtin.txt
index c857fbc3de..ec702201d5 100644
--- a/runtime/doc/builtin.txt
+++ b/runtime/doc/builtin.txt
@@ -1,4 +1,4 @@
-*builtin.txt* For Vim version 9.1. Last change: 2024 Mar 14
+*builtin.txt* For Vim version 9.1. Last change: 2024 Mar 23
VIM REFERENCE MANUAL by Bram Moolenaar
@@ -9068,7 +9068,8 @@ slice({expr}, {start} [, {end}]) *slice()*
Similar to using a |slice| "expr[start : end]", but "end" is
used exclusive. And for a string the indexes are used as
character indexes instead of byte indexes, like in
- |vim9script|. Also, composing characters are not counted.
+ |vim9script|. Also, composing characters are treated as a
+ part of the preceding base character.
When {end} is omitted the slice continues to the last item.
When {end} is -1 the last item is omitted.
Returns an empty value if {start} or {end} are invalid.
@@ -9465,8 +9466,8 @@ strcharpart({src}, {start} [, {len} [, {skipcc}]]) *strcharpart()*
of byte index and length.
When {skipcc} is omitted or zero, composing characters are
counted separately.
- When {skipcc} set to 1, Composing characters are ignored,
- similar to |slice()|.
+ When {skipcc} set to 1, composing characters are treated as a
+ part of the preceding base character, similar to |slice()|.
When a character index is used where a character does not
exist it is omitted and counted as one character. For
example: >
@@ -9484,7 +9485,7 @@ strchars({string} [, {skipcc}]) *strchars()*
in String {string}.
When {skipcc} is omitted or zero, composing characters are
counted separately.
- When {skipcc} set to 1, Composing characters are ignored.
+ When {skipcc} set to 1, composing characters are ignored.
|strcharlen()| always does this.
Returns zero on error.
diff --git a/src/testdir/test_functions.vim b/src/testdir/test_functions.vim
index 85ccc78b8f..fecebc49da 100644
--- a/src/testdir/test_functions.vim
+++ b/src/testdir/test_functions.vim
@@ -3953,4 +3953,55 @@ func Test_glob_extended_mswin()
call delete('Xtestglob', 'rf')
endfunc
+" Tests for the slice() function.
+func Test_slice()
+ let lines =<< trim END
+ call assert_equal([1, 2, 3, 4, 5], slice(range(6), 1))
+ call assert_equal([2, 3, 4, 5], slice(range(6), 2))
+ call assert_equal([2, 3], slice(range(6), 2, 4))
+ call assert_equal([0, 1, 2, 3], slice(range(6), 0, 4))
+ call assert_equal([1, 2, 3], slice(range(6), 1, 4))
+ call assert_equal([1, 2, 3, 4], slice(range(6), 1, -1))
+ call assert_equal([1, 2], slice(range(6), 1, -3))
+ call assert_equal([1], slice(range(6), 1, -4))
+ call assert_equal([], slice(range(6), 1, -5))
+ call assert_equal([], slice(range(6), 1, -6))
+
+ call assert_equal(0z1122334455, slice(0z001122334455, 1))
+ call assert_equal(0z22334455, slice(0z001122334455, 2))
+ call assert_equal(0z2233, slice(0z001122334455, 2, 4))
+ call assert_equal(0z00112233, slice(0z001122334455, 0, 4))
+ call assert_equal(0z112233, slice(0z001122334455, 1, 4))
+ call assert_equal(0z11223344, slice(0z001122334455, 1, -1))
+ call assert_equal(0z1122, slice(0z001122334455, 1, -3))
+ call assert_equal(0z11, slice(0z001122334455, 1, -4))
+ call assert_equal(0z, slice(0z001122334455, 1, -5))
+ call assert_equal(0z, slice(0z001122334455, 1, -6))
+
+ call assert_equal('12345', slice('012345', 1))
+ call assert_equal('2345', slice('012345', 2))
+ call assert_equal('23', slice('012345', 2, 4))
+ call assert_equal('0123', slice('012345', 0, 4))
+ call assert_equal('123', slice('012345', 1, 4))
+ call assert_equal('1234', slice('012345', 1, -1))
+ call assert_equal('12', slice('012345', 1, -3))
+ call assert_equal('1', slice('012345', 1, -4))
+ call assert_equal('', slice('012345', 1, -5))
+ call assert_equal('', slice('012345', 1, -6))
+
+ #" Composing chars are treated as a part of the preceding base char.
+ call assert_equal('β̳́γ̳̂δ̳̃ε̳̄ζ̳̅', 'ὰ̳β̳́γ̳̂δ̳̃ε̳̄ζ̳̅'->slice(1))
+ call assert_equal('γ̳̂δ̳̃ε̳̄ζ̳̅', 'ὰ̳β̳́γ̳̂δ̳̃ε̳̄ζ̳̅'->slice(2))
+ call assert_equal('γ̳̂δ̳̃', 'ὰ̳β̳́γ̳̂δ̳̃ε̳̄ζ̳̅'->slice(2, 4))
+ call assert_equal('ὰ̳β̳́γ̳̂δ̳̃', 'ὰ̳β̳́γ̳̂δ̳̃ε̳̄ζ̳̅'->slice(0, 4))
+ call assert_equal('β̳́γ̳̂δ̳̃', 'ὰ̳β̳́γ̳̂δ̳̃ε̳̄ζ̳̅'->slice(1, 4))
+ call assert_equal('β̳́γ̳̂δ̳̃ε̳̄', 'ὰ̳β̳́γ̳̂δ̳̃ε̳̄ζ̳̅'->slice(1, -1))
+ call assert_equal('β̳́γ̳̂', 'ὰ̳β̳́γ̳̂δ̳̃ε̳̄ζ̳̅'->slice(1, -3))
+ call assert_equal('β̳́', 'ὰ̳β̳́γ̳̂δ̳̃ε̳̄ζ̳̅'->slice(1, -4))
+ call assert_equal('', 'ὰ̳β̳́γ̳̂δ̳̃ε̳̄ζ̳̅'->slice(1, -5))
+ call assert_equal('', 'ὰ̳β̳́γ̳̂δ̳̃ε̳̄ζ̳̅'->slice(1, -6))
+ END
+ call v9.CheckLegacyAndVim9Success(lines)
+endfunc
+
" vim: shiftwidth=2 sts=2 expandtab
diff --git a/src/testdir/test_vim9_builtin.vim b/src/testdir/test_vim9_builtin.vim
index afb8cb9100..442d375feb 100644
--- a/src/testdir/test_vim9_builtin.vim
+++ b/src/testdir/test_vim9_builtin.vim
@@ -4101,30 +4101,10 @@ def Test_simplify()
enddef
def Test_slice()
- assert_equal('12345', slice('012345', 1))
- assert_equal('123', slice('012345', 1, 4))
- assert_equal('1234', slice('012345', 1, -1))
- assert_equal('1', slice('012345', 1, -4))
- assert_equal('', slice('012345', 1, -5))
- assert_equal('', slice('012345', 1, -6))
-
- assert_equal([1, 2, 3, 4, 5], slice(range(6), 1))
- assert_equal([1, 2, 3], slice(range(6), 1, 4))
- assert_equal([1, 2, 3, 4], slice(range(6), 1, -1))
- assert_equal([1], slice(range(6), 1, -4))
- assert_equal([], slice(range(6), 1, -5))
- assert_equal([], slice(range(6), 1, -6))
-
var lds: list<dict<string>> = [{key: 'value'}]
assert_equal(['val'], lds->slice(0, 1)->map((_, v) => 'val'))
assert_equal(['val'], lds[ : ]->map((_, v) => 'val'))
- assert_equal(0z1122334455, slice(0z001122334455, 1))
- assert_equal(0z112233, slice(0z001122334455, 1, 4))
- assert_equal(0z11223344, slice(0z001122334455, 1, -1))
- assert_equal(0z11, slice(0z001122334455, 1, -4))
- assert_equal(0z, slice(0z001122334455, 1, -5))
- assert_equal(0z, slice(0z001122334455, 1, -6))
v9.CheckDefAndScriptFailure(['slice({"a": 10}, 1)'], ['E1013: Argument 1: type mismatch, expected list<any> but got dict<number>', 'E1211: List required for argument 1'])
v9.CheckDefAndScriptFailure(['slice([1, 2, 3], "b")'], ['E1013: Argument 2: type mismatch, expected number but got string', 'E1210: Number required for argument 2'])
v9.CheckDefAndScriptFailure(['slice("abc", 1, "c")'], ['E1013: Argument 3: type mismatch, expected number but got string', 'E1210: Number required for argument 3'])
diff --git a/src/version.c b/src/version.c
index 4eec075ca2..32037e3778 100644
--- a/src/version.c
+++ b/src/version.c
@@ -705,6 +705,8 @@ static char *(features[]) =
static int included_patches[] =
{ /* Add new patch number below this line */
/**/
+ 199,
+/**/
198,
/**/
197,