rrthomas / lrexlib

A Lua (5.1 and later) binding of various regex library APIs (POSIX, PCRE, PCRE2, GNU, Oniguruma and TRE)
Other
161 stars 29 forks source link

rex.gsub: pros and cons of allowing nil/false as the replacement argument #10

Closed shmuz closed 10 years ago

shmuz commented 10 years ago

As far as I can see nil/false as the replacement argument serves the only purpose: to emphasize the caller's intention of doing no replacements in the case gsub is used for counting matches in the subject. This is a pro (though not a big one, as "%0" does the same thing and is no less expressive).

Cons:

  1. We further complicate the already very complicated API of rex.gsub
  2. We break the widely used Lua convention that a missing trailing argument can be treated as nil

I propose:

  1. (at least) Remove this feature.
  2. (optionally) Add a new function rex.count that will be implemented internally on the base of rex.gmatch and therefore (1) will be faster and (2) will show the caller's intention much more clear than gsub(s,p,nil). I'd be happy to implement this function.
rrthomas commented 10 years ago

Here's the most relevant part of the previous discussion that I can find:

On 17 October 2012 00:48, Shmuel Zeigerman shmuz@013net.net wrote:

  1. Enhance gsub so that when it detects repl=="%0" it does not create the output buffer, so it is as efficient as in your proposal. There is even a bonus - no need to introduce a new special case for the library users.

This seems to me equivalent to my proposal: it would require documentation (otherwise users won't know it's efficient!) and possibly slightly more complex implementation than my suggestion. Also, it's not obvious to someone reading the code that there's anything different about it.

I was worried about efficiency and expressivity in the case of using "%0", so I would say that I'd prefer both of your options to be implemented.