1

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.

NoSenseEtAl
  • 28,205
  • 28
  • 128
  • 277
  • 1
    This seems pretty nice to me. What do you not like it? What would you like to improve? I would probably just change the function name to `remove_if_prefixed` or something like that. – Aykhan Hagverdili Feb 18 '21 at 13:12
  • @AyxanHaqverdili maybe there is some STL algorithm or some other member function I did not think of... – NoSenseEtAl Feb 18 '21 at 13:15

1 Answers1

4

I mostly care about having something like nonexistant

bool /*prefix removed*/string_view::remove_prefix(string_view prefix);

Indeed, such function is not in standard library. But you've now written such function, and your function is thus a nicer way to do something like what you want.


If you're open for a different design, I would suggest following alternatives:

constexpr std::string_view
remove_prefix(std::string_view sv, std::string_view prefix) noexcept
{
    return sv.starts_with(prefix)
        ? sv.substr(prefix.size())
        : sv;
}

// usage
string_view no_prefix = remove_prefix(view, prefix);
bool was_removed = no_prefix.size() != view.size();

Or return a class:

struct modify_result {
    std::string_view result;
    bool modified;
};

constexpr modify_result
remove_prefix(std::string_view sv, std::string_view prefix) noexcept
{
    return sv.starts_with(prefix)
        ? modify_result{sv.substr(prefix.size()), true}
        : modify_result{sv, false};
}


// usage
auto [no_prefix, was_removed] = remove_prefix(view, prefix);

I'm not saying these are better for your use case. These are just alternative solutions which may apply to different use cases.

eerorika
  • 232,697
  • 12
  • 197
  • 326
  • Why did you remove `const` from the arguments? – Aykhan Hagverdili Feb 18 '21 at 13:24
  • @AyxanHaqverdili Keeps the declaration more readable due to not having irrelevant information. – eerorika Feb 18 '21 at 13:25
  • @AyxanHaqverdili FWIW, top level const on value parameters is meaningless from the callers perspective. `int(int)` and `int(const int)` are the same thing. You can read more about that [here](https://stackoverflow.com/questions/117293/use-of-const-for-function-parameters#:~:text=It%20is%20not%20recommended%20to,side%20effects%20outside%20the%20function.&text=%22Google%20C%2B%2B%20Style%20Guide%22%20%22Use%20of%20const%22%20section) – NathanOliver Feb 18 '21 at 13:36
  • @NathanOliver it often prevents accidentally modifying the values I didn't mean to. This is a definition, not just a declaration, so I think it'd be nice to spread the, perhaps subjectively, better style in a public forum like this. It's just a matter of style, of course, but `const` by default, mutable if needed is a nice style imo. – Aykhan Hagverdili Feb 18 '21 at 13:40
  • Definitely subjective, because I strongly disagree. If you const-qualify top-level argument types, it looks like it should allow overloading on argument constness, but this is not possible. Since top-level const qualification is [explicitly stripped](https://timsong-cpp.github.io/cppwp/dcl.fct#5) from the function type, it feels misleading. – Useless Feb 18 '21 at 14:00
  • 1
    @Useless it helps with reading function implementation, even if useless for function declaration. – NoSenseEtAl Feb 18 '21 at 14:13
  • Yes, reading comprehension is also subjective. I do just fine without it. – Useless Feb 18 '21 at 14:28