lua-users home
lua-l archive

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


> Here is a version using a light wrapper aroung string.gmatch. Because of
> the way gmatch is implemented (I think that was the reason exposed on
> this list when the issue has been raised), it works better if you
> concatenate a delimiter at the end of the string to split. But if the
> delimiter contains magic characters, you have to provide a simpler
> version that match the magic characters (term in the example below, for
> terminator). Tested with stock Lua 5.1:

Just to add my $.02, here is a terminator-free version. It should be slower
then Jerome's version, of course, but the convenience of using a magic
delimiter without the terminator might pay off if there's no need to split too
many lines. :)

local str1 = "foo bar baf"
local str2 = "foo<space type='a'>bar<space type='b'>baf"

function split(s, d)
  local d = d or "%s"
  local splitaux
  splitaux = function(s)
    local x, y = string.find(s, d)
    if not x then
      coroutine.yield(s)
    else
      coroutine.yield(string.sub(s, 1, x-1))
      return splitaux(string.sub(s, y+1))
    end
  end
  return coroutine.wrap(function() return splitaux(s) end)
end

for token in split(str1) do
  print(token)
end

for token in split(str2, "<space[^>]*>") do
  print(token)
end

Cheers,
Luis.

-- 
A mathematician is a device for turning coffee into theorems.
        -- P. Erdos 

-- 
Luis Carvalho
Applied Math PhD Student - Brown University
PGP Key: E820854A <carvalho@dam.brown.edu>