C++ Logo

std-proposals

Advanced search

Re: [std-proposals] Iterators for basic_string and basic_string_view find functions

From: Jason McKesson <jmckesson_at_[hidden]>
Date: Fri, 20 Oct 2023 10:03:48 -0400
On Thu, Oct 19, 2023 at 7:13 PM Yexuan Xiao via Std-Proposals
<std-proposals_at_[hidden]> wrote:
>
> I propose to add a new set of find functions for basic_string and basic_string_view, which take and return iterators instead of indices. These functions will have the same name as the existing ones, but with an “x” prefix.
>
> Before:
>
> if (auto start = s.find(c); start != std::string::npos)
> {
> std::transform(s.begin() + start, s.begin() + start + c.size(), s.begin() + start, [](unsigned char c) { return std::toupper(c); });
> }
>
> After:
>
> if (auto start = s.xfind(c); start != s.end()) {
> std::transform(start, start + c.size(), start, [](unsigned char c){ return std::toupper(c); });
> }
>
> The full content using Bikeshed is here: https://api.csswg.org/bikeshed/?url=https://storage.nykz.org/proposals/iterators-for-string.bs&force=1

As it currently stands, the `basic_string` API is pretty clear. Member
functions use *indicies*; if you want to do some operation on the
string using iterators, we have standard algorithms for those
operations.

We shouldn't treat algorithms like a last resort; they're a first
class feature of all ranges.

Received on 2023-10-20 14:04:07