summaryrefslogtreecommitdiffstats
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
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).
-rw-r--r--ChangeLog11
-rw-r--r--lib.c27
-rw-r--r--lib.h1
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 <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
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);