KrYn0MoRe

custom string pack

May 28th, 2021 (edited)
462
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Lua 0.68 KB | None | 0 0
  1. local arg_split = utf8.char(2^16-2)
  2.  
  3. local function sargs(str)
  4.     local r = string.split(str,arg_split)
  5.     local t = {}
  6.     if 0 >= #r then
  7.         r = {str}
  8.     end
  9.     for i,v in pairs(r) do
  10.         if not (v and #v > 0) then
  11.             r[i] = nil
  12.         end
  13.     end
  14.     local c = 0
  15.     for i,v in pairs(r) do
  16.         c = c + 1
  17.         t[c] = v
  18.     end
  19.     return t
  20. end
  21.  
  22. local function unpack_str(str)
  23.     return table.unpack(sargs(str))
  24. end
  25.  
  26. local function pack_str(...)
  27.     local t = {...}
  28.     local r = ''
  29.     for i,s in pairs(t) do
  30.         s = string.gsub(s,arg_split,'')
  31.         r = r .. s
  32.         if #t > i then
  33.             r = r .. arg_split
  34.         end
  35.     end
  36.     return r
  37. end
  38.  
  39. -- toast
  40.  
  41. local packed = pack_str('a','b','c')
  42. print(packed)
  43. warn(unpack_str(packed))
Add Comment
Please, Sign In to add comment