Murga-Projects Forums
Binary string library - Printable Version

+- Murga-Projects Forums (http://www.murga-projects.com/forum)
+-- Forum: Project Forums (/forumdisplay.php?fid=1)
+--- Forum: MurgaLua - General (/forumdisplay.php?fid=2)
+--- Thread: Binary string library (/showthread.php?tid=357)


Binary string library - jpjacobs - 12-23-2008 08:43 AM

Hi!

I know it's entirely off topic, but since there's not so much talk here anyway, I'll just dump itSmile
Instead of studying, I made a binary stringlib yesterday, based on the use of metatables. There's a newBin() function which takes about any sensible input (number, string of 0's and 1's optionally prefixed by 0b, table of trues and falses).
This returns a table of trues and falses _with_ a useful metatable defining operations as follows:
a+b = element by element xor
a*b = element by element and
-a = element by element inversion
a-b = a+ (-b)
a/b : gives error, if anyone has a useful suggestion for sensible behavior
a..b does what it's supposed to do, if one of them is a string then do string concat, if one is a number then convert it to bin first.

The metatable also has a __tostring function so they get printed in a sensible way.
The bins also come with a toDec method for easy conversion back to numbers.

I don't make any claims about correctness or performance, but suggestions are always welcome...

Right: you can find the thing here: http://jpjacobs.ulyssis.org/scripts/binlib.lua
greetz,
JP

BTW i'm really really looking forward to have a new murgaLua version Wink
BTW2 added lunit tests at http://jpjacobs.ulyssis.org/scripts/binlibtests.lua