summaryrefslogtreecommitdiffstats
path: root/lib.c
diff options
context:
space:
mode:
authorKaz Kylheku <kaz@kylheku.com>2010-02-27 11:38:12 +0900
committerKaz Kylheku <kaz@kylheku.com>2010-02-27 11:38:12 +0900
commit501b00b590d74253346e4c3b17331dcc07a42145 (patch)
tree3c0675adf5f5b1f64fef87c0926be21ace1eee59 /lib.c
parent647ec2123df81073de01a1799393fb1dbf738c29 (diff)
downloadtxr-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).
Diffstat (limited to 'lib.c')
-rw-r--r--lib.c27
1 files changed, 22 insertions, 5 deletions
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;