|
Bradley Smith wrote:
Just a note... you don't have to make the 2nd arg of that gsub a function... you could do this for a faster approach:Yuri Takhteyev wrote:I am wondering why you find this omission so shocking? When I ran into the need for a function like this myself, I just added it in my own app and moved on: function escape(text)return (text or ""):gsub("&", "&"):gsub(">",">"):gsub("<","<")endOr this alternative: function escape(text) local escaped = { ['<']='<', ['>']='>', ["&"]='&' } return text:gsub('[<>&]', function(c) return escaped[c] end) end print(escape('2 < 3 & 10 > 9'))
-- Pull escaped 'const' out local escaped = { ['<']='<', ['>']='>', ["&"]='&' } function escape(text) return text:gsub('[<>&]', escaped) endThis allows gsub to skip rapidly calling a function and instead directly do the replacement... (though if you had more complicated logic, you'd have to change the setup (or pre-populate a table w/ responses [or even take advantage of metatables and have the function called only for unknown responses and cache them])