diff options
author | Kaz Kylheku <kaz@kylheku.com> | 2015-05-14 06:12:30 -0700 |
---|---|---|
committer | Kaz Kylheku <kaz@kylheku.com> | 2015-05-14 06:12:30 -0700 |
commit | 7d58b9a73f3adc4f665a83f72db2eeae19519a17 (patch) | |
tree | 88da00d4228ab900144f00f8dc310ec476744305 | |
parent | 5a383a288d620b9ae6111d9f33b9e420d3fcd0f8 (diff) | |
download | txr-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-- | ChangeLog | 9 | ||||
-rw-r--r-- | lib.c | 61 | ||||
-rw-r--r-- | txr.1 | 6 |
3 files changed, 74 insertions, 2 deletions
@@ -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*). @@ -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) { @@ -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 |