Advertisement
alestane

Superfluous

Jun 10th, 2011
243
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Lua 0.86 KB | None | 0 0
  1.  
  2. function split(str, pat)
  3.    local t = {}
  4.    local fpat = "(.-)" .. pat
  5.    local last_end = 1
  6.    local s, e, cap = str:find(fpat, 1)
  7.    while s do
  8.       if s ~= 1 or cap ~= "" then
  9.      table.insert(t,cap)
  10.       end
  11.       last_end = e+1
  12.       s, e, cap = str:find(fpat, last_end)
  13.    end
  14.    if last_end <= #str then
  15.       cap = str:sub(last_end)
  16.       table.insert(t, cap)
  17.    end
  18.    return t
  19. end
  20.  
  21. function todict(str, pat1,pat2)
  22.    local t = {}
  23.    local fpat = "(.-)" .. pat1
  24.    local last_end = 1
  25.    local s, e, cap = str:find(fpat, 1)
  26.    while s do
  27.       if s ~= 1 or cap ~= "" then
  28.          x = split(cap,pat2)
  29.          t[x[1] ] = x[2]
  30.       end
  31.       last_end = e+1
  32.       s, e, cap = str:find(fpat, last_end)
  33.    end
  34.    if last_end <= #str then
  35.       cap = str:sub(last_end)
  36.       x = split(cap,pat2)
  37.       t[x[1] ] = x[2]
  38.    end
  39.    return t
  40. end
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement