Re: Reporting possible BUG on string.gsub
[
Date Prev][
Date Next][
Thread Prev][
Thread Next]
[
Date Index]
[
Thread Index]
- Subject: Re: Reporting possible BUG on string.gsub
- From: Kristofer Karlsson <kristofer.karlsson@...>
- Date: 2009年1月23日 17:23:55 +0100
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
<roberto@inf.puc-rio.br> 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 50円 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.
Maybe ;)
-- Roberto