Well since I didn't much explain the code you misunderstood it. :( Mea
culpa. Sorry.
This is Windows code that always gets \r\n up from the API level. The
separate testing for \r was not to handle out-of-order \n\r, but because
it can happen that the buffer doesn't have room for the ordinary \r\n's
final \n, in which case that single value is delivered via the next
read. Whichever of the two situations happen, the received control chars
are removed, and a single \n is added at the end of a complete line --
or at least that was the intention, and it seems to work (I use a very
small buffer, just 6 values, to test it). :)
The idea of nulling is good in general but for this code it's necessary
to keep explicit track of the length to avoid checking the length again,
and nulling isn't simpler than decrementing, is it?
Cheers, and thanks,
- Alf
PS: This brings up the question of how to unit-test such code
automatically, which seems to boil down to how to mock the API read
function (in this case Windows' ReadConsoleW) in a good way, maintaining
the maintainability of the code, so to speak?