incorrect Stream::find( ... ) code.

Consider a string we're looking for "ACAB" in an input stream "xxxxACACAB". The last four characters definitely match the wanted string "ACAB", but the find( ... ) function (nor the findUntil( ...) function) doesn't find it because after a mismatch it backs up to the first position in the search string "ACAB".

For 'normal' strings such as "OK" or "ERROR" this doesn't matter much but the code is conceptually dead wrong; it shouldn't have backed up to the first position in the search string but to the position of the longest prefix in the search string that happens to be a postfix in the current input stream (read that again!). This is the so called 'failure function' in the Knuth Morris Pratt online string search function and it needs an additional array of ints proportional to the length of the string to be searched for.

Just to let you know ...

kind regards,

Jos

Just to let you know ...

All we know after reading your post is that your code doesn't do what you expect. We do not know that there is a problem with Stream::find().

PaulS:
All we know after reading your post is that your code doesn't do what you expect. We do not know that there is a problem with Stream::find().

Try it and see for your self (in the loop() function:)

Serial.println(Serial.find("ACAB"));

... and type "xxxxxACACAB" in the monitor.

kind regards,

Jos

JosAH:
Try it and see for your self (in the loop() function:)

Serial.println(Serial.find("ACAB"));

... and type "xxxxxACACAB" in the monitor.

kind regards,

Jos

Please post your code to be tested.

This seems to be an ancient issue Stream::findUntil returns incorrect result.

There seems to be an old fix Fix findUntil and general timeouts in Stream library #2696.

Whandall:
This seems to be an ancient issue Stream::findUntil returns incorrect result.

There seems to be an old fix Fix findUntil and general timeouts in Stream library #2696.

Thanks for the links; I knew it wasn't my fault (as suggested by PaulS in reply #1). While the links concentrate more on the timeout issue, I think that building a failure function is more of a problem: it needs an array of int indexes, as long as the string being searched for. Keeping a large buffer ready isn't a solution and malloc'ing a buffer each time the function is called is a nono as well. While the actual construction of that failure function is 'trivial' (see: Knuth–Morris–Pratt algorithm - Wikipedia), consructing it 'conceptually', i.e. producing ff(i) on the fly isn't so trivial :wink: I'm still thinking about it ...

kind regards,

Jos