From StrategyWiki, the video game walkthrough and strategy guide wiki
Jump to navigation Jump to search
(test)
(removed broken/unused functions)
 
(7 intermediate revisions by the same user not shown)
Line 3: Line 3:
]]--
]]--
local p = {}
local p = {}
 
function p.explode(frame)
function p.explode(frame)
--[[
--[[
By Notmyhandle
By Notmyhandle, working August 9th, 2014, compatibility is Lua 5.1 or 5.2, not sure
args:
args:
1:string
1:string
Line 13: Line 13:
   --If no string is present, don't waste any more time.
   --If no string is present, don't waste any more time.
   if (frame.args[1]=='') then return false end
   if (frame.args[1]=='') then return false end
 
   local finalstring,rindex,s,e = 0
   local finalstring,rindex,s,e = 0
   local rindexdebug = "string"
   local rindexdebug,rindexnegativedebug,capturedstring,capturedstringdebug = "string"
   --capture the starting index
   --capture the starting index
   s = string.find(frame.args[1],frame.args[2])
   s = string.find(frame.args[1],frame.args[2])
 
   if (frame.args[3]=='left') then
   if (frame.args[3]=='left') then
   --capture the index of the left side of the first delimiter character
   --capture the index of the left side of the first delimiter character
     rindex = (s)
     rindex = (s-1)
     rindexdebug = string.format ("%s %d", "rindex = ", rindex)
     capturedstring = string.sub(frame.args[1], 1, rindex)
    finalstring = rindexdebug + string.sub(frame.args[1], (rindex*-1), 0)
    if (frame.args[4]=='debug') then
      rindexdebug = string.format("%s %d %s", "rindex = ", rindex, "<br>")
      rindexnegative = rindex * -1
      rindexnegativedebug = string.format("%s %d %s", "rindexnegative = ", rindexnegative, "<br>")
      capturedstringdebug = string.format("%s %s", "capturedstring = ", capturedstring)
      finalstring = string.format("%s %s %s", rindexdebug, rindexnegativedebug, capturedstringdebug)
    else
      finalstring = capturedstring
    end
   else
   else
   --capture the index of the right side of the first delimiter character
   --capture the index of the right side of the first delimiter character
Line 29: Line 37:
     finalstring = string.sub(frame.args[1], rindex)
     finalstring = string.sub(frame.args[1], rindex)
   end
   end
 
   return finalstring
   return finalstring
 
end
 
 
function p.explode2(frame)
 
--[[
args:
1:delimiter character (divider character)
2:string
]]--
 
  if (frame.args[1]=='') then return false end
  local pos,arr = 0,{}
  -- for each divider found
  for st,sp in function() return string.find(frame.args[2],frame.args[1],pos,true) end do
    table.insert(arr,string.sub(frame.args[2],pos,st-1)) -- Attach chars left of current divider
    pos = sp + 1 -- Jump past current divider
  end
  table.insert(arr,string.sub(frame.args[2],pos)) -- Attach chars right of last divider
  return arr
 
end
end


Line 65: Line 52:
]]--
]]--
   return string.find(frame.args[1],frame.args[2],frame.args[3],frame.args[4])
   return string.find(frame.args[1],frame.args[2],frame.args[3],frame.args[4])
end
function p.split(frame)
--[[
args:
1:string
2:delimiter character (divider character)
]]--
  local t = {}  -- NOTE: use {n = 0} in Lua-5.0
  local fpat = "(.-)" .. frame.args[2]
  local last_end = 1
  local s, e, cap = frame.args[1]:find(fpat, 1)
  while s do
      if s ~= 1 or cap ~= "" then
    table.insert(t,cap)
      end
      last_end = e+1
      s, e, cap = frame.args[1]:find(fpat, last_end)
  end
  if last_end <= #frame.args[1] then
      cap = frame.args[1]:sub(last_end)
      table.insert(t, cap)
  end
  return t
end
end


return p
return p

Latest revision as of 22:53, 9 August 2014

Documentation for this module may be created at Module:Explode/Documentation

--[[
This module is used to explode a passed string into parts. First parameter is the divider string to divide the string at. The second parameter is the string.
]]--
local p = {}
 
function p.explode(frame)
--[[
By Notmyhandle, working August 9th, 2014, compatibility is Lua 5.1 or 5.2, not sure
args:
1:string
2:delimiter character (divider character)
]]--
  --If no string is present, don't waste any more time.
  if (frame.args[1]=='') then return false end
 
  local finalstring,rindex,s,e = 0
  local rindexdebug,rindexnegativedebug,capturedstring,capturedstringdebug = "string"
  --capture the starting index
  s = string.find(frame.args[1],frame.args[2])
 
  if (frame.args[3]=='left') then
  --capture the index of the left side of the first delimiter character
    rindex = (s-1)
    capturedstring = string.sub(frame.args[1], 1, rindex)
    if (frame.args[4]=='debug') then
      rindexdebug = string.format("%s %d %s", "rindex = ", rindex, "<br>")
      rindexnegative = rindex * -1
      rindexnegativedebug = string.format("%s %d %s", "rindexnegative = ", rindexnegative, "<br>")
      capturedstringdebug = string.format("%s %s", "capturedstring = ", capturedstring)
      finalstring = string.format("%s %s %s", rindexdebug, rindexnegativedebug, capturedstringdebug)
    else
      finalstring = capturedstring
    end
  else
  --capture the index of the right side of the first delimiter character
    rindex = (s+1)
    finalstring = string.sub(frame.args[1], rindex)
  end
 
  return finalstring
 
end

function p.find(frame)
--[[
args:
1:string
2:delimiter character (divider character)
3:starting position (far left if not set)
4:set to 'true' if you want plain matching enabled.
See http://lua-users.org/wiki/StringLibraryTutorial
]]--
  return string.find(frame.args[1],frame.args[2],frame.args[3],frame.args[4])
end

return p