From 1cae1fb7f1b1c2882fedfcbfb0d97015c8726cc8 Mon Sep 17 00:00:00 2001 From: Kaz Kylheku Date: Fri, 16 Sep 2016 20:14:29 -0700 Subject: Bugfix in split-str: empty-match regexes. * lib.c (split_str_keep): In the regex case, changing to an infinite loop. The do/while is no longer needed because the if statement includes a test of the position having reached the end of the string. This is done before it is incremented by len, so we avoid wrongly keeping a separator. * txr.1: Clarified that an empty regex match behaves like an sep which is an empty string, --- lib.c | 9 +++++---- 1 file changed, 5 insertions(+), 4 deletions(-) (limited to 'lib.c') diff --git a/lib.c b/lib.c index 13982b69..56d4cf88 100644 --- a/lib.c +++ b/lib.c @@ -3814,24 +3814,25 @@ val split_str_keep(val str, val sep, val keep_sep) if (regexp(sep)) { list_collect_decl (out, iter); val pos = zero; + val slen = length(str); - do { + for (;;) { cons_bind (new_pos, len, search_regex(str, sep, pos, nil)); - if (eql(pos, new_pos) && len == zero) + if (len == zero && new_pos != slen) new_pos = plus(new_pos, one); iter = list_collect(iter, sub_str(str, pos, new_pos)); pos = new_pos; - if (len) { + if (len && pos != slen) { pos = plus(pos, len); if (keep_sep) iter = list_collect(iter, sub_str(str, new_pos, pos)); continue; } break; - } while (le(pos, length_str(str))); + } return out; } else { -- cgit v1.2.3