Advertisement
Rochet2

GetParams from function (split)

Aug 29th, 2014
447
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Lua 1.21 KB | None | 0 0
  1. local sub = string.sub
  2. local gsub = string.gsub
  3. local find = string.find
  4. local tinsert = _G.tinsert or table.insert
  5. local function getParams(str)
  6.     local len = str:len()
  7.     local params = {}
  8.     local i, j = find(str, "%S+")
  9.     while (i and j) do
  10.         if (find(str, '"', i) == i) then
  11.             _, j = find(str, '"', i+1)
  12.             while (true) do
  13.                 if (not j) then
  14.                     return params
  15.                 end
  16.                 if (j+1 > len) then
  17.                     break
  18.                 end
  19.                 if (j <= 1 or find(str, "\\", j-1) ~= j-1) then
  20.                     break
  21.                 end
  22.                 _, j = find(str, '"', j+1)
  23.             end
  24.             local unquot = i+1 >= j+1 and "" or gsub(sub(str, i+1, j-1), '\\"', '"')
  25.             tinsert(params, unquot)
  26.         else
  27.             tinsert(params, sub(str, i, j))
  28.         end
  29.         if (j+1 > len) then
  30.             return params
  31.         end
  32.         i, j = find(str, "%S+", j+1)
  33.     end
  34.     return params
  35. end
  36.  
  37. -- Test code
  38. local cmd = 'something Rochet "attgasd" 1000 "Because I said \\"so\\""'
  39. print(cmd) -- User used command
  40.  
  41. for k, v in pairs(getParams(cmd)) do
  42.     print(k,v)
  43. end
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement