Serial Input Basics

denvlad:
to clear the input buffer (your last advice) is that it happens very fast, which means that we can easily be in a situation when the next character has not yet been received, so the loop will exit and fail to 'clear' the buffer

I think you are over-analyzing it and making it harder than it needs to be. The goal is to clear the input buffer: if the next character has not been received, it's not in the input buffer, so how can it be cleared?

This is not a function to ignore characters until some arbitrary end of message marker, or until there is no more data to be received. It is simply to clear out the characters that have already been received. The code was proposed to counter the lack of a flush() function for the serial input. A flush() function typically has two purposes: for an output stream, it makes sure all output has actually been written; for an input stream it discards any input data already available (and not any future input data.) The Serial class implements a flush() function, but only on the output stream. The proposed code is to provide a similar functionality for the input stream.

The major point of this approach to serial processing is to not block processing under any situations. By arbitrarily redefining the semantics of flush() to discard data until some end of message indication subverts that goal by introducing a blocking scenario. If you do want to introduce a function to skip until a certain marker, it would take a little more effort to do it in a non-blocking way. But that was not the intent of the subject code.