Skip to content

Commit 393d826

Browse files
authored
[std.algorithm.searching] Tweak countUntil docs (dlang#10734)
For a single needle, it only requires an input range. Split docs for unary `pred`. Document `needle` parameter. Explain pseudo-tuple returned, extend example.
1 parent d258782 commit 393d826

File tree

1 file changed

+56
-19
lines changed

1 file changed

+56
-19
lines changed

std/algorithm/searching.d

Lines changed: 56 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -776,26 +776,36 @@ if (isInputRange!R && !isInfinite!R)
776776

777777
/++
778778
Counts elements in the given
779-
$(REF_ALTTEXT forward range, isForwardRange, std,range,primitives)
780-
until the given predicate is true for one of the given `needles`.
779+
$(REF_ALTTEXT input range, isInputRange, std,range,primitives)
780+
until the given predicate is true for one of the given `needles` or `needle`.
781781
782782
Params:
783-
pred = The predicate for determining when to stop counting.
783+
pred = Binary predicate for determining when to stop counting,
784+
which will be passed each element of `haystack` and a needle.
784785
haystack = The
785786
$(REF_ALTTEXT input range, isInputRange, std,range,primitives) to be
786-
counted.
787-
needles = Either a single element, or a
788-
$(REF_ALTTEXT forward range, isForwardRange, std,range,primitives)
789-
of elements, to be evaluated in turn against each
790-
element in `haystack` under the given predicate.
787+
counted. Must be a
788+
$(REF_ALTTEXT forward range, isForwardRange, std,range,primitives) to
789+
use multiple needles.
790+
needles = A sequence of values or
791+
$(REF_ALTTEXT forward ranges, isForwardRange, std,range,primitives)
792+
of values, to be evaluated in turn by calling `pred(element, needle)`
793+
with each element of `haystack`.
794+
needle = A value passed as the 2nd argument to `pred`.
791795
792-
Returns: The number of elements which must be popped from the front of
796+
Returns:
797+
- The number of elements which must be popped from the front of
793798
`haystack` before reaching an element for which
794-
`startsWith!pred(haystack, needles)` is `true`. If
799+
$(LREF startsWith)`!pred(haystack, needles)` is `true`.
800+
- If
795801
`startsWith!pred(haystack, needles)` is not `true` for any element in
796-
`haystack`, then `-1` is returned. If more than one needle is provided,
797-
`countUntil` will wrap the result in a tuple similar to
798-
`Tuple!(ptrdiff_t, "steps", ptrdiff_t needle)`
802+
`haystack`, then `-1` is returned.
803+
- If more than one needle is provided,
804+
`countUntil` will wrap the result in a pseudo-tuple similar to
805+
`Tuple!(ptrdiff_t, "steps", ptrdiff_t, "needle")`.
806+
- `steps` is the count value, which can be implicitly tested for equality.
807+
- `needle` is the index into `needles` which matched.
808+
- Both are `-1` if there was no match.
799809
800810
See_Also: $(REF indexOf, std,string)
801811
+/
@@ -923,19 +933,29 @@ if (isInputRange!R &&
923933
assert(countUntil("日本語", '') == 2);
924934
assert(countUntil("日本語", "") == -1);
925935
assert(countUntil("日本語", '') == -1);
926-
assert(countUntil([0, 7, 12, 22, 9], [12, 22]) == 2);
927-
assert(countUntil([0, 7, 12, 22, 9], 9) == 4);
928-
assert(countUntil!"a > b"([0, 7, 12, 22, 9], 20) == 3);
929936

930-
// supports multiple needles
937+
const arr = [0, 7, 12, 22, 9];
938+
assert(countUntil(arr, [12, 22]) == 2);
939+
assert(countUntil(arr, 9) == 4);
940+
assert(countUntil!"a > b"(arr, 20) == 3);
941+
}
942+
943+
/// Multiple needles
944+
@safe unittest
945+
{
931946
auto res = "...hello".countUntil("ha", "he");
932947
assert(res.steps == 3);
933-
assert(res.needle == 1);
948+
assert(res.needle == 1); // the 2nd needle matched
934949

935950
// returns -1 if no needle was found
936951
res = "hello".countUntil("ha", "hu");
937952
assert(res.steps == -1);
938953
assert(res.needle == -1);
954+
955+
// `steps` can also be implicitly compared
956+
const arr = [0, 7, 12, 22, 9];
957+
assert(countUntil(arr, 22, 12) == 2); // `12` found after 2 elements
958+
assert(countUntil(arr, 5, 6) == -1);
939959
}
940960

941961
@safe unittest
@@ -1009,7 +1029,24 @@ if (isInputRange!R &&
10091029
assert(10.iota.repeat.joiner.countUntil!(a => a >= 9) == 9);
10101030
}
10111031

1012-
/// ditto
1032+
/++
1033+
Counts elements in the given
1034+
$(REF_ALTTEXT input range, isInputRange, std,range,primitives)
1035+
until the given predicate is true for one of the elements of `haystack`.
1036+
1037+
Params:
1038+
pred = Unary predicate for determining when to stop counting.
1039+
haystack = The
1040+
$(REF_ALTTEXT input range, isInputRange, std,range,primitives) to be
1041+
counted.
1042+
1043+
Returns:
1044+
- The number of elements which must be popped from the front of
1045+
`haystack` before reaching an element for which
1046+
$(LREF startsWith)`!pred(haystack)` is `true`.
1047+
- If `startsWith!pred(haystack)` is not `true` for any element in
1048+
`haystack`, then `-1` is returned.
1049+
+/
10131050
ptrdiff_t countUntil(alias pred, R)(R haystack)
10141051
if (isInputRange!R &&
10151052
is(typeof(unaryFun!pred(haystack.front)) : bool))

0 commit comments

Comments
 (0)