As of C++20 string_view
has remove_prefix
but it is "wrong"(wrong for my usecase) thing.
It takes as an argument number of chars, instead of a prefix(string like thing).
I have this code, but I wonder if there is some nicer way to do what I want(note that my code cares about if prefix was removed so I return boolean):
static bool Consume(std::string_view& view, const std::string_view prefix)
{
if (view.starts_with(prefix))
{
view.remove_prefix(prefix.size());
return true;
}
return false;
}
note: I know I could return optional<string_view>
instead of bool + out arg, but that is a different style discussion, I mostly care about having something like nonexistant
bool /*prefix removed*/string_view::remove_prefix(string_view prefix);
note2: tagged this C++17 because that is when string_view
"arrived", I am fine with any C++20 alternatives.