lua-users home
lua-l archive

Re: String indexing again

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


On Mon, Dec 20, 2010 at 10:01 AM, Dirk Laurie <dpl@sun.ac.za> wrote:
> I suppose that is the portable way.  OK, I'll shut up on this topic
> now.
As a last gasp, it's not difficult to write a custom type that does
provide index access to string 'characters' (modulo UTF-8 etc [1])
http://gist.github.com/748160
You use it like this:
sa = StringAccessor 'hello'
for c in sa:iter() do -- (see [2])
 print(c)
end
print(sa[1],sa[3])
print('end',sa[-1])
steve d.
[1] This kind of pattern would be useful for wrapping strings so that
one could properly extract non-ASCII characters....
[2] With Lua 5.2, ipairs can be overriden as well, making string
accessors more 'table-like'

AltStyle によって変換されたページ (->オリジナル) /