BufRead::read_until with two byte delimiter

BufRead::read_until method allows you to read a buffer until a provided delimiter is found. I am wondering is there a similar method that allows you to use a byte array as a delimiter instead of a single byte.

PS: My delimiter has three bytes.

What's the incoming data type? str has split which takes Pattern which can be an &str. Otherwise I think you're probably left with implementing your own, which isn't difficult, it just sort of depends on desired input and output.

Ohh I am reading from a async Data Source. So it is going to be either a AsyncRead or a BuffRead.

Turns out there's a few more details in implementing AsyncBufRead than I realized so I took the easy way out and copied the read_until implementation from tokio and added some very critical 3's to it :laughing:. All credit to the tokio devs for this solution.

Playground

It seems to work as is, but I would test it before assuming my hack job is as robust as the tokio implementation.

Unfortunately that won't work if the delimiter is cut at the boundary between two reads.

1 Like

Oh you’re right of course. Thanks. It works a bit differently than I thought at first read through.

Also memchr3 is any of 3 chars not 3 consecutive so that also was wrong.

Turns out copy paste isn’t really the best strategy.

Here's a new one. Its not exactly the most efficient algorithm, but at least this time I'm a bit more confident it will work.

Playground

Thank you. This is awesome!!!