lua-users home
lua-l archive

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


On Friday, August 22, 2014 12:17:09 AM Petite Abeille wrote:
> function replace( s, this, that ) return ( s:gsub( escape( this ), escape(
> that ) ) ) end

Or...

    local p, q = s:find(this, 1, true)
    if p then s = s:sub(1, p-1) .. that .. s:sub(q+1) end

Still just three function calls and avoids the pattern matching machine which 
is the whole reason of having a plain option.

Although it is well established that a plain option to gsub is not strictly 
necessary, I don't think I've heard a compelling reason not to have it.

Pro: Avoid the overhead of pattern matching. Will be backward-compatible.

Con: Adds to code maintenance. Can be trivially emulated. May only be modestly 
faster than find/sub.
    
-- 
tom <telliamed@whoopdedo.org>