Reverse splitters of binaries and IO lists

Hi,

I’m wrapping a simple API operating via ascii messages on tcp sockets. Responses to some requests are multi-line messages followed by a terminator line.

I am reading packets from the socket prepending them to the list of already received packets. After each read I am checking whether the message is over by comparing the last N bytes sent on the socket with the message terminator (of size N). If the message has ended I need to check whether the request has been successful. To do that I need to extract the line before the terminator line and parse it.

An elegant approach to finding the message ends and to error checking would be to create a Stream emitting the lines starting from the last one. The thing is that I am not quite sure how to write one since I could not find a way to search for substrings or to split in in a reverse direction (from the end). Note I do not want to split the whole message eagerly merely to get its last two lines

Could one someone point me in a right direction? I checked binary.match and String.split[ter and both work only in forward direction. The only relevant functionality I found is Erlang’s string.rstr but clearly it works on char lists rather than binaries. Erlang’s EEP-9 implemented in R12B proposed extending str() and rstr() to binaries in a binary_string module but I could not find any source for that module or load it which makes me thing this was never actually implemented.

There is no binary_string module but the string module has been extended. The find/3 function should do what you want. Check it out in the Erlang docs here.

1 Like

Many thanks, Robert.