bitstring
bitstring copied to clipboard
Interrogation of pack format
Feature suggestion –
A function to retrieve the start and end bit index of a pack format parameter, ie. the ability to interrogate a pack format. If no parameter is supplied, it returns the start, end of the whole format, ie. the length.
The r'n'd project I'm working on at the moment has a lot of hand-counted constants that follow each format list, with bit lengths of the format and the occasional offset within that.
I'm not sure what you mean by the start and end bit indices. Some pack formats will have a well defined length, but this isn't true in general (some formats' length depend on the values packed).
So I don't think I can do this, if I understand the feature request correctly.
(Sorry for the delay - I wasn't receiving any notifications from github).
Concrete example, keeping with the MPEG world you've already demonstrated.
Let's say you define the structure of an MPEG TS adaptation field so you can pack and unpack.
TSPacketAdaptationFieldFormat = [
'uint:8=adaptation_field_length',
'bin:1=discontinuity_indicator',
'bin:1=random_access_indicator',
'bin:1=elementary_stream_priority_indicator',
'bin:1=PCR_flag',
'bin:1=OPCR_flag',
'bin:1=splicing_point_flag',
'bin:1=transport_private_data_flag',
'bin:1=adaptation_field_extension_flag']
The first use is one of convenience. Say I only want to read the PCR flag. There's a lot of busy-work in writing out all the variables for the full unpack. I have a full screen's worth of parameters to unpack into without even being able to yet see the = foo.unpack(! It would be easier just to be able to reference where the field you're after is, and extract it directly.
The critical use is for adaptation_field_length. This length is defined as the length following the value. Therefore, you need to know the position of the value within the pack format. For each of these cases, I have to have a separate constant created alongside my format list, and I'm there hand-counting bits to get the value right.
Similarly, this adaptation field sits within other structures (and so pack format lists), and so I need to know the overall length of each format to calculate length values and seek to where I want to be.
Now, all that said, this was a feature request, the lack of this has not stopped me doing what I had to. It just felt that given pack formats exist, there was an obvious piece of functionality missing to fully realise their use. And it's not me losing my own time to implement it. So no need to apologise – rather great thanks from me for you creating bitstring!