From 6b40915a50db3159394221a7b6b46cbb74dae606 Mon Sep 17 00:00:00 2001 From: Kaz Kylheku Date: Fri, 16 Sep 2016 06:55:59 -0700 Subject: split-str gains ability to keep separating pieces. * eval.c (eval_init): Register split-str to split_str_keep, with optional argument. * lib.c (split_str_keep): New function, formed from split_str, with third argument. (split_str): Reduced to wrapper around split_str_keep. Thus we don't have to update umpteen existing calls with an extra nil parameter. * lib.h (split_str_keep): Declared. * txr.1: Documented new optional argument of split-str. --- lib.c | 15 ++++++++++++++- 1 file changed, 14 insertions(+), 1 deletion(-) (limited to 'lib.c') diff --git a/lib.c b/lib.c index 7abdb95b..13982b69 100644 --- a/lib.c +++ b/lib.c @@ -3807,8 +3807,10 @@ val scat(val sep, ...) return ret; } -val split_str(val str, val sep) +val split_str_keep(val str, val sep, val keep_sep) { + keep_sep = default_bool_arg(keep_sep); + if (regexp(sep)) { list_collect_decl (out, iter); val pos = zero; @@ -3824,6 +3826,8 @@ val split_str(val str, val sep) if (len) { pos = plus(pos, len); + if (keep_sep) + iter = list_collect(iter, sub_str(str, new_pos, pos)); continue; } break; @@ -3846,6 +3850,8 @@ val split_str(val str, val sep) val piece = mkustring(one); init_str(piece, cstr); iter = list_collect(iter, piece); + if (keep_sep && *(cstr+1)) + iter = list_collect(iter, null_string); } gc_hint(str); @@ -3870,6 +3876,8 @@ val split_str(val str, val sep) cstr += span; if (psep != 0) { cstr += len_sep; + if (keep_sep) + iter = list_collect(iter, sep); continue; } break; @@ -3883,6 +3891,11 @@ val split_str(val str, val sep) } } +val split_str(val str, val sep) +{ + return split_str_keep(str, sep, nil); +} + val split_str_set(val str, val set) { const wchar_t *cstr = c_str(str); -- cgit v1.2.3