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 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.
Maybe ;)
-- Roberto