Eio.Buf_read.line check terminator

Hello, I’ve bumped into an issue which I’m finding difficult to solve elegantly. I have a server reading lines from clients using Eio.Buf_read.line. The documentation of the function:

val line : string parser
line parses one line.

Lines can be terminated by either LF or CRLF. The returned string does not include the terminator.

If End_of_file is reached after seeing some data but before seeing a line terminator, the data seen is returned as the last line.

I would like to treat strings terminated with EOF differently from strings terminated with CRLF, but given that the function returns the string without its terminator, I don’t know how to handle each case differently. There is at_end_of_input which can be used to check if I am at the end of input, but again I don’t know if I am dealing with string[CRLF][EOF] or string[EOF].

Do I have to roll my own parser similar to line or is there an obvious smarter way that I’m missing here? Thanks.

You could call consumed_bytes before and after line. If the difference is exactly the length of the string then you know it didn’t have a terminator.

1 Like

Thanks a lot, @talex5! This did the trick and is more elegant than any alternative I came up with so far. :smile: