Creating chainable functions that manipulate strings
#8
(10-28-2015, 07:48 PM)xoft Wrote: I think this would be much better suited as a C++ implementation exported to Lua. It's far easier to do that bit twiddling in C++. To be honest, I'd have no idea how to read and write floats and doubles in Lua only, considering that the Lua's number type can internally be an integer, float or double, based on compile-time settings.

Oh it most certainly would be. IIRC it's something like 4 lines of C to do the conversion and one line to push to lua. Part of this is me wanting to get better at programming. Part of it is a challenge. And now, part of it is proving it's possibleTongue

But the basic idea is you use string.char and string.byte with some careful math to produce manipulation of individual bits within the four byte sequence.

Edit: And it's done! I'm still deeming float and double manipulation as experimental, as they seem to only retain accuracy to ~2 decimal places between translations (something that could be fixed with the C++ implementation as C++ has proper bit/byte manipulation instead of the rather large number of math.floor I used), but I've now got a plugin message library that can read and write Strings, Bytes, Shorts, Ints, Longs, Bools, Floats, and Doubles in a chainable manner!

For reference: To convert between the bytestring received with plugin messages and a float, you take the first 4 bytes of the bytestring and change their definition from a string to a float. Literally that's it. I don't think it's that simple to code, but that is the process you'd need to emulate to get it into C++. I've seen some code snippets where they use byte unions to do this. For Doubles, it's exactly the same but with 8 bytes. The other direction is literally just that. \63\128\0\0 is the binary representation of the float number 1. The string is literally composed of \63\128\0\0.

If you/someone else were to give us a Lua function that "calls" the C++ function, I'd be even happier than I am now.
Reply
Thanks given by:


Messages In This Thread
RE: Creating chainable functions that manipulate strings - by Zee1234 - 10-29-2015, 03:11 AM



Users browsing this thread: 1 Guest(s)