String.split omits final empty string(s) for trailing pattern #338

Open
opened 2025-12-30 01:23:39 +01:00 by adam · 3 comments
Owner

Originally created by @sin-ack on GitHub (Aug 18, 2025).

Reproduced in 0.29.0.

Reproducer:

// Expected: List("", "c", "", "")
// Got: List("", "c")
"abcababab".split("ab")
// Works as expected: List("", "1")
".1".split(".")
// Expected: List("", "1", "")
// Got: List("", "1")
".1.".split(".")

Empty strings will be omitted from the end no matter how many times you repeat the pattern.

Originally created by @sin-ack on GitHub (Aug 18, 2025). Reproduced in 0.29.0. Reproducer: ```pkl // Expected: List("", "c", "", "") // Got: List("", "c") "abcababab".split("ab") // Works as expected: List("", "1") ".1".split(".") // Expected: List("", "1", "") // Got: List("", "1") ".1.".split(".") ``` Empty strings will be omitted from the end no matter how many times you repeat the pattern.
Author
Owner

@StefMa commented on GitHub (Aug 18, 2025):

The issue occurs because the implementation of String.split in the codebase ultimately delegates to Java’s String.split method. By default, Java’s split omits trailing empty strings from the result. This is reflected in StringNodes.java, where the split method calls self.split(Pattern.quote(separator)), so when the separator appears at the end of the string or multiple times in succession, the resulting list omits empty strings at the end—matching the behavior you observed in your examples.

@StefMa commented on GitHub (Aug 18, 2025): The issue occurs because the implementation of String.split in the codebase ultimately delegates to Java’s String.split method. By default, Java’s split omits trailing empty strings from the result. This is reflected in StringNodes.java, where the split method calls self.split(Pattern.quote(separator)), so when the separator appears at the end of the string or multiple times in succession, the resulting list omits empty strings at the end—matching the behavior you observed in your examples.
Author
Owner

@bioball commented on GitHub (Aug 18, 2025):

I agree that the behavior seems incorrect. However, fixing this now would be a breaking change.

To get your expected behavior, you can use splitLimit instead. For example:

"abcababab".splitLimit("ab", import("pkl:math").maxInt)
@bioball commented on GitHub (Aug 18, 2025): I agree that the behavior seems incorrect. However, fixing this now would be a breaking change. To get your expected behavior, you can use `splitLimit` instead. For example: ```pkl "abcababab".splitLimit("ab", import("pkl:math").maxInt) ```
Author
Owner

@sin-ack commented on GitHub (Aug 18, 2025):

Thanks for the workaround!

@sin-ack commented on GitHub (Aug 18, 2025): Thanks for the workaround!
Sign in to join this conversation.
1 Participants
Notifications
Due Date
No due date set.
Dependencies

No dependencies set.

Reference: starred/pkl#338