lua-users home
lua-l archive

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


On Mon, May 5, 2014 at 10:14 PM, Thomas Jericke <tjericke@indel.ch> wrote:

> It would only be a problem for user defined metatables for string that
> define numerical index, with a different meaning.


I think I would amend how I originally wrote the __index:

debug.getmetatable('').__index = function (s, k) return string[k] or
string.byte(s, k, k) end

Now it works even if the user puts a numeric key in the string library
table :-)  But really unpredictably breaks indexing individual
characters...

Anyway, since doing the benchmark for something that seemed quite
one-sided, I think I would just prefer calling string.byte() directly:

for i = 1, #s do
    local c = string.byte(s, i, i)
    ...
end