At present it seems Swift's standard lib has a split
method that consumes the split separator, eg:
[1, 2, 3, 4].split(separator: 3) // [[1, 2], [4]]
"abc def".split(separator: " ") // ["abc", "def"]
Array(0...10).split { $0.isMultiple(of: 3) } // [[1, 2], [4, 5], [7, 8], [10]]
This is great when you're splitting strings on whitespace or commas or breaking down other sequences where you don't want to retain the separator.
However often you want to retain the separator. One recent example I ran into is breaking down "PascalCase" identifiers.
Eg, I wanted to break strings like "SanFrancisco"
down into an array such as ["San", "Francisco"]
(and then join it together again with space separators).
Ideally there would be a function in the Swift standard library that acted like the following:
[1, 2, 3, 4].splitBefore(separator: 3) // [[1, 2], [3, 4]]
"abc def".splitBefore(separator: " ") // ["abc", " def"]
Array(0...10).splitBefore { $0.isMultiple(of: 3) } // [[0, 1, 2], [3, 4, 5], [6, 7, 8], [9, 10]]
"SanFrancisco".splitBefore { $0.isUppercase } // ["San", "Francisco"]
How about it? This would be a purely additive change so wouldn't break ABI.
PS: I'm not such a fan of using splitBefore
as the method name. But I can't currently think of anything better.