From 501b00b590d74253346e4c3b17331dcc07a42145 Mon Sep 17 00:00:00 2001 From: Kaz Kylheku Date: Sat, 27 Feb 2010 11:38:12 +0900 Subject: * 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). --- ChangeLog | 11 +++++++++++ lib.c | 27 ++++++++++++++++++++++----- lib.h | 1 + 3 files changed, 34 insertions(+), 5 deletions(-) diff --git a/ChangeLog b/ChangeLog index cf47df66..654122fb 100644 --- a/ChangeLog +++ b/ChangeLog @@ -1,3 +1,14 @@ +2010-02-27 Kaz Kylheku + + * 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 * lib.c (init_str): Bugfix: copy only len characters, not len + 1, so diff --git a/lib.c b/lib.c index 3135d57f..6ce2a1f4 100644 --- a/lib.c +++ b/lib.c @@ -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; diff --git a/lib.h b/lib.h index 55f0ba6d..233e4a0a 100644 --- a/lib.h +++ b/lib.h @@ -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); -- cgit v1.2.3