diff options
author | Kaz Kylheku <kaz@kylheku.com> | 2012-01-26 20:35:36 -0800 |
---|---|---|
committer | Kaz Kylheku <kaz@kylheku.com> | 2012-01-26 20:35:36 -0800 |
commit | b4bca7645111c277afa6f6cde03d0f51592b4748 (patch) | |
tree | 813f8964483d3ea4fcf22dc2a61f7e6c1450553e /lib.c | |
parent | d011fda9b6b078f09027eb65d500c8beffc99414 (diff) | |
download | txr-b4bca7645111c277afa6f6cde03d0f51592b4748.tar.gz txr-b4bca7645111c277afa6f6cde03d0f51592b4748.tar.bz2 txr-b4bca7645111c277afa6f6cde03d0f51592b4748.zip |
* lib.c (replace_list): Always convert the input items to a list,
even in the trivial case that an empty list is being replaced.
Allow a string to be the replacement (split into a list of
characters).
(replace_str): Bugfix in assignment from vector; wrong index
used over source vector.
(split_str): If the splitting set is empty, just split the
string into characters instead of getting into an infinite loop.
(replace_vec): Allow replacement source to be a string.
Diffstat (limited to 'lib.c')
-rw-r--r-- | lib.c | 46 |
1 files changed, 33 insertions, 13 deletions
@@ -481,6 +481,13 @@ val replace_list(val list, val from, val to, val items) { val len = nil; + if (vectorp(items)) + items = list_vector(items); + else if (stringp(items)) + items = list_str(items); + else if (!listp(items)) + uw_throwf(error_s, lit("replace_list: cannot replace with ~s"), items, nao); + if (!list) return items; @@ -1539,13 +1546,14 @@ val replace_str(val str_in, val from, val to, val items) val iter; cnum f = c_num(from); cnum t = c_num(to); + cnum s; if (listp(items)) { for (iter = items; iter && f != t; iter = cdr(iter), f++) str_in->st.str[f] = c_chr(car(iter)); } else if (vectorp(items)) { - for (; f != t; f++) - str_in->st.str[f] = c_chr(vecref(items, num(f))); + for (s = 0; f != t; f++, s++) + str_in->st.str[f] = c_chr(vecref(items, num(s))); } else { uw_throwf(error_s, lit("replace_str: source object ~s not supported"), items, nao); @@ -1618,18 +1626,22 @@ val split_str(val str, val sep) list_collect_decl (out, iter); - for (;;) { - const wchar_t *psep = wcsstr(cstr, csep); - size_t span = (psep != 0) ? psep - cstr : wcslen(cstr); - val piece = mkustring(num(span)); - init_str(piece, cstr); - list_collect (iter, piece); - cstr += span; - if (psep != 0) { - cstr += len_sep; - continue; + if (len_sep == 0) { + return list_str(str); + } else { + for (;;) { + const wchar_t *psep = wcsstr(cstr, csep); + size_t span = (psep != 0) ? psep - cstr : wcslen(cstr); + val piece = mkustring(num(span)); + init_str(piece, cstr); + list_collect (iter, piece); + cstr += span; + if (psep != 0) { + cstr += len_sep; + continue; + } + break; } - break; } rel1(&sep); @@ -2881,6 +2893,14 @@ val replace_vec(val vec_in, val from, val to, val items) if (vectorp(items)) { memcpy(vec_in->v.vec + c_num(from), items->v.vec, sizeof *vec_in->v.vec * c_num(len_it)); + } else if (stringp(items)) { + cnum f = c_num(from); + cnum t = c_num(to); + cnum s; + const wchar_t *str = c_str(items); + + for (s = 0; f != t; f++, s++) + vec_in->v.vec[f] = chr(str[s]); } else { val iter; cnum f = c_num(from); |