I'd like to suggest enriching findSplit* method family with the "needleless" predicate option.
Motivation:
Let's have a range of string tokens that - after cutting of the initial brace - looks like this:
auto range = ["23", "42", "14.3", "-323", "}"]
Now I want to map all the numbers to the array:
auto array = range.findSplitBefore(x => !x.isNumeric).map(to!double).array;
As you can see, I do not need the second parameter of prediate, as I am not comparing the subsequent haystack elements with any needle - I check the condition on the haystack elements alone instead. Unfortunately, It's not that simple, because I have only this prototype:
auto findSplitBefore(alias pred, R1, R2)(R1 haystack, R2 needle) //[...]
That's why I need to add a dummy parameter to achieve what I want to:
auto array = range.findSplitBefore(x => !x.isNumeric)([""]).map(to!double).array;
So what I suggest is to add this prototype as well for convenience:
auto findSplitBefore(alias pred, R)(R haystack) //[...]
and the similar ones for findSplit and findSplitAfter.
Comment #1 by hsteoh — 2018-05-09T17:38:13Z
Found a similar need for needleless overloads of findSplit* today too. The context is that I'm trying to tokenize a string, and if it starts with a digit, say "1234abcd" I'd like to be able to split it into "1234" and "abcd". So ideally:
------
auto input = "1234abcd";
auto r = input.findSplitBefore!(ch => !isDigit(ch));
assert(r[0] == "1234" && r[1] == "abcd");
------
The current overloads do not allow this, though.
Comment #2 by robert.schadek — 2024-12-01T16:31:14Z