lua-users home
lua-l archive

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

I disagree, I think it's a bug.
Bugs aren't just about getting valid output for valid inputs, it's also about giving clear and understandable errors whenever possible.
string.format throws an error if it encounters an unterminated "%", so it only makes sense that gsub should too.
Silently ignoring the error and doing something completely different is nog very friendly behaviour.

> = string.format("%")
stdin:1: invalid option '%' to 'format'

On Fri, Jan 23, 2009 at 2:57 PM, Roberto Ierusalimschy <> wrote:
> please take a look at the following code:
> ----BEGIN---
> str = [[This is a string with @@ANYTHING@@ chance of survival]];
> replace = "5%"
> print(str)
> print(replace)
> str = str:gsub("@@ANYTHING@@", replace)
> print(str)
> -----END----
> The result I have here is
> ----BEGIN---
> This is a string with @@ANYTHING@@ chance of survival
> 5%
> This is a string with 5
> -----END----
> But the last sentence should be "This is a string with 5% chance of
> survival".

Actually, the last sentence is not what you are seeing, and it should not
be what you said. The last sentence is actually

 This is a string with 5\0 chance of survival

The '%' is an escape in the replacement string too. It should be followed
by some character. As it is not, Lua is using the terminating 0 instead.
It would be nicer if Lua raised an error in this case, but I am not sure
this is a bug: it simply follows the rule garbage in -> garbage out.

> Do I get to have my name on

Maybe ;)

-- Roberto