diff options
author | Jo-Philipp Wich <jo@mein.io> | 2021-04-27 23:03:20 +0200 |
---|---|---|
committer | Jo-Philipp Wich <jo@mein.io> | 2021-04-27 23:08:17 +0200 |
commit | e29b5744132d7dfb2989c70d4255840126d6ad19 (patch) | |
tree | 66a539220cb6a637eef52b00f42cdc1b0bdf99e0 | |
parent | 8469c4b1be228f42c46f08852f028f7801b93cc9 (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.c | 5 | ||||
-rw-r--r-- | tests/custom/03_bugs/13_split_by_string_leading_trailing | 11 |
2 files changed, 13 insertions, 3 deletions
@@ -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 -- |