<div dir="ltr">+1 on the idea of supporting variable-length strings with the length encoded in the preceding packed element!<br><div><br></div><div>Several months ago I was trying to write a parser and writer of PostgreSQL's COPY ... WITH BINARY format. I started out trying to implement it in pure python using the struct module. Due to the existence of variable-length strings encoded in precisely the way you mention, it was not possible to parse an entire row of data without invoking any pure-python-level logic. This made the implementation infeasibly slow. I had to switch to using cython to get it done fast enough (implementation is here: <a href="https://github.com/spitz-dan-l/postgres-binary-parser">https://github.com/spitz-dan-l/postgres-binary-parser</a>).</div><div><br></div><div>I believe that with this single change ($, or whatever format specifier one wishes to use), assuming it were implemented efficiently in c, I could have avoided using cython and gotten a satisfactory level of performance with the struct module and python/numpy's already-performant bytestring manipulation faculties.</div><div><br></div><div>-Dan Spitz</div></div><br><div class="gmail_quote"><div dir="ltr">On Wed, Jan 18, 2017 at 5:32 AM Elizabeth Myers <<a href="mailto:elizabeth@interlinked.me">elizabeth@interlinked.me</a>> wrote:<br></div><blockquote class="gmail_quote" style="margin:0 0 0 .8ex;border-left:1px #ccc solid;padding-left:1ex">Hello,<br class="gmail_msg">
<br class="gmail_msg">
I've noticed a lot of binary protocols require variable length<br class="gmail_msg">
bytestrings (with or without a null terminator), but it is not easy to<br class="gmail_msg">
unpack these in Python without first reading the desired length, or<br class="gmail_msg">
reading bytes until a null terminator is reached.<br class="gmail_msg">
<br class="gmail_msg">
I've noticed the netstruct library<br class="gmail_msg">
(<a href="https://github.com/stendec/netstruct" rel="noreferrer" class="gmail_msg" target="_blank">https://github.com/stendec/netstruct</a>) has a format specifier, $, which<br class="gmail_msg">
assumes the previous type to pack/unpack is the string's length. This is<br class="gmail_msg">
an interesting idea in of itself, but doesn't handle the null-terminated<br class="gmail_msg">
string chase. I know $ is similar to pascal strings, but sometimes you<br class="gmail_msg">
need more than 255 characters :p.<br class="gmail_msg">
<br class="gmail_msg">
For null-terminated strings, it may be simpler to have a specifier for<br class="gmail_msg">
those. I propose 0, but this point can be bikeshedded over endlessly if<br class="gmail_msg">
desired ;) (I thought about using n/N but they're :P).<br class="gmail_msg">
<br class="gmail_msg">
It's worth noting that (maybe one of?) Perl's equivalent to the struct<br class="gmail_msg">
module, whose name escapes me atm, has a module which can handle this<br class="gmail_msg">
case. I can't remember if it handled variable length or zero-terminated<br class="gmail_msg">
though; maybe it did both. Perl is more or less my 10th language. :p<br class="gmail_msg">
<br class="gmail_msg">
This pain point is an annoyance imo and would greatly simplify a lot of<br class="gmail_msg">
code if implemented, or something like it. I'd be happy to take a look<br class="gmail_msg">
at implementing it if the idea is received sufficiently warmly.<br class="gmail_msg">
<br class="gmail_msg">
--<br class="gmail_msg">
Elizabeth<br class="gmail_msg">
_______________________________________________<br class="gmail_msg">
Python-ideas mailing list<br class="gmail_msg">
<a href="mailto:Python-ideas@python.org" class="gmail_msg" target="_blank">Python-ideas@python.org</a><br class="gmail_msg">
<a href="https://mail.python.org/mailman/listinfo/python-ideas" rel="noreferrer" class="gmail_msg" target="_blank">https://mail.python.org/mailman/listinfo/python-ideas</a><br class="gmail_msg">
Code of Conduct: <a href="http://python.org/psf/codeofconduct/" rel="noreferrer" class="gmail_msg" target="_blank">http://python.org/psf/codeofconduct/</a><br class="gmail_msg">
</blockquote></div>