I’m creating an Erlang application that needs to parse a binary TCP stream from a 3rd party program.
One of the types of packets I can receive has data formatted like this:
N_terms *[Flags ( 8 bits ), Type ( 8 bits ), [ optional data ] ].
The problem I have is the optional data is determined by a permutation of all possible
combinations of flags and types. Additionally, depending on the type there is additional optional data associated with it.
If I were to write a parser in an imperative language, I’d simply read in the 2 fields and then have a series of if( … ) statements where I would read a value and increment my position in the stream. In Erlang, my initial naive assumption is that I would have 2^N function clauses to match byte syntax on the stream, where N is total number of flags + all types with additional optional data.
As it stands, at a minimum I have 3 flags and 1 type that has optional data that I must implement, which would mean I’d have 16 different function clauses to match on the stream.
There must be a better, idiomatic way to do this in Erlang – what am I missing?
Edit:
I should clarify I do not know the number of terms in advance.
One solution is to take
and then write a function
decode(Flag, Type)which returns a description of whatRestwill contain. Now, that description can then be passed to a decoder forRestwhich can then use the description given to parse it correctly. A simple solution is to make the description into a list and whenever you decode something off of the stream, you use that description list to check that it is valid. That is, the description acts like your if.. construction.As for the pointer move, it is easy. If you have a
Binaryand decode itNext is the moved pointer under the hood you are searching for. So you can just break your binary into pieces like that to obtain the next part to work on.