Creating chainable functions that manipulate strings
#1
I'm working on a bunch of plugin message related things, and it occurred to me that having a chainable set of functions would be great. I have functions for reading and writing UTFs (strings), Bytes (8 bit numbers), Shorts (16 bit numbers), Ints (32...), and Longs (64..)*.

Currently the "write" functions return that section of the "string" that will eventually form a message. So, for example, to send a BungeeCord Connect plugin message the code would be:
cPlayer:GetClientHandle():SendPluginMessage("BungeeCord",writeUTF("Connect") .. writeUTF("Server Name"))
What I would like is to be able to do it like this:
cPlayer:GetClientHandle():SendPluginMessage("BungeeCord",writeUTF("Connect"):writeUTF("Server Name"))
or at least:
cPlayer:GetClientHandle():SendPluginMessage("BungeeCord","":writeUTF("Connect"):writeUTF("Server Name"))
Is there a way I could do that? I tried:
function writeUTF(self,a_in) --String
  assert(type(self) == "string", "Not a string!")
  if a_in then
    assert(type(a_in) == "string", "Not a string!")
    local len = a_in:len()
    return self .. string.char((len-127)/256,len % 256) .. a_in
  else
    local len = self:len()
    return string.char((len-127)/256,len % 256) .. self
  end
end
and the same thing but with self replaced with a_str. I didn't EXPECT those to work as self is supposed to be an object (itself haha?), but I was really hoping it would work. How do the string functions do it? My google foo was not enough to get anything more than manuals on what they do in the code, not behind the scenes.

I have "read" functions for the same datatypes*. Currently, they return the value they read and the part of the "message" string that remains after removing the translated section. So
readUTF("\0\5Hello\0\2Hi")
returns
"Hello", "\0\2Hi"
How could I make THESE chainable? It may be easier as I can return an array instead of the actual value, so that instead of "Hello", "\0\2Hi", the above would return {1="Hello"},"\0\2Hi", then chaining onto that result, the output would be {1="Hello",2="Hi"},"" and that's the end of that.

Finally, I want to make a writeSmartInteg(#) that identifies the smallest number type that # can be stored in and stores it as such and readSmartInteg(string) that figures out what type of number is being presented based on the first two bytes (\0\1 is always byte, \0\2 short, etc) and returns the number.

To make those chainable, I should just have to format it the same way and have it carefully call the right read/write functions. It don't think it would be difficult to do this, but then again I have no idea how hard chaining functions that act on strings will be so.. yeah.



*I originally planned on including read/writeFloat, read/writeDouble, and read/writeUTF16, but those proved an absolutely massive hassle. I'll likely get around to the Float and Double eventually, but I doubt I'll ever work on UTF16.
Reply
Thanks given by:


Messages In This Thread
Creating chainable functions that manipulate strings - by Zee1234 - 10-28-2015, 05:49 AM



Users browsing this thread: 2 Guest(s)