Skip to content

Fix split(none, n) returning trailing whitespace - #1001

Merged
mitsuhiko merged 2 commits into
mitsuhiko:mainfrom
ChandraChaan:fix/splitn-ws-trailing
Nov 2, 2026
Merged

mitsuhiko merged 2 commits into
mitsuhiko:mainfrom
ChandraChaan:fix/splitn-ws-trailing

Conversation

@ChandraChaan

Copy link
Copy Markdown
Contributor

splitn_whitespace remembered where the last split ended and, once the input was exhausted, returned everything after it. When the string ends in whitespace right after a word, that whitespace came back as an extra item, unlike Python and minijinja-go:

Template Before After / Python
{{ "a b "|split(none, 5)|list }} ["a", "b", " "] ["a", "b"]
{{ "a "|split(none, 1)|list }} ["a", " "] ["a"]
{{ " "|split(none, 0)|list }} [" "] []

This also contradicts the filter's doc comment ("multiple spaces and empty strings will be removed from the result"), and applies to pycompat's str.split, which calls this filter.

At the end of the input it now returns only a word that is still open, or the remainder after the last allowed split, so last_split_end is no longer needed. The remainder still keeps its trailing whitespace like Python ("a b " with 2 splits is ["a", "b "]), and the split-n-ws snapshots are unchanged. Added cases to test_splitn_whitespace and a changelog entry.

I ran CI on my fork:

I haven't changed the Go port here. It has a separate issue in the same feature (split(none, 0) still splits once), which I can send separately.

splitn_whitespace remembered where the last split ended and returned
everything after it once the input was exhausted. When the string ended
in whitespace right after a word, that whitespace was returned as an
extra item, so "a b "|split(none, 5) gave ["a", "b", " "] and
"   "|split(none, 0) gave ["   "].

At the end of the input only a word that is still open (or the remainder
after the last allowed split) is returned now.
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants