Skip to content

P1659R3 starts_with and ends_with #4674

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 1 commit into from
Jun 16, 2021
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
105 changes: 105 additions & 0 deletions source/algorithms.tex
Original file line number Diff line number Diff line change
Expand Up @@ -1131,6 +1131,36 @@
constexpr ForwardIterator
search(ForwardIterator first, ForwardIterator last, const Searcher& searcher);

namespace ranges {
// \ref{alg.starts.with}, starts with
template<@\libconcept{input_iterator}@ I1, @\libconcept{sentinel_for}@<I1> S1, @\libconcept{input_iterator}@ I2, @\libconcept{sentinel_for}@<I2> S2,
class Pred = ranges::equal_to, class Proj1 = identity, class Proj2 = identity>
requires @\libconcept{indirectly_comparable}@<I1, I2, Pred, Proj1, Proj2>
constexpr bool starts_with(I1 first1, S1 last1, I2 first2, S2 last2, Pred pred = {},
Proj1 proj1 = {}, Proj2 proj2 = {});
template<@\libconcept{input_range}@ R1, @\libconcept{input_range}@ R2, class Pred = ranges::equal_to,
class Proj1 = identity, class Proj2 = identity>
requires @\libconcept{indirectly_comparable}@<iterator_t<R1>, iterator_t<R2>, Pred, Proj1, Proj2>
constexpr bool starts_with(R1&& r1, R2&& r2, Pred pred = {},
Proj1 proj1 = {}, Proj2 proj2 = {});

// \ref{alg.ends.with}, ends with
template<@\libconcept{input_iterator}@ I1, @\libconcept{sentinel_for}@<I1> S1, @\libconcept{input_iterator}@ I2, @\libconcept{sentinel_for}@<I2> S2,
class Pred = ranges::equal_to, class Proj1 = identity, class Proj2 = identity>
requires (@\libconcept{forward_iterator}@<I1> || @\libconcept{sized_sentinel_for}@<S1, I1>) &&
(@\libconcept{forward_iterator}@<I2> || @\libconcept{sized_sentinel_for}@<S2, I2>) &&
@\libconcept{indirectly_comparable}@<I1, I2, Pred, Proj1, Proj2>
constexpr bool ends_with(I1 first1, S1 last1, I2 first2, S2 last2, Pred pred = {},
Proj1 proj1 = {}, Proj2 proj2 = {});
template<@\libconcept{input_range}@ R1, @\libconcept{input_range}@ R2, class Pred = ranges::equal_to,
class Proj1 = identity, class Proj2 = identity>
requires (@\libconcept{forward_range}@<R1> || @\libconcept{sized_range}@<R1>) &&
(@\libconcept{forward_range}@<R2> || @\libconcept{sized_range}@<R2>) &&
@\libconcept{indirectly_comparable}@<iterator_t<R1>, iterator_t<R2>, Pred, Proj1, Proj2>
constexpr bool ends_with(R1&& r1, R2&& r2, Pred pred = {},
Proj1 proj1 = {}, Proj2 proj2 = {});
}

// \ref{alg.modifying.operations}, mutating sequence operations
// \ref{alg.copy}, copy
template<class InputIterator, class OutputIterator>
Expand Down Expand Up @@ -4219,6 +4249,81 @@
\tcode{Searcher} need not meet the \oldconcept{CopyConstructible} requirements.
\end{itemdescr}

\rSec2[alg.starts.with]{Starts with}

\indexlibraryglobal{starts_with}%
\begin{itemdecl}
template<@\libconcept{input_iterator}@ I1, @\libconcept{sentinel_for}@<I1> S1, @\libconcept{input_iterator}@ I2, @\libconcept{sentinel_for}@<I2> S2,
class Pred = ranges::equal_to, class Proj1 = identity, class Proj2 = identity>
requires @\libconcept{indirectly_comparable}@<I1, I2, Pred, Proj1, Proj2>
constexpr bool ranges::starts_with(I1 first1, S1 last1, I2 first2, S2 last2, Pred pred = {},
Proj1 proj1 = {}, Proj2 proj2 = {});
template<@\libconcept{input_range}@ R1, @\libconcept{input_range}@ R2, class Pred = ranges::equal_to, class Proj1 = identity,
class Proj2 = identity>
requires @\libconcept{indirectly_comparable}@<iterator_t<R1>, iterator_t<R2>, Pred, Proj1, Proj2>
constexpr bool ranges::starts_with(R1&& r1, R2&& r2, Pred pred = {},
Proj1 proj1 = {}, Proj2 proj2 = {});
Comment on lines +4261 to +4265
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@jwakely: I'm not sure why this second overload doesn't have a specification, but that's what's in the paper. Please let me know if anything is missing here (or if this is covered by some blanket wording elsewhere).

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The range overloads map to the first/last overloads per [algorithms.requirements] p14.

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Aha, thanks!

\end{itemdecl}

\begin{itemdescr}
\pnum
\returns
\begin{codeblock}
ranges::mismatch(std::move(first1), last1, std::move(first2), last2,
pred, proj1, proj2).in2 == last2
\end{codeblock}
\end{itemdescr}

\rSec2[alg.ends.with]{Ends with}

\indexlibraryglobal{ends_with}%
\begin{itemdecl}
template<@\libconcept{input_iterator}@ I1, @\libconcept{sentinel_for}@<I1> S1, @\libconcept{input_iterator}@ I2, @\libconcept{sentinel_for}@<I2> S2,
class Pred = ranges::equal_to, class Proj1 = identity, class Proj2 = identity>
requires (@\libconcept{forward_iterator}@<I1> || @\libconcept{sized_sentinel_for}@<S1, I1>) &&
(@\libconcept{forward_iterator}@<I2> || @\libconcept{sized_sentinel_for}@<S2, I2>) &&
@\libconcept{indirectly_comparable}@<I1, I2, Pred, Proj1, Proj2>
constexpr bool ranges::ends_with(I1 first1, S1 last1, I2 first2, S2 last2, Pred pred = {},
Proj1 proj1 = {}, Proj2 proj2 = {});
\end{itemdecl}

\begin{itemdescr}
\pnum
Let \tcode{N1} be \tcode{last1 - first1} and
\tcode{N2} be \tcode{last2 - first2}.

\pnum
\returns
\tcode{false} if $\tcode{N1} < \tcode{N2}$, otherwise
\begin{codeblock}
ranges::equal(std::move(first1) + (N1 - N2), last1, std::move(first2), last2,
pred, proj1, proj2)
\end{codeblock}
\end{itemdescr}

\begin{itemdecl}
template<@\libconcept{input_range}@ R1, @\libconcept{input_range}@ R2, class Pred = ranges::equal_to, class Proj1 = identity,
class Proj2 = identity>
requires (@\libconcept{forward_range}@<R1> || @\libconcept{sized_range}@<R1>) &&
(@\libconcept{forward_range}@<R2> || @\libconcept{sized_range}@<R2>) &&
@\libconcept{indirectly_comparable}@<iterator_t<R1>, iterator_t<R2>, Pred, Proj1, Proj2>
constexpr bool ranges::ends_with(R1&& r1, R2&& r2, Pred pred = {},
Proj1 proj1 = {}, Proj2 proj2 = {});
\end{itemdecl}

\begin{itemdescr}
\pnum
Let \tcode{N1} be \tcode{ranges::distance(r1)} and
\tcode{N2} be \tcode{ranges::distance(r2)}.

\pnum
\returns
\tcode{false} if $\tcode{N1} < \tcode{N2}$, otherwise
\begin{codeblock}
ranges::equal(ranges::drop_view(ranges::ref_view(r1), N1 - N2), r2, pred, proj1, proj2)
\end{codeblock}
\end{itemdescr}

\rSec1[alg.modifying.operations]{Mutating sequence operations}

\rSec2[alg.copy]{Copy}
Expand Down
1 change: 1 addition & 0 deletions source/support.tex
Original file line number Diff line number Diff line change
Expand Up @@ -661,6 +661,7 @@
#define @\defnlibxname{cpp_lib_quoted_string_io}@ 201304L // also in \libheader{iomanip}
#define @\defnlibxname{cpp_lib_ranges}@ 201911L
// also in \libheader{algorithm}, \libheader{functional}, \libheader{iterator}, \libheader{memory}, \libheader{ranges}
#define @\defnlibxname{cpp_lib_ranges_starts_ends_with}@ 202106L // also in \libheader{algorithm}
#define @\defnlibxname{cpp_lib_raw_memory_algorithms}@ 201606L // also in \libheader{memory}
#define @\defnlibxname{cpp_lib_remove_cvref}@ 201711L // also in \libheader{type_traits}
#define @\defnlibxname{cpp_lib_result_of_sfinae}@ 201210L // also in \libheader{functional}, \libheader{type_traits}
Expand Down