lua-users home
lua-l archive

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


2015-11-07 17:52 GMT+02:00 Marco Atzori <marco.atzori.1983@gmail.com>:
> Il 07/11/2015 16:46, Matthew Wild ha scritto:
>>
>> If you have actual constraints, revealing them might allow people to
>> help you better. If you don't have any constraints and just want it to
>> perform well, then there are some really much simpler and more
>> efficient solutions than your implementation.
>
> This code is part of a videogames addon, and is executed every frame refresh
> (about every tenth of a second) for which using tables created in
> continuation is a considerable waste of memory. If there are simpler
> solutions, show them to me as well. I asked your help for this reason ;-)

[from original post]
   gettok(text,-1,46,-3)

I cannot envisage why that argument '46' should be needed at all.

I would use a table created once and for all for each string.

local tok_meta = { __index = { gettok =
   function(tbl,i,j)
     local n=#tbl
     j = j or i
     if i and i<0 then i=n+i+1 end
     if j and j<0 then j=n+j+1 end
   return table.concat(tbl,tbl.sep,i,j) end }}

local tokens = function(str,sep)
   local fields={sep=sep}
   str:gsub(("[^%s]+"):format(sep),function(field)
      fields[#fields+1]=field
      end)
   return setmetatable(fields,tok_meta)
end

local s = tokens( "apple.banana.cherry.grape.orange",".")

> s:gettok(1)
apple
> s:gettok(-2)
grape
> s:gettok(2,4)
banana.cherry.grape
> s:gettok(-3,-1)
cherry.grape.orange