Summary
str_split in lib/bash/str/lib_str.sh produces a trailing empty element when the input string ends with the separator. This behavior is correct and consistent with most split implementations, but it is undocumented and untested.
Current behavior
local -a parts=()
str_split parts "alpha,beta," ","
# parts = ("alpha" "beta" "") ← trailing empty element
The loop terminates when the remainder no longer contains the separator. After consuming "beta," the remainder becomes "", the loop exits, and "" is unconditionally appended as the final element.
What's needed
- Add a note to the
str_split function header comment explaining that a trailing separator produces a trailing empty element.
- Add a BATS test case covering this exact scenario so the behavior is pinned and visible.
Suggested test
@test "str_split produces a trailing empty element for a trailing separator" {
local -a parts=()
str_split parts "alpha,beta," ","
[ "${#parts[@]}" -eq 3 ]
[ "${parts[0]}" = "alpha" ]
[ "${parts[1]}" = "beta" ]
[ "${parts[2]}" = "" ]
}
Summary
str_splitinlib/bash/str/lib_str.shproduces a trailing empty element when the input string ends with the separator. This behavior is correct and consistent with most split implementations, but it is undocumented and untested.Current behavior
The loop terminates when the remainder no longer contains the separator. After consuming
"beta,"the remainder becomes"", the loop exits, and""is unconditionally appended as the final element.What's needed
str_splitfunction header comment explaining that a trailing separator produces a trailing empty element.Suggested test