From 3d559d88a88b4b4427a85f4601ef7784f361522f Mon Sep 17 00:00:00 2001 From: "jonathan.davis" Date: Mon, 15 Aug 2011 14:27:17 -0700 Subject: [PATCH] Revert "Removed new, unnecessary overload of countUntil." This reverts commit b1765dc0cb5f2e185648935448a2b215d07ed2c2. Okay. So, I misread what count does and reverted my previous changes prematurely. So I'm putting them back. count counts the number of elements in the entire range which match the predicate, not just until the predicate fails. So, it does _not_ do what the new overload of countUntil does. --- std/algorithm.d | 45 +++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 45 insertions(+) diff --git a/std/algorithm.d b/std/algorithm.d index 5841945e3..5c5aa72c2 100644 --- a/std/algorithm.d +++ b/std/algorithm.d @@ -3655,6 +3655,51 @@ unittest assert(countUntil!"a > b"([0, 7, 12, 22, 9], 20) == 3); } +/++ + Returns the number of elements which must be popped from $(D haystack) + before $(D pred(hastack.front)) is $(D true.). + + Examples: +-------------------- +assert(countUntil!(std.uni.isWhite)("hello world") == 5); +assert(countUntil!(std.ascii.isDigit)("hello world") == -1); +assert(countUntil!"a > 20"([0, 7, 12, 22, 9]) == 3); +-------------------- + +/ +sizediff_t countUntil(alias pred, R)(R haystack) +if (isForwardRange!R && is(typeof(unaryFun!pred(haystack.front)) == bool)) +{ + static if (isNarrowString!R) + { + // Narrow strings are handled a bit differently + auto length = haystack.length; + for (; !haystack.empty; haystack.popFront()) + { + if (unaryFun!pred(haystack.front)) + { + return length - haystack.length; + } + } + } + else + { + typeof(return) result; + for (; !haystack.empty; ++result, haystack.popFront()) + { + if (unaryFun!pred(haystack.front)) return result; + } + } + return -1; +} + +//Verify Examples. +unittest +{ + assert(countUntil!(std.uni.isWhite)("hello world") == 5); + assert(countUntil!(std.ascii.isDigit)("hello world") == -1); + assert(countUntil!"a > 20"([0, 7, 12, 22, 9]) == 3); +} + /** * Same as $(D countUntil). This symbol has been scheduled for * deprecation because it is easily confused with the homonym function