pkl icon indicating copy to clipboard operation
pkl copied to clipboard

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

Open sin-ack opened this issue 4 months ago • 3 comments

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.

sin-ack avatar Aug 18 '25 15:08 sin-ack

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 avatar Aug 18 '25 16:08 StefMa

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 avatar Aug 18 '25 17:08 bioball

Thanks for the workaround!

sin-ack avatar Aug 18 '25 21:08 sin-ack