mirror of
https://github.com/dlang/phobos.git
synced 2025-04-29 22:50:38 +03:00
[std/algorithm/searching] Improve findSplit docs
Improve formatting for returned tuple. Fix type of `result[1]` for After/Before. Fix `pred` description. Show example using custom `pred`.
This commit is contained in:
parent
9cb5f6d7db
commit
6b5f730b84
1 changed files with 35 additions and 21 deletions
|
@ -2885,38 +2885,52 @@ if (isForwardRange!R1 && ifTestable!(typeof(haystack.front), unaryFun!pred))
|
||||||
These functions find the first occurrence of `needle` in `haystack` and then
|
These functions find the first occurrence of `needle` in `haystack` and then
|
||||||
split `haystack` as follows.
|
split `haystack` as follows.
|
||||||
|
|
||||||
`findSplit` returns a tuple `result` containing $(I three) ranges. `result[0]`
|
$(PANEL
|
||||||
is the portion of `haystack` before `needle`, `result[1]` is the portion of
|
`findSplit` returns a tuple `result` containing $(I three) ranges.
|
||||||
`haystack` that matches `needle`, and `result[2]` is the portion of `haystack`
|
$(UL
|
||||||
after the match. If `needle` was not found, `result[0]` comprehends `haystack`
|
$(LI `result[0]` is the portion of `haystack` before `needle`)
|
||||||
|
$(LI `result[1]` is the portion of
|
||||||
|
`haystack` that matches `needle`)
|
||||||
|
$(LI `result[2]` is the portion of `haystack`
|
||||||
|
after the match.)
|
||||||
|
)
|
||||||
|
If `needle` was not found, `result[0]` comprehends `haystack`
|
||||||
entirely and `result[1]` and `result[2]` are empty.
|
entirely and `result[1]` and `result[2]` are empty.
|
||||||
|
|
||||||
`findSplitBefore` returns a tuple `result` containing two ranges. `result[0]` is
|
`findSplitBefore` returns a tuple `result` containing two ranges.
|
||||||
the portion of `haystack` before `needle`, and `result[1]` is the balance of
|
$(UL
|
||||||
`haystack` starting with the match. If `needle` was not found, `result[0]`
|
$(LI `result[0]` is the portion of `haystack` before `needle`)
|
||||||
|
$(LI `result[1]` is the balance of `haystack` starting with the match.)
|
||||||
|
)
|
||||||
|
If `needle` was not found, `result[0]`
|
||||||
comprehends `haystack` entirely and `result[1]` is empty.
|
comprehends `haystack` entirely and `result[1]` is empty.
|
||||||
|
|
||||||
`findSplitAfter` returns a tuple `result` containing two ranges.
|
`findSplitAfter` returns a tuple `result` containing two ranges.
|
||||||
`result[0]` is the portion of `haystack` up to and including the
|
$(UL
|
||||||
match, and `result[1]` is the balance of `haystack` starting
|
$(LI `result[0]` is the portion of `haystack` up to and including the
|
||||||
after the match. If `needle` was not found, `result[0]` is empty
|
match)
|
||||||
|
$(LI `result[1]` is the balance of `haystack` starting
|
||||||
|
after the match.)
|
||||||
|
)
|
||||||
|
If `needle` was not found, `result[0]` is empty
|
||||||
and `result[1]` is `haystack`.
|
and `result[1]` is `haystack`.
|
||||||
|
)
|
||||||
|
$(P
|
||||||
In all cases, the concatenation of the returned ranges spans the
|
In all cases, the concatenation of the returned ranges spans the
|
||||||
entire `haystack`.
|
entire `haystack`.
|
||||||
|
|
||||||
If `haystack` is a random-access range, all three components of the tuple have
|
If `haystack` is a random-access range, all three components of the tuple have
|
||||||
the same type as `haystack`. Otherwise, `haystack` must be a
|
the same type as `haystack`. Otherwise, `haystack` must be a
|
||||||
$(REF_ALTTEXT forward range, isForwardRange, std,range,primitives) and
|
$(REF_ALTTEXT forward range, isForwardRange, std,range,primitives) and
|
||||||
the type of `result[0]` and `result[1]` is the same as $(REF takeExactly,
|
the type of `result[0]` (and `result[1]` for `findSplit`) is the same as $(REF takeExactly,
|
||||||
std,range).
|
std,range).
|
||||||
|
|
||||||
For more information about `pred` see $(LREF find).
|
For more information about `pred` see $(LREF find).
|
||||||
|
)
|
||||||
Params:
|
Params:
|
||||||
pred = Predicate to use for comparing needle against haystack.
|
pred = Predicate to compare elements.
|
||||||
haystack = The range to search.
|
haystack = The forward range to search.
|
||||||
needle = What to look for.
|
needle = The forward range to look for.
|
||||||
|
|
||||||
Returns:
|
Returns:
|
||||||
|
|
||||||
|
@ -3171,12 +3185,12 @@ if (isForwardRange!R1 && isForwardRange!R2)
|
||||||
}
|
}
|
||||||
else assert(0);
|
else assert(0);
|
||||||
|
|
||||||
// works with const aswell
|
// findSplitBefore returns 2 ranges
|
||||||
if (const split = "dlang-rocks".findSplit("-"))
|
if (const split = [2, 3, 2, 3, 4, 1].findSplitBefore!"a > b"([2, 2]))
|
||||||
{
|
{
|
||||||
assert(split[0] == "dlang");
|
assert(split[0] == [2, 3, 2]);
|
||||||
assert(split[1] == "-");
|
// [3, 4] each greater than [2, 2]
|
||||||
assert(split[2] == "rocks");
|
assert(split[1] == [3, 4, 1]);
|
||||||
}
|
}
|
||||||
else assert(0);
|
else assert(0);
|
||||||
}
|
}
|
||||||
|
|
Loading…
Add table
Add a link
Reference in a new issue