lua-users home
lua-l archive

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


On Fri, Aug 22, 2014 at 12:31 AM, Dirk Laurie <dirk.laurie@gmail.com> wrote:

> Another con is that it would be a fifth parameter, preceded by one that
> is usually nil. No standard library routine in Lua 5.2 has that many
> non-vararg parameters.

I would change the form from:

string.gsub (s, pattern, repl [, n])

to:

string.gsub (s, pattern, repl [[plain [, n]])

In my mind, this isn't very confusing.  It's not pretty, but it's
something I think people would learn to get used to like the optional
2nd parameter to table.insert()

string.gsub('replace this and this', 'this', 'that', true, 1) -- it
seems fairly reasonable to me

If the 4th optional arg is a boolean it assumes it as the plain match
option.  If there are more than 3 args and the *last* argument is a
number that is the number of times to do the replacement.  I don't
think this would be very tricky in C... hmm.

The best argument (in my opinion) is that it's not very obvious how to
set this behavior up with string.find() and string.sub() and such.
I've rarely needed a plain gsub(), but when I have it's been very
tedious to go look through the docs to remember what to escape.  I
would welcome this slight prototype mangling just to avoid another
"docrun" heh.