summaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
authorKaz Kylheku <kaz@kylheku.com>2015-05-14 06:12:30 -0700
committerKaz Kylheku <kaz@kylheku.com>2015-05-14 06:12:30 -0700
commit7d58b9a73f3adc4f665a83f72db2eeae19519a17 (patch)
tree88da00d4228ab900144f00f8dc310ec476744305
parent5a383a288d620b9ae6111d9f33b9e420d3fcd0f8 (diff)
downloadtxr-7d58b9a73f3adc4f665a83f72db2eeae19519a17.tar.gz
txr-7d58b9a73f3adc4f665a83f72db2eeae19519a17.tar.bz2
txr-7d58b9a73f3adc4f665a83f72db2eeae19519a17.zip
* lib.c (replace_list, replace_str, replace_vec): Handle
the case when from is a vector, for consistency with the sel function and the dwim operator. * txr.1: Document that the third argument of select and replace may be a vector.
-rw-r--r--ChangeLog9
-rw-r--r--lib.c61
-rw-r--r--txr.16
3 files changed, 74 insertions, 2 deletions
diff --git a/ChangeLog b/ChangeLog
index 5b68c559..a1b00fd4 100644
--- a/ChangeLog
+++ b/ChangeLog
@@ -1,3 +1,12 @@
+2015-05-14 Kaz Kylheku <kaz@kylheku.com>
+
+ * lib.c (replace_list, replace_str, replace_vec): Handle
+ the case when from is a vector, for consistency with the sel
+ function and the dwim operator.
+
+ * txr.1: Document that the third argument of select and replace may be
+ a vector.
+
2015-05-13 Kaz Kylheku <kaz@kylheku.com>
Recursive lcons force bug (keep-if*, remove-if*).
diff --git a/lib.c b/lib.c
index 28e58240..38eb599b 100644
--- a/lib.c
+++ b/lib.c
@@ -849,6 +849,29 @@ val replace_list(val list, val items, val from, val to)
}
return list;
+ } else if (vectorp(from)) {
+ val where = from;
+ val seq = list;
+ val idx = zero;
+ val wlen = length_vec(where);
+ val widx = zero;
+
+ if (!missingp(to))
+ uw_throwf(error_s,
+ lit("replace-str: to-arg not applicable when from-arg is a vector"),
+ nao);
+
+ for (; seq && items && lt(widx, wlen); seq = cdr(seq), idx = plus(idx, one)) {
+ val wh = nil;
+
+ for (; lt(widx, wlen) && lt(wh = vecref(where, widx), idx); widx = plus(widx, one))
+ ; /* empty */
+
+ if (eql(wh, idx))
+ rplaca(seq, pop(&items));
+ }
+
+ return list;
} else if (null_or_missing_p(from)) {
from = zero;
} else if (from == t) {
@@ -2669,6 +2692,25 @@ val replace_str(val str_in, val items, val from, val to)
}
return str_in;
+ } else if (vectorp(from)) {
+ val where = from;
+ val len = length_str(str_in);
+ val wlen = length_vec(from);
+ val i;
+
+ if (!missingp(to))
+ uw_throwf(error_s,
+ lit("replace-str: to-arg not applicable when from-arg is a vector"),
+ nao);
+
+ for (i = zero; lt(i, wlen) && items; i = plus(i, one)) {
+ val wh = vecref(where, i);
+ if (ge(wh, len))
+ break;
+ chr_str_set(str_in, wh, pop(&items));
+ }
+
+ return str_in;
} else if (null_or_missing_p(from)) {
from = zero;
} else if (from == t) {
@@ -5051,6 +5093,25 @@ val replace_vec(val vec_in, val items, val from, val to)
}
return vec_in;
+ } else if (vectorp(from)) {
+ val where = from;
+ val len = length_vec(vec_in);
+ val wlen = length_vec(from);
+ val i;
+
+ if (!missingp(to))
+ uw_throwf(error_s,
+ lit("replace-vec: to-arg not applicable when from-arg is a vector"),
+ nao);
+
+ for (i = zero; lt(i, wlen) && items; i = plus(i, one)) {
+ val wh = vecref(where, i);
+ if (ge(wh, len))
+ break;
+ set(vecref_l(vec_in, wh), pop(&items));
+ }
+
+ return vec_in;
} else if (null_or_missing_p(from)) {
from = zero;
} else if (from == t) {
diff --git a/txr.1 b/txr.1
index 08c2752a..deba1a76 100644
--- a/txr.1
+++ b/txr.1
@@ -13664,7 +13664,8 @@ which consists of those elements of
.meta object
which are identified by
the indices in
-.metn index-list .
+.metn index-list ,
+which may be a list or a vector.
If
.meta function
@@ -17849,7 +17850,8 @@ with the input sequence.
The
.code replace
function has two invocation styles, distinguished by the
-type of the third argument. If the third argument is a list, then it is deemed to be the
+type of the third argument. If the third argument is a list or vector, then it
+is deemed to be the
.meta index-list
parameter of the second form.
Otherwise, if the third argument is missing, or is not a list, then