summaryrefslogtreecommitdiffhomepage
diff options
context:
space:
mode:
authorJo-Philipp Wich <jo@mein.io>2021-04-27 23:03:20 +0200
committerJo-Philipp Wich <jo@mein.io>2021-04-27 23:08:17 +0200
commite29b5744132d7dfb2989c70d4255840126d6ad19 (patch)
tree66a539220cb6a637eef52b00f42cdc1b0bdf99e0
parent8469c4b1be228f42c46f08852f028f7801b93cc9 (diff)
lib: fix uc_split() quirks
- Ensure that split by string produces an initial empty string in the result array when the string to split starts with the split substring - Ensure that split by string produces a trailing empty string in the result array when the string to split ends with the split substring Signed-off-by: Jo-Philipp Wich <jo@mein.io>
-rw-r--r--lib.c5
-rw-r--r--tests/custom/03_bugs/13_split_by_string_leading_trailing11
2 files changed, 13 insertions, 3 deletions
diff --git a/lib.c b/lib.c
index 5d6235d..20a1b81 100644
--- a/lib.c
+++ b/lib.c
@@ -967,7 +967,7 @@ uc_split(uc_vm *vm, size_t nargs)
else if (ucv_type(sep) == UC_STRING) {
sepstr = ucv_string_get(sep);
- for (p = splitstr + (*sepstr ? 1 : 0), seplen = strlen(sepstr); *p; p++) {
+ for (p = splitstr, seplen = strlen(sepstr); *p; p++) {
if (!strncmp(p, sepstr, seplen)) {
if (*sepstr || p > splitstr)
ucv_array_push(arr, ucv_string_new_length(splitstr, p - splitstr));
@@ -977,8 +977,7 @@ uc_split(uc_vm *vm, size_t nargs)
}
}
- if (*splitstr)
- ucv_array_push(arr, ucv_string_new_length(splitstr, p - splitstr));
+ ucv_array_push(arr, ucv_string_new_length(splitstr, p - splitstr));
}
else {
ucv_put(arr);
diff --git a/tests/custom/03_bugs/13_split_by_string_leading_trailing b/tests/custom/03_bugs/13_split_by_string_leading_trailing
new file mode 100644
index 0000000..10a6062
--- /dev/null
+++ b/tests/custom/03_bugs/13_split_by_string_leading_trailing
@@ -0,0 +1,11 @@
+When splitting a string, the existing uc_split() implementation failed
+to produce an empty leading and trailing result array element when the
+subject string started or ended with a delimitter.
+
+-- Expect stdout --
+[ "", "foo", "" ]
+-- End --
+
+-- Testcase --
+{{ split("/foo/", "/") }}
+-- End --