MCPcopy Create free account
hub / github.com/andreasfertig/programming-with-cpp20 / Parse

Function Parse

02.18-coroutineParsingDataStream1/main.cpp:200–231  ·  view source on GitHub ↗

Source from the content-addressed store, hash-verified

198static const byte SOF{0x10};
199
200FSM Parse(
201 DataStreamReader& stream) // #A Pass the stream a parameter
202{
203 while(true) {
204 byte b = co_await stream; // #B Await on the stream
205 if(ESC != b) { continue; }
206
207 b = co_await stream;
208 // #C not looking at a end or start sequence
209 if(SOF != b) { continue; }
210
211 std::string frame{};
212 // #D capture the full frame
213 while(true) {
214 b = co_await stream;
215
216 if(ESC == b) {
217 // #E skip this byte and look at the next one
218 b = co_await stream;
219
220 if(SOF == b) {
221 co_yield frame;
222 break;
223 } else if(ESC != b) {
224 break; // #F out of sync
225 }
226 }
227
228 frame += static_cast<char>(b);
229 }
230 }
231}
232
233generator<byte> sender(std::vector<byte> fakeBytes)
234{

Callers 1

mainFunction · 0.70

Calls

no outgoing calls

Tested by

no test coverage detected