title: Incremental Stream Parsing in C++
body: <p>I am reading data from a (serial) port using a non-blocking read() function (in C/C++). 
  This means the data comes-in in chunks of undetermined (but reported) sizes (incl. 0) each time I "poll" the port. I then need to parse this "stream" for certain patterns (not XML).</p>
  
  <p>My naive implementation concatenates the new string to the previous stream-string each time read() returns a non-zero buffer, and re-parses the whole string.
  When a pattern is matched, the relevant part is discarded leaving only the tail of the string for the next time.</p>
  
  <p>Obviously there are much more efficient way to do this, for example, incremental parsing a-la SAX, deque-like buffers or similar string slices etc.
  Also, obviously, I am not the first to have to do this type of stream parsing.</p>
  
  <p>Does anyone know of any library that already does this sort of thing?
  Preventing memory-overflow in case of missing pattern matches would also be a big plus.</p>
  
  <p>Thanks,
  Adi</p>