0

I'd like to have a std::getline function which is able to stop if it encounters any of the characters listed in a string, so I came up with the following:

std::istream& read_until(std::istream& is, std::string& s, const std::string& list) {
    s.clear();
    while (is.peek() && is && list.find(is.peek()) == list.npos) {
        s += is.get();
    }
    return is;
}

The fact that it leaves the terminating character on the stream is the desired behavior. This works, but it's ugly and doesn't feel the right way to go. I'd like to ask if you see any clear mistake or if you have a better way of handling this.

Costantino Grana
  • 3,132
  • 1
  • 15
  • 35
  • 1
    If you want help improving working code, you should post this on [CodeReview.SE](https://codereview.stackexchange.com/help/how-to-ask). If you do decide to do so, please delete the question here. – NathanOliver Mar 30 '20 at 15:45
  • 3
    FWIW, here is a really spiffy way to do it: https://stackoverflow.com/a/41029063/4342498 – NathanOliver Mar 30 '20 at 15:49
  • @NathanOliver Wow the linked solution is really crazy! I'll try it and if unsatisfactory I'll move this question to CodeReview. Thanks. – Costantino Grana Mar 30 '20 at 15:58

0 Answers0