lua-users home
lua-l archive

Re: String represents arrays of characters.

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


Nick Gammon and I actually discussed that particular trick a bit here:
http://www.gammon.com.au/forum/?id=10778 . You should note that you're
effectively blocking the str:func() shorthand by doing that though.
----
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
end
----
~Jonathan Castello
On Mon, Dec 13, 2010 at 12:57 PM, Petite Abeille <petite_abeille@mac.com> wrote:
>
> On Dec 13, 2010, at 9:44 PM, Dirk Laurie wrote:
>
>> In the light of that statement, one can't be blamed for expecting
>> that s[i] means the i-th character of a string.  At present, s[i]
>> is nil, even if i is in the range 1 to #s -- another gotcha.
>
> Meh...
>
> getmetatable( '' )[ '__index' ] = function( aTable, anIndex ) return string.char( string.byte( aTable, anIndex ) ) end
>
>
>
>

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