From 899ae557edfafa8489b6a6ad44c547a5a3a0e488 Mon Sep 17 00:00:00 2001 From: Jack Stouffer Date: Mon, 19 Jun 2017 12:40:36 -0400 Subject: [PATCH] Fix Issue 17525: std.algorithm.searching.skipOver should have a single argument with pred version --- std/algorithm/searching.d | 58 +++++++++++++++++++++++++++++++++++++++ 1 file changed, 58 insertions(+) diff --git a/std/algorithm/searching.d b/std/algorithm/searching.d index 72a6f341d39..2c1bee04b4e 100644 --- a/std/algorithm/searching.d +++ b/std/algorithm/searching.d @@ -4026,6 +4026,64 @@ if (is(typeof(binaryFun!pred(r.front, e))) && isInputRange!R) assert(!s2.skipOver('a')); } +/** + * An overload of `skipOver` which takes a predicate. Skips over any elements + * from the start of the range that evaluate to `true`. Stops popping elements + * on the first element which `pred(r.front)` equals `false`. + * + * Params: + * pred = the test for each element + * r = the range to skip over + * Returns: + * `true` if elements were skipped over, `false` otherwise. + */ +bool skipOver(alias pred, R)(ref R r) if (ifTestable!(typeof(r.front), unaryFun!pred)) +{ + bool ret = false; + + while (!r.empty && unaryFun!pred(r.front)) + { + r.popFront; + ret = true; + } + + return ret; +} + +/// +@safe pure unittest +{ + import std.ascii : isWhite; + + auto s1 = "\t\nHello"; + assert(s1.skipOver!(isWhite)); + assert(s1 == "Hello"); + + auto s2 = "Hello\t\n"; + assert(s2.skipOver!(a => !isWhite(a))); + assert(s2 == "\t\n"); + + auto s3 = "Hello\t\n"; + assert(!s3.skipOver!(isWhite)); + assert(s3 == "Hello\t\n"); +} + +@system unittest +{ + import std.algorithm.comparison : equal; + import std.ascii : isUpper; + import std.internal.test.dummyrange; + + auto r1 = new ReferenceInputRange!int([1, 2, 3, 4, 5, 6]); + auto r2 = new ReferenceInputRange!dchar("Hello"); + + r1.skipOver!(a => a < 3); + assert(r1.equal([3, 4, 5, 6])); + + r2.skipOver!(isUpper); + assert(r2.equal("ello")); +} + /** Checks whether the given $(REF_ALTTEXT input range, isInputRange, std,range,primitives) starts with (one