summaryrefslogtreecommitdiffstats
path: root/lib.c
diff options
context:
space:
mode:
authorKaz Kylheku <kaz@kylheku.com>2024-07-10 02:53:57 -0700
committerKaz Kylheku <kaz@kylheku.com>2024-07-10 02:53:57 -0700
commit36a83b0c6ba341377a3fe0595993599d172e70a8 (patch)
treefcb0c75ce183c68382829348f19646c6626b8263 /lib.c
parentfbacd030e6e93b3cbd60763154f96a28360a7a46 (diff)
downloadtxr-36a83b0c6ba341377a3fe0595993599d172e70a8.tar.gz
txr-36a83b0c6ba341377a3fe0595993599d172e70a8.tar.bz2
txr-36a83b0c6ba341377a3fe0595993599d172e70a8.zip
split, split*: fix poor behavior for beyond-length indices.
* lib.c (split_func, split_Star_func): Ignore indices greater than the length of the sequence, the same as negative indices are ignored which don't become nonnegative after adding the length. * tests/012/seq.tl: Fix questionable test cases, which now confirm the right behavior.
Diffstat (limited to 'lib.c')
-rw-r--r--lib.c14
1 files changed, 8 insertions, 6 deletions
diff --git a/lib.c b/lib.c
index 4e21108b..eb4c9b8c 100644
--- a/lib.c
+++ b/lib.c
@@ -4240,19 +4240,20 @@ static val partition_func(val base, val lcons)
static val split_func(val base, val lcons)
{
us_cons_bind (seq, indices, lcons);
- val len = nil;
+ val le = nil;
for (;;) {
if (iter_more(indices)) {
+ val len = if3(le, le, le = length(seq));
val raw_index = iter_item(indices);
val index = if3((!opt_compat || opt_compat > 170) && minusp(raw_index),
- plus(raw_index, if3(len, len, len = length(seq))),
+ plus(raw_index, len),
raw_index);
val index_rebased = minus(index, base);
indices = iter_step(indices);
- if (minusp(index_rebased)) {
+ if (minusp(index_rebased) || gt(index_rebased, len)) {
continue;
} else {
val first = sub(seq, zero, index_rebased);
@@ -4282,19 +4283,20 @@ static val split_func(val base, val lcons)
static val split_star_func(val base, val lcons)
{
us_cons_bind (seq, indices, lcons);
- val len = nil;
+ val le = nil;
for (;;) {
if (iter_more(indices)) {
+ val len = if3(le, le, le = length(seq));
val raw_index = iter_item(indices);
val index = if3((!opt_compat || opt_compat > 170) && minusp(raw_index),
- plus(raw_index, if3(len, len, len = length(seq))),
+ plus(raw_index, len),
raw_index);
val index_rebased = minus(index, base);
indices = iter_step(indices);
- if (minusp(index_rebased)) {
+ if (minusp(index_rebased) || gt(index_rebased, len)) {
continue;
} else {
val first = sub(seq, zero, index_rebased);