Skip to content

Docs: document and test str_split trailing-separator behavior #128

Description

@codeforester

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

  1. Add a note to the str_split function header comment explaining that a trailing separator produces a trailing empty element.
  2. 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]}" = "" ]
}

Activity

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

Labels

documentationDocumentation improvements

Type

No type

Projects

Milestone

Relationships

None yet

Development

No branches or pull requests

Issue actions