Suggestion for indexing strings

Posted by Nick Gammon on Fri 26 Nov 2010 07:56 PM — 9 posts, 32,138 views.

Australia Forum Administrator #0
I saw on the Lua mailing list a cool suggestion for string indexing.

The gist is to make a simpler syntax for pulling out an individual character from a string. For example, if you want to find the first character you currently do:


a = "nick"
print (string.sub (a, 1, 1)) --> n


This is a little ugly. Now if you add the following code to the initialization of your Lua code:


getmetatable ("").__index = function (str, i)

  if (type (i) == "number") then
    return string.sub (str, i, i)  -- index into str
  end -- if

  return string [i]  -- fallback (eg. string.match)
end -- function


What that does is add a metamethod for handling the indexing into any string (since all Lua strings share the same metatable).

Now you can do this:


a = "nick"
print (a [1])  --> n


[EDIT]

Modified per discussion below, so the discussion may not make a heap of sense.
Amended on Fri 26 Nov 2010 10:00 PM by Nick Gammon
USA #1
You might want to use string.sub(str) instead of str:sub(), since you're incurring needless overhead by invoking the metamethod again.

Improvement allowing for a range:

getmetatable ("").__index = function (str, i)
  if (type (i) == "number") then
    return string.sub (str, i, i)  -- index into string
  elseif (type (i) == "table") then
    return string.sub (str, i[1], i[2])
  else
    return string [i]  -- fallback
  end  -- if
end -- function


[EDIT]: It's a pity you can't make the same syntax work for modifying strings.
Amended on Fri 26 Nov 2010 08:21 PM by Twisol
Australia Forum Administrator #2
Twisol said:

You might want to use string.sub(str) instead of str:sub(), since you're incurring needless overhead by invoking the metamethod again.



I had that initially and changed it. I was trying to avoid the overhead of:

  • Looking up "string" in the global environment
  • Looking up "sub" in that table


Whereas doing str:sub hopefully only is one lookup (the metatable). Am I wrong?

As for the table, I think that might be getting as complicated as the original.


print ( a [ { 2, 3 } ] )


instead of:


print ( a:sub (2, 3) )


USA #3
Nick Gammon said:
I had that initially and changed it. I was trying to avoid the overhead of:


*Looking up "string" in the global environment
*Looking up "sub" in that table


Whereas doing str:sub hopefully only is one lookup (the metatable). Am I wrong?

Well, it's one lookup plus a function call (the metamethod), which probably involves more Lua bytecode than just a lookup. And you can cache 'string' in a local. In fact, you can cache 'sub' in a local, too:

local string = string
local sub = string.sub
getmetatable ("").__index = function (str, i)
  if (type (i) == "number") then
    return sub (str, i, i)  -- index into string
  else
    return string [i]  -- fallback
  end  -- if
end -- function


Nick Gammon said:
As for the table, I think that might be getting as complicated as the original.

Touche. I was trying to go for the Ruby syntax of str[1,2], but you can't pass multiple values to the index metamethod.
Amended on Fri 26 Nov 2010 08:50 PM by Twisol
Australia Forum Administrator #4
It seems you may have a point. I think you added in caching string.sub while I was replying, but the difference was minor as it turned out.

Test bed:


collectgarbage ( )

local a, b = "nick"
start = utils.timer ()

for i = 1, 1000000 do
  b = a [3]
end -- for

print (string.format ("Time taken = %0.2f", utils.timer () - start))


Results in speed order:


 return sub (str, i, i)         --> 0.42 (string.sub cached) 
 return string.sub (str, i, i)  --> 0.43 (string cached)   
 return string.sub (str, i, i)  --> 0.47 secs
 return str:sub (i, i)          --> 0.72 secs


So my original was about 1.5 as fast than my improved version. Caching the string.sub lookup helped, but not by much.

I'll change the original post back to the simpler version.
Amended on Fri 26 Nov 2010 09:23 PM by Nick Gammon
Australia Forum Administrator #5
Meanwhile I'm not sure about the fallback line. Surely if you do, say:


a = "abcd"
print (a ["x"])


... you are indexing into the key "x" in string "a"? (Not key "x" in the string table). However somewhat surprisingly it seems to work in the sense that it returns nil.

However wouldn't it always?

So perhaps the fallback should be either:



  return nil  -- fallback

-- or

  error ("Attempting non-numeric index into string")

Amended on Fri 26 Nov 2010 09:27 PM by Nick Gammon
Australia Forum Administrator #6
Or even:


  return ""   -- fallback


... on the basis that indexing out of range returns an empty string.
USA #7
You need to leave it as string[i]. Otherwise you prevent users from doing str:gsub() or anything else in 'string' using the shorthand.
Amended on Fri 26 Nov 2010 09:37 PM by Twisol
Australia Forum Administrator #8
Aha. That makes sense.