[Date Prev][Date Next][Thread Prev][Thread Next]
[Date Index]
[Thread Index]
- Subject: Re: A small motivating example. Was: small incompatibility
- From: Rici Lake <lua@...>
- Date: Mon, 24 Oct 2005 17:13:20 -0500
On 24-Oct-05, at 4:52 PM, Rici Lake wrote:
In order to test this function, I had to implement the str.gsub
behaviour, of course. The rules I used are:
1) nil or false: leave the original string intact
2) true: delete the match (i.e. replace with "")
3) string: replace %x as in current implementation
4) table: lookup the first capture in the table, and
continue. (If another table is encountered,
use the next capture as the index.)
5) function: call the function with all captures. If it
returns a boolean or nil, treat it as above;
if it returns a string, use the string as the
replacement without interpreting %x. Otherwise,
throw an error.
I forgot to add the following comment:
There seems to me to be a semantic ambiguity about the definition of
"number of matches" in the case where the match is rejected by the use
of a nil/false value. On the one hand, there is a sense in which this
is "not a match"; i.e., the semantic intent was to not do the replace.
On the other hand, it's handy to be able to use gsub simply in order to
count the number of matches:
function wc(input)
local _, wordcount = input:gsub("%S+", nil)
local _, linecount = input:gsub("\n", nil)
return linecount, wordcount, #input
end
This will work with the patched version, and the copy-elimination
optimization seems to help, but again, I haven't done any serious
benchmarking. (This implementation of wc appears to be about 6 times
slower than the standard C implementation, tested on a 2 MB file:
0.207s vs 0.034s. With the nils changed to true, the time goes up to
0.321s.)
> function wc(input)
>> local _, wordcount = input:gsub("%S+", nil)
>> local _, linecount = input:gsub("\n", nil)
>> return linecount, wordcount, #input
>> end
> =wc(io.open"src/lstrlib.c":read"*a")
883 3272 23805
>
rlake@freeb:~/src/lua-5.1-alpha$ wc src/lstrlib.c
883 3272 23805 src/lstrlib.c