lua-users home
lua-l archive

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

hi there! :)

Roberto Ierusalimschy:

> The following line should do the trick:
>  s = string.gsub(s, "%W", "%%%0")

my solution was:
`string.escPattern=function(str) return str:gsub('%p', '%%%0') end`

(((im lying, it was actually:
`string.escPattern=function(str) return str:gsub('(%p)', '%%%1') end`
but the other variant looks better, and i think it should be more
optimal and micro optimization is important when the ice caps are
already melting! just think about those poor penguins and linux! :D

that means less chars to escape (white spaces and control charsr), and
a non-inverted char class is maybe (i guess) faster as it is more

and this should be legit, as the manual explicitly allows this:

"%x: (where x is any non-alphanumeric character) represents the
character x. This is the standard way to escape the magic characters.
Any punctuation character (even the non magic) can be preceded by a
'%' when used to represent itself in a pattern."

(so it says `'%W'` is legit, but the last sentence is the point, and
every magic chars are in `'%p'`, and i dont even believe that anything
else will ever become a magic char than punctuation chars...)

all the bests to all of u! :)