From bcba0c027998319d48c6e4b4ee3715eddf8946e1 Mon Sep 17 00:00:00 2001 From: Calvin Rose Date: Tue, 5 Jan 2021 18:53:00 -0600 Subject: [PATCH] Fix #548 - string/split bug. Also update docstrings for string/find. The 'skipping' behavior that was documented only applies to to string/replace-all. --- src/core/string.c | 9 +++++---- test/suite0010.janet | 6 ++++++ 2 files changed, 11 insertions(+), 4 deletions(-) diff --git a/src/core/string.c b/src/core/string.c index e37d1687..ca7232ab 100644 --- a/src/core/string.c +++ b/src/core/string.c @@ -398,6 +398,7 @@ static Janet cfun_string_split(int32_t argc, Janet *argv) { const uint8_t *slice = janet_string(state.text + lastindex, result - lastindex); janet_array_push(array, janet_wrap_string(slice)); lastindex = result + state.patlen; + kmp_seti(&state, lastindex); } const uint8_t *slice = janet_string(state.text + lastindex, state.textlen - lastindex); janet_array_push(array, janet_wrap_string(slice)); @@ -598,9 +599,8 @@ static const JanetReg string_cfuns[] = { JDOC("(string/find-all patt str)\n\n" "Searches for all instances of pattern patt in string " "str. Returns an array of all indices of found patterns. Overlapping " - "instances of the pattern are not counted, meaning a byte in string " - "will only contribute to finding at most on occurrence of pattern. If no " - "occurrences are found, will return an empty array.") + "instances of the pattern are counted individual, meaning a byte in string " + "may contribute to multiple found patterns.") }, { "string/has-prefix?", cfun_string_hasprefix, @@ -621,7 +621,8 @@ static const JanetReg string_cfuns[] = { { "string/replace-all", cfun_string_replaceall, JDOC("(string/replace-all patt subst str)\n\n" - "Replace all instances of patt with subst in the string str. " + "Replace all instances of patt with subst in the string str. Overlapping " + "matches will not be counted, only the first match in such a span will be replaced. " "Will return the new string if patt is found, otherwise returns str.") }, { diff --git a/test/suite0010.janet b/test/suite0010.janet index 84208101..92290721 100644 --- a/test/suite0010.janet +++ b/test/suite0010.janet @@ -131,4 +131,10 @@ (check-indent "\n Hello, world!\n dedented text\n " 4) (check-indent "\n Hello, world!\n indented text\n " 4) +# String bugs +(assert (deep= (string/find-all "qq" "qqq") @[0 1]) "string/find-all 1") +(assert (deep= (string/find-all "q" "qqq") @[0 1 2]) "string/find-all 2") +(assert (deep= (string/split "qq" "1qqqqz") @["1" "" "z"]) "string/split 1") +(assert (deep= (string/split "aa" "aaa") @["" "a"]) "string/split 2") + (end-suite)