From 7d58b9a73f3adc4f665a83f72db2eeae19519a17 Mon Sep 17 00:00:00 2001 From: Kaz Kylheku Date: Thu, 14 May 2015 06:12:30 -0700 Subject: * 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. --- lib.c | 61 +++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 61 insertions(+) (limited to 'lib.c') diff --git a/lib.c b/lib.c index 28e58240..38eb599b 100644 --- a/lib.c +++ b/lib.c @@ -848,6 +848,29 @@ val replace_list(val list, val items, val from, val to) rplaca(seq, pop(&items)); } + 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; @@ -2668,6 +2691,25 @@ val replace_str(val str_in, val items, val from, val to) chr_str_set(str_in, wh, pop(&items)); } + 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; @@ -5050,6 +5092,25 @@ val replace_vec(val vec_in, val items, val from, val to) set(vecref_l(vec_in, wh), pop(&items)); } + 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; -- cgit v1.2.3