You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
I was looking at using this library for converting some python objects that use the struct module into C++ definitions
Unfortunately I found that this repo only handles a small subset of valid struct format strings
struct allows spaces in format strings, as well as integers preceding an identifier. "I 128s 2h" is a valid format for the struct module, but not for this repo
Instead it fails on the first space, since it isn't listed as a valid character, rather than skipping it
The same for any integer.
I've made my own implementation to handle this, rather than a fork, since I also have to handle my custom objects
(The binary data is represented in nested structures, so my definitions also have to take that into account)
One common special case to look out for is strings:
With struct both "5c" & "5s" represent the equivalent of char[5] in C
However, the objects returned are different:
This is an important restriction, in writing my own function to standardise the format strings,
I had to include a case for strings, to ensure I recognised it as the single object struct would expect
Unfortunately my solution is a little clunky, but I'll share it here if you think you'd find it useful:
defsimplify(_format):
"""Converts a format string for the struct module into a list of type chars"""_format=_format.replace(" ", "")
if_format[-1].isnumeric():
raiseRuntimeError("_format is invalid (ends in a number)")
out, i=list(), 0whilei<len(_format):
char=_format[i]
ifchar.isnumeric(): # find the whole numberj=1while_format[i:i+j].isnumeric():
j+=1j-=1count=int(_format[i:i+j])
i+=jf=_format[i]
# NOTE: f is only assumed to be valid, validity is not checked hereiff=="s": # stringout.append(f"{count}s")
else:
out.extend([f] *count) # "2I" -> ["I", "I"]elifchar.isalpha(): # lazily collect typeout.append(_format[i])
elifchar=="?": # bool is not an alphabetical character, but is still validout.append("?")
else:
raiseRuntimeError(f"Invalid character '{char}' in _format")
i+=1returnout
This could probably be tidier but in my testing it seems to work with the format strings I'm using
(no pointer types, padding, size_t or 0 length types)
Using this with the dictionary lookups in struct_parse should handle both spaces and types preceeded by counts
Strings are passed through as "128s" etc., so a special case would need to be added
Presumably noting the type as string and also the size
I hope this is useful in expanding this repo to support all valid format strings the struct module accepts
The text was updated successfully, but these errors were encountered:
this returns a tuple of strings, with all types but strings being multiplied out
should be far easier to work with than my old solution
one major point to note is that the second regex pattern does not match for s, leaving strings unchanged
Thanks for taking the time to report this bug and for sharing your code! I'll leave this issue open and take a look at it implementing it when I get some time 🙂
I was looking at using this library for converting some python objects that use the
struct
module into C++ definitionsUnfortunately I found that this repo only handles a small subset of valid
struct
format stringsstruct
allows spaces in format strings, as well as integers preceding an identifier."I 128s 2h"
is a valid format for thestruct
module, but not for this repoInstead it fails on the first space, since it isn't listed as a valid character, rather than skipping it
The same for any integer.
I've made my own implementation to handle this, rather than a fork, since I also have to handle my custom objects
(The binary data is represented in nested structures, so my definitions also have to take that into account)
One common special case to look out for is strings:
With
struct
both"5c"
&"5s"
represent the equivalent ofchar[5]
in CHowever, the objects returned are different:
This is an important restriction, in writing my own function to standardise the format strings,
I had to include a case for strings, to ensure I recognised it as the single object
struct
would expectUnfortunately my solution is a little clunky, but I'll share it here if you think you'd find it useful:
This could probably be tidier but in my testing it seems to work with the format strings I'm using
(no pointer types, padding, size_t or 0 length types)
Using this with the dictionary lookups in
struct_parse
should handle both spaces and types preceeded by countsStrings are passed through as
"128s"
etc., so a special case would need to be addedPresumably noting the type as string and also the size
I hope this is useful in expanding this repo to support all valid format strings the
struct
module acceptsThe text was updated successfully, but these errors were encountered: