lua-users home
lua-l archive

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



On 04/11/2006, at 8:39 PM, Shmuel Zeigerman wrote:

So each of the following calls

  string.gsub("a", "a", function() end)
  string.gsub("a", "a", {})

should return "a", 0

However each of them returns "a", 1

Judging by the Lua source, it is actually reporting the match count. In a sense, the count is accurate if you count replacing "a" by "a" as a replacement. However that does not fit with the quoted statement "then there is no replacement".

Probably the manual should be reworded as:

"gsub also returns, as its second value, the total number of matches made."

Alternatively, change the behaviour of Lua to actually not count the cases where the replacement isn't made. However in that case, do you count this example:

string.gsub ("a", "a", "a")

You could argue that replacing "a" by "a" is not really replacing it. Before Lua 5.1, if you wrote a replacement function you had to be careful to return the original text if you couldn't make a replacement, now returning nil does the same job.

- Nick