summaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
-rw-r--r--ChangeLog9
-rw-r--r--arith.c2
-rw-r--r--lib.c42
3 files changed, 37 insertions, 16 deletions
diff --git a/ChangeLog b/ChangeLog
index 7d1199a2..16085bf5 100644
--- a/ChangeLog
+++ b/ChangeLog
@@ -1,5 +1,14 @@
2012-01-25 Kaz Kylheku <kaz@kylheku.com>
+ * arith.c (zerop): Misspelling in error message.
+
+ * lib.c (sub_list, replace_list, sub_vec, replace_vec):
+ Allow the value t to specify one element past the end, so that t t
+ refers to zero-length sequence just past the end of the array or list.
+ Also, fixed out of bounds memmoves in replace_vec.
+
+2012-01-25 Kaz Kylheku <kaz@kylheku.com>
+
* eval.c (eval_init): New functions registered.
* lib.c (sub_list, replace_list, vectorp): New functions.
diff --git a/arith.c b/arith.c
index 6f19a63e..d736e925 100644
--- a/arith.c
+++ b/arith.c
@@ -743,7 +743,7 @@ val zerop(val num)
return t;
if (!fixnump(num) && !bignump(num))
- uw_throwf(error_s, lit("zerof: ~s is not a number"), num, nao);
+ uw_throwf(error_s, lit("zerop: ~s is not a number"), num, nao);
return nil;
}
diff --git a/lib.c b/lib.c
index 496b0cdc..78e77f57 100644
--- a/lib.c
+++ b/lib.c
@@ -416,19 +416,23 @@ val sub_list(val list, val from, val to)
if (from == nil)
from = zero;
+ else if (from == t)
+ from = nil;
else if (lt(from, zero))
- from = plus(from, len ? len : len = length(list));
+ from = plus(from, len = length(list));
- if (to && lt(to, zero))
- to = plus(to, len ? len : len = length(list));
+ if (to == t)
+ to = nil;
+ else if (to && lt(to, zero))
+ to = plus(to, if3(len, len, len = length(list)));
- if (to && gt(from, to)) {
+ if (to && from && gt(from, to)) {
return nil;
} else if (!to || (len && ge(to, len))) {
val iter, i;
for (i = zero, iter = list; iter; iter = cdr(iter), i = plus(i, one)) {
- if (ge(i, from))
+ if (from && ge(i, from))
break;
}
return iter;
@@ -439,7 +443,7 @@ val sub_list(val list, val from, val to)
for (i = zero, iter = list; iter; iter = cdr(iter), i = plus(i, one)) {
if (ge(i, to))
break;
- if (ge(i, from))
+ if (from && ge(i, from))
list_collect(ptail, car(iter));
}
@@ -456,21 +460,25 @@ val replace_list(val list, val from, val to, val items)
if (from == nil)
from = zero;
+ else if (from == t)
+ from = nil;
else if (lt(from, zero))
from = plus(from, len ? len : len = length(list));
+ if (to == t)
+ to = nil;
if (to && lt(to, zero))
to = plus(to, len ? len : len = length(list));
if (!to || (len && ge(to, len))) {
- if (zerop(from)) {
+ if (from && zerop(from)) {
return (listp(items)) ? items : list_vector(items);
} else {
val iter, i;
list_collect_decl (out, ptail);
for (i = zero, iter = list; iter; iter = cdr(iter), i = plus(i, one)) {
- if (ge(i, from))
+ if (from && ge(i, from))
break;
list_collect (ptail, car(iter));
}
@@ -485,7 +493,7 @@ val replace_list(val list, val from, val to, val items)
for (i = zero, iter = list; iter; iter = cdr(iter), i = plus(i, one)) {
if (ge(i, to))
break;
- if (lt(i, from))
+ if (from && lt(i, from))
list_collect(ptail, car(iter));
}
@@ -2689,11 +2697,13 @@ val sub_vec(val vec_in, val from, val to)
if (from == nil)
from = zero;
+ else if (from == t)
+ from = len;
else if (lt(from, zero))
from = plus(from, len);
- if (to == nil)
- to = length_vec(vec_in);
+ if (to == nil || to == t)
+ to = len;
else if (lt(to, zero))
to = plus(to, len);
@@ -2727,11 +2737,13 @@ val replace_vec(val vec_in, val from, val to, val items)
if (from == nil)
from = zero;
+ else if (from == t)
+ from = len;
else if (lt(from, zero))
from = plus(from, len);
- if (to == nil)
- to = length_vec(vec_in);
+ if (to == nil || to == t)
+ to = len;
else if (lt(to, zero))
to = plus(to, len);
@@ -2747,7 +2759,7 @@ val replace_vec(val vec_in, val from, val to, val items)
memmove(vec_in->v.vec + t - c_num(len_diff),
vec_in->v.vec + t,
- l * sizeof vec_in->v.vec);
+ (l - t) * sizeof vec_in->v.vec);
vec_in->v.vec[vec_length] = minus(len, len_diff);
to = plus(from, len_it);
@@ -2760,7 +2772,7 @@ val replace_vec(val vec_in, val from, val to, val items)
memmove(vec_in->v.vec + t + c_num(len_diff),
vec_in->v.vec + t,
- l * sizeof vec_in->v.vec);
+ (l - t) * sizeof vec_in->v.vec);
to = plus(from, len_it);
}