[Date Prev][Date Next][Thread Prev][Thread Next]
[Date Index]
[Thread Index]
- Subject: Re: unescape lua string (opposite of %q)
- From: Jonathan Castello <twisolar@...>
- Date: Sat, 5 Jun 2010 00:06:49 -0700
On Fri, Jun 4, 2010 at 11:37 PM, Ricardo Ramos Massaro
<ricardo.massaro@gmail.com> wrote:
> On Sat, Jun 5, 2010 at 3:18 AM, Jonathan Castello <twisolar@gmail.com> wrote:
>> ----
>> local input = [["line1"\nline2]]
>> input = input:gsub([["]], [[\"]])
>> local output = loadstring("return \"" .. input .. "\"")()
>> ----
>
> This one fails if the input contains a backslash followed by a double quote.
>
> -Ricardo
>
Bleh, good catch. I can't believe I forgot about these two.
----
function reverse_q(str)
str = str:gsub([[\?"]], [[\"]])
return (assert(loadstring("return \"" .. str .. "\""))())
end
----
Test: print(reverse_q[[\\\n\"]])
Output:
\
"
Those two should be the only ones you have to worry about, I think. \
is obvious, and " is because I'm using \" in loadstring.
~Jonathan