Closed shmuz closed 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:
- 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.
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:
nil
I propose:
rex.count
that will be implemented internally on the base ofrex.gmatch
and therefore (1) will be faster and (2) will show the caller's intention much more clear thangsub(s,p,nil)
. I'd be happy to implement this function.