4 ms·
> If you're just going to extract the values once, why not just write the code to actually do that instead of trying to play tricks with memcpy()? Because it's
by adgar 15y ago
> If you're just going to extract the values once, why not just write the code to actually do that instead of trying to play tricks with memcpy()?
Because it's more expressive, doesn't cost performance, and is less error-prone than writing field extraction code manually. And because it's the type of processing people actually use C for.
> Those tricks won't even work in many cases where certain fields indicate the length of certain other fields (which is quite common).
... it's common for a field to specify the size of the data portion of a packet, not other header fields. The one exception I can think of is a version number, which indicates the layout of the rest of the packet, which isn't exactly rocket science to model using explicitly-packed structs.
> When I first started programming in C I also had this fantasy that I could parse network formats with memcpy().
Nobody here has such a fantasy, they're expressing a desire for this to be made possible.
> Since then I've become convinced that writing the actual parsing code is for the best.
Given a programming language grammar, would you prefer to use an LR parser generator, to write an LR grammar by hand, or to write a recursive-descent LL parser by hand?
- ori_b 15y agoIt seems that the preferred implementation these days is to write an LL parser by hand, if most production compilers and interpreters are anything to go by.
- adgar 15y agoThere are some well-publicized LL parsers - C# for example - but I'm not convinced by "most production compilers and interpreters."