lua-users home
lua-l archive

[Date Prev][Date Next][Thread Prev][Thread Next] [Date Index] [Thread Index]



On 12/12/14 04:18 PM, Dirk Laurie wrote:
2014-12-12 17:57 GMT+02:00 Thiago L. <fakedme@gmail.com>:

What if I want to be able to stick any pattern on it without having to write
a new one every time?
What about putting some thought into answering your own questions
before running to the list? You are not a beginner by any means.

I was having issues making it work properly... but I finally managed to do it (stateless)

local findpairs
do
  local function _findnext(stringandpattern, index)
    -- only branch if index = 0
    if index > 0 then
      -- this should always give a match,
      -- as we're starting from the same index as the returned match
local x,y = string.find(stringandpattern[1], stringandpattern[2], index)
      return string.find(stringandpattern[1], stringandpattern[2], y+1)
    else
return string.find(stringandpattern[1], stringandpattern[2], index + 1)
    end
  end
  function findpairs(string, pattern)
    return _findnext, {string, pattern}, 0
  end
end

--
Disclaimer: these emails are public and can be accessed from <TODO: get a non-DHCP IP and put it here>. If you do not agree with this, DO NOT REPLY.