Hi,
for your specific example, the in my opinion best way would be
static_assert("hello world"sv.starts_with("hello"));
which is possible since C++20. But in general I agree, that it would be
nice to have these member functions. Specifically, I dislike calls to
string_view::substr with one argument (which is equivalent to your
proposed string_view::last):
"hello_world".substr(5);
This returns "_world"sv, but I do not find that obvious from the name
only.
Absolutely. However, notice that `.substr(5)` isn't `.last(5)`; it's `.last_all_but(5)`.
The vocabulary terms for "first" and "last" here, since Ranges-v3, are actually "take" and "drop":
assert("hello_world"sv.take(5) == "hello");
assert("hello_world"sv.drop(5) == "_world");
assert("hello_world"sv.drop_all_but(5) == "world");
The standard library was "poisoned" early on with APIs (like single-argument .substr(5)) that take integers to indicate either "start here" or "go until here" with no explanation. For example, it was recently brought to my attention that
assert(std::string("hello_world", 5) == "hello");
assert(std::string("hello_world"s, 5) == "_world");
–Arthur