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. --- ChangeLog | 9 +++++++++ lib.c | 61 +++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++ txr.1 | 6 ++++-- 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 + + * 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 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 @@ -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; 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 -- cgit v1.2.3