summaryrefslogtreecommitdiffstats
path: root/lib.c
diff options
context:
space:
mode:
authorKaz Kylheku <kaz@kylheku.com>2024-07-10 04:15:22 -0700
committerKaz Kylheku <kaz@kylheku.com>2024-07-10 04:15:22 -0700
commita51888496ffbe18b3fcd7b27fec5c743a629a719 (patch)
tree1840aa344821eb04fee84c8081a971ba0b433887 /lib.c
parent36a83b0c6ba341377a3fe0595993599d172e70a8 (diff)
downloadtxr-a51888496ffbe18b3fcd7b27fec5c743a629a719.tar.gz
txr-a51888496ffbe18b3fcd7b27fec5c743a629a719.tar.bz2
txr-a51888496ffbe18b3fcd7b27fec5c743a629a719.zip
partition, split, split*: bug handling negative indices.
* lib.c (partition_func, split_func, split_star_func): When negative indices occur after the sequence has already been shortened, the conversion to positivce must take into account the base. This must be added so that the positive index produced is relative to the original length of the input sequence. When index_rebased is calculated, the base is subtracted out again. If we based the positive index off the shortened length, it's as if we are subtracting base twice. * tests/012/seq.tl: Dubious test cases for split* are replaced with the new results that make sense. Additional test cases are added which cover this issue, for not only split* but split and partition.
Diffstat (limited to 'lib.c')
-rw-r--r--lib.c7
1 files changed, 4 insertions, 3 deletions
diff --git a/lib.c b/lib.c
index eb4c9b8c..2b246e69 100644
--- a/lib.c
+++ b/lib.c
@@ -4205,7 +4205,8 @@ static val partition_func(val base, val lcons)
if (iter_more(indices)) {
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(plus(raw_index, if3(len, len, len = length(seq))),
+ base),
raw_index);
val index_rebased = minus(index, base);
@@ -4247,7 +4248,7 @@ static val split_func(val base, val lcons)
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, len),
+ plus(plus(raw_index, len), base),
raw_index);
val index_rebased = minus(index, base);
@@ -4290,7 +4291,7 @@ static val split_star_func(val base, val lcons)
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, len),
+ plus(plus(raw_index, len), base),
raw_index);
val index_rebased = minus(index, base);