I have this situation where my function continuously receive data of various length. The data can be anything. I want to find the best way I to hunt for particular string in this data. The solution will require somehow to buffer previous data but I cannot wrap my head around the problem.
Here is an example of the problem:
DATA IN -> [\x00\x00\x01\x23B][][LABLABLABLABLA\x01TO][KEN][BLA\x01]…
if every […] represents a data chunk and [] represents a data chunk with no items, what is the best way to scan for the string TOKEN?
UPDATE:
I realised the question is a bit more complex. the [] are not separators. I just use them to describe the structure of the chunk per above example. Also TOKEN is not a static string per-se. It is variable length. I think the best way to read line by line but than the question is how to read a streaming buffer of variable length into lines.
Sorry, I voted to delete my previous answer as my understanding of the question was not correct. I didn’t read carefully enouogh and thought that the [] are token delimiters.
For your problem I’d recommend building a small state machine based on a simple counter:
For every character you do something like the following pseudo code:
This takes a minimum of processor cycles and also a minimum of memory aso you don’t need to buffer anything except the chunk just received.