Advertisement
PaymentOption

gfind utility

Aug 7th, 2014
276
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Lua 0.77 KB | None | 0 0
  1. --[[
  2.         This function behaves just like gmatch, but it also returns the position
  3.     in the line of the match.
  4.  
  5.     @return iterator function which returns the start, stop, and textual match of every
  6.             occurence of the given pattern.
  7.  
  8.     Example:
  9.         for start, stop, match in gfind (str, pat) do
  10.             ...
  11.         end
  12. ]]
  13. local function gfind (_string, pattern)
  14.     if not _string or not pattern then
  15.         error ("Arguments to gfind are invalid: faulty string or pattern.")
  16.     end
  17.  
  18.     local start, stop;
  19.  
  20.     return function()
  21.         if stop then
  22.             stop = stop + 1
  23.         end
  24.         start, stop = _string:find (pattern, stop)
  25.  
  26.         return start, stop, (start ~= nil) and _string:sub (start, stop) or nil
  27.     end
  28. end
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement