diff options
author | Kaz Kylheku <kaz@kylheku.com> | 2010-02-27 11:38:12 +0900 |
---|---|---|
committer | Kaz Kylheku <kaz@kylheku.com> | 2010-02-27 11:38:12 +0900 |
commit | 501b00b590d74253346e4c3b17331dcc07a42145 (patch) | |
tree | 3c0675adf5f5b1f64fef87c0926be21ace1eee59 | |
parent | 647ec2123df81073de01a1799393fb1dbf738c29 (diff) | |
download | txr-501b00b590d74253346e4c3b17331dcc07a42145.tar.gz txr-501b00b590d74253346e4c3b17331dcc07a42145.tar.bz2 txr-501b00b590d74253346e4c3b17331dcc07a42145.zip |
* lib.h (split_str_sep): Declared.
* lib.c (split_str_sep): New function.
(split_str): Semantics changed; the second argument
is not a set of separator characters (like in split_str_sep)
but rather a separator string. Fixed bug: if the input
string is empty, the output list is empty. This caused
infinite looping behavior in @(freeform).
-rw-r--r-- | ChangeLog | 11 | ||||
-rw-r--r-- | lib.c | 27 | ||||
-rw-r--r-- | lib.h | 1 |
3 files changed, 34 insertions, 5 deletions
@@ -1,3 +1,14 @@ +2010-02-27 Kaz Kylheku <kkylheku@gmail.com> + + * lib.h (split_str_sep): Declared. + + * lib.c (split_str_sep): New function. + (split_str): Semantics changed; the second argument + is not a set of separator characters (like in split_str_sep) + but rather a separator string. Fixed bug: if the input + string is empty, the output list is empty. This caused + infinite looping behavior in @(freeform). + 2010-02-24 Kaz Kylheku <kkylheku@gmail.com> * lib.c (init_str): Bugfix: copy only len characters, not len + 1, so @@ -997,17 +997,34 @@ val split_str(val str, val sep) { const wchar_t *cstr = c_str(str); const wchar_t *csep = c_str(sep); + size_t len_sep = c_num(length_str(sep)); + list_collect_decl (out, iter); - for (;;) { - size_t span = wcscspn(cstr, csep); + for (; *cstr != 0; cstr += len_sep) { + 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; + } + + return out; +} + +val split_str_set(val str, val set) +{ + const wchar_t *cstr = c_str(str); + const wchar_t *cset = c_str(set); + list_collect_decl (out, iter); + + for (; *cstr != 0; cstr++) { + size_t span = wcscspn(cstr, cset); val piece = mkustring(num(span)); init_str(piece, cstr); list_collect (iter, piece); cstr += span; - if (!*cstr) - break; - cstr++; } return out; @@ -308,6 +308,7 @@ val search_str_tree(val haystack, val tree, val start_num, val from_end); val sub_str(val str_in, val from_num, val to_num); val cat_str(val list, val sep); val split_str(val str, val sep); +val split_str_set(val str, val set); val trim_str(val str); val string_lt(val astr, val bstr); val chr(wchar_t ch); |