view: add split_on overload for array delimiters

This commit is contained in:
Danny Robson 2022-01-13 13:59:48 +10:00
parent 94bc72d3a1
commit a2259013f5

View File

@ -793,9 +793,9 @@ namespace cruft {
auto const pos = std::find (std::begin (buffer), std::end (buffer), value);
if (pos == std::end (buffer))
return {
buffer,
{std::end (buffer), std::end (buffer)}
};
buffer,
{std::end (buffer), std::end (buffer)}
};
return {
{ buffer.begin (), pos, },
@ -804,6 +804,37 @@ namespace cruft {
}
///////////////////////////////////////////////////////////////////////////
/// Split a view at the first occurrence of the sub-string `value`.
///
/// If no occurrence was found the returned pair will be the original view
/// and an empty view.
template <typename IteratorT, std::size_t N>
std::pair<view<IteratorT>, view<IteratorT>>
split_on (
view<IteratorT> const buffer,
typename std::iterator_traits<IteratorT>::value_type (&value)[N]
) {
auto const pos = std::search (
std::begin (buffer),
std::end (buffer),
std::begin (value),
std::end (value)
);
if (pos == std::end (buffer))
return {
buffer,
{std::end (buffer), std::end (buffer)}
};
return {
{ buffer.begin (), pos, },
{ pos + N, buffer.end () }
};
}
///////////////////////////////////////////////////////////////////////////
/// Returns a reference to a value of the designated type at the front of
/// the word-view. if there is insufficient data for the extraction an