[Date Prev][Date Next][Thread Prev][Thread Next]
[Date Index]
[Thread Index]
- Subject: Re: io.format (was: Reported to the Bloat Police: string.pack, .unpack and .packsize!)
- From: Sean Conner <sean@...>
- Date: Fri, 21 Aug 2015 16:58:51 -0400
It was thus said that the Great John Hind once stated:
> > Responding to (the Great) Sean Conner, Thu, 20 Aug 2015 12:47:14 -0400:
>
> > I'm not sure what your point is here. Yes, this:
> >
> > syslog('emergency',"%s stat(%q) = %s",dbtype,conf.file,errno[err])
> >
> >could be replaced with:
> >
> > syslog('emergency',dbtype .. ' stat("' .. conf.file .. '") = ' .. errno[err])
>
> I really could not have come up with a better example myself! Frankly the
> second Version "expresses intent" *much* more clearly than the first. I
> would have to look up "%q" in a reference manual and the whole pattern
> concept is opaque unless you are steeped in "C".
On a lark, I looked up how to format strings in some other languages. I
checked Perl, Go, Python, Java, D, PHP and Ruby (not a representative
sample) and all use the C style format string (with Python having a *second*
format style it can use). Common Lisp has formatting strings, but it
replaces the '%' for '~' and the following letters have different meanings
than in C. Erlang too has a format string using a leading '~' instead of a
'%', and like Lisp, the following letter has different meanings than in C.
So for better or worse, the C style format strings appeared to have
influenced quite a few languages that came after it. And those that don't
use C format strings do tend to have format strings, but they are radically
different from C's.
> > One use case that comes up from time to time that you haven't covered is
> >moving data from one Lua state on one computer to another Lua state on
> >another computer (that may very well be a different architecture). Yes,
> >networking will probably be needed, but you don't need a multi-threaded
> >library for this case.
>
> A good case for serialising to Lua. As you say you need at least one other
> Library so pack/unpack may as well be an external library too.
But there's always serialization to disk. No external library needed to
write to disk.
> >I was unaware that ZIP files were a proprietary binary format. I wonder
> >whose intellectual property I'm violating by decoding DNS packets or CBOR
> >formatted data.
>
> This is a nit-pick - of course I realise there are non-proprietary binary
> formats too! And good luck with reading or writing ZIP files using Lua
> 5.3!
You make it sound like it will be difficult to parse ZIP files with Lua
5.3.
> >In Lua 5.1, to read in a 32-bit value from network byte order to native
> >order, I have the following code:
>
> I will not quote the whole thing, but what you left out was the
> implementation using 5.3 bit-wise shift operators
Because it was too similar to the Lua 5.1 version. But here's both to
compare and contrast:
val = x:sub(1,1):byte() * 2^24
+ x:sub(2,2):byte() * 2^16
+ x:sub(3,3):byte() * 2^8
+ x:sub(4,4):byte()
val = x:sub(1,1):byte() << 24
| x:sub(2,2):byte() << 16
| x:sub(3,3):byte() << 8
| x:sub(4,4):byte()
> which is clear, straight-forward and efficient and "expresses intent" much
> more clearly than pack/unpack with its arbitrary pattern syntax.
I wouldn't call the pack/unpack syntax "arbitrary"---arbitrary would be
something like:
val = string.unpack("\\|$")
> >Bit packing might cause even more bloat.
>
> That is a challenge - bit packing in less than 500 lines of C! I'll give
> it a go when I Get a moment.
Don't forget unpacking!
-spc