[Date Prev][Date Next][Thread Prev][Thread Next]
[Date Index]
[Thread Index]
- Subject: The __tostring metamethod for strings
- From: Dirk Laurie <dirk.laurie@...>
- Date: Sat, 28 Jul 2018 16:31:52 +0200
The 'tostring' function calls a metamethod if one exists. The manual
is quite clear on that.
One might think that if the subject is already a string, the
__tostring metamethod is skipped, but that is not the case.
This feature could be exploited in many ways. Here is one.
getmetatable"".__tostring = function(s)
local function tostr(a)
if a<'!' or a>'~' then return a end
if a==' ' then return ' ' end
a = a:byte()+160
if a<256 then return string.char(239,188,a-64) end
return string.char(239,189,a-128)
end
return(s:gsub(".",tostr))
end
print"Hello, moon, the eclipse is over!"