Bug 5507 – countUntil should take Ranges... instead of R2

Status
RESOLVED
Resolution
FIXED
Severity
enhancement
Priority
P2
Component
phobos
Product
D
Version
D2
Platform
Other
OS
All
Creation time
2011-01-30T11:18:00Z
Last change time
2013-02-24T16:50:18Z
Assigned to
nobody
Creator
Jesse.K.Phillips+D

Comments

Comment #0 by Jesse.K.Phillips+D — 2011-01-30T11:18:23Z
The recent addition of countUntil makes use of the startsWith template function which has an overload for accepting many ranges. By modifying the signature you can take advantage of this overload at no additional code: - sizediff_t countUntil(alias pred = "a == b", R1, R2)(R1 haystack, R2 needle) + sizediff_t countUntil(alias pred = "a == b", R1, Ranges...)(R1 haystack, Ranges needle) https://github.com/D-Programming-Language/phobos/blob/master/std/algorithm.d#L3239
Comment #1 by monarchdodra — 2012-11-15T05:31:27Z
(In reply to comment #0) > The recent addition of countUntil makes use of the startsWith template function > which has an overload for accepting many ranges. By modifying the signature you > can take advantage of this overload at no additional code: > > - sizediff_t countUntil(alias pred = "a == b", R1, R2)(R1 haystack, R2 needle) > + sizediff_t countUntil(alias pred = "a == b", R1, Ranges...)(R1 haystack, > Ranges needle) > > https://github.com/D-Programming-Language/phobos/blob/master/std/algorithm.d#L3239 While the new signature *would* be nice to have, this actually *would* make count until much more complex: startsWith only cares about the beginning of the ranges, and as such, is perfectly capable of operating on input+input ranges. By contrast, countUntil *Needs* to have Forward+Forward 1. This makes the "if(is(typeof(startsWith(haystack, needles))))" a bad condition Also, since startsWith "consumes" its inputs, the ranges passed to it need to be saved. 2. This would require a lot of code, to analyze just what in needle is a range (that needs saving) or is an element (that doesn't). Not saying it's a bad idea, or that it can't happen, but it requires *much* more than a simple signature change. For now (IMHO), we should concentrate on consolidating the current implementati
Comment #2 by eco — 2013-01-10T09:33:35Z
Hit this answering a StackOverflow question <http://stackoverflow.com/q/14262766/216300>. My solution (using countUntil() on the result of find() with multiple needles) requires two passes which is unfortunate since there should only need to be one pass. There is probably a way to avoid this that I didn't see but whatever it is is probably just as convoluted as my solution. countUntil should really just accept multiple needles so consider this my +1.
Comment #3 by github-bugzilla — 2013-02-24T16:50:17Z