Bug 6490 – countUntil will take a non-range for its second argument

Status
RESOLVED
Resolution
INVALID
Severity
normal
Priority
P2
Component
phobos
Product
D
Version
D2
Platform
All
OS
All
Creation time
2011-08-14T03:33:00Z
Last change time
2015-06-09T05:15:01Z
Assigned to
nobody
Creator
issues.dlang

Comments

Comment #0 by issues.dlang — 2011-08-14T03:33:15Z
This code compiles: import std.algorithm; void main() { string str = "abc123"; auto i = countUntil!((a, b){return (a >= '0' && a <= '9') || a == '.';}) (str, 0); } It shouldn't. Notice that the second argument to countUntil is 0, which is an int, not a range.
Comment #1 by code — 2011-08-14T03:43:43Z
Are you sure that this is a bug? The documentation says that it counts until haystack.startsWith!pred(needle) is true, and startsWith is also defined for single element needles. You are right, the usefulness of this behavior is questionable, though.
Comment #2 by issues.dlang — 2011-08-14T03:46:35Z
Okay. I misunderstood countUntil. Its signature is sizediff_t countUntil(alias pred = "a == b", R1, R2)(R1 haystack, R2 needle) if (is(typeof(startsWith!pred(haystack, needle)))) Of course, I didn't see the template constraint in the documentation, and given the type names, I assumed that needle had to be a range. My mistake. There's no problem with this code.
Comment #3 by issues.dlang — 2011-08-14T03:47:53Z
The real issue here IMHO is the fact that 0 implicitly converts to a dchar, but that's not going to change anytime soon. The original code that I copied this from was doing that though. I wouldn't normally put a 0 there. In any case, this isn't a bug.