[Date Prev][Date Next][Thread Prev][Thread Next]
[Date Index]
[Thread Index]
- Subject: Re: unquote?
- From: Petite Abeille <petite.abeille@...>
- Date: Tue, 4 Oct 2011 23:42:12 +0200
On Oct 4, 2011, at 11:32 PM, Geoff Leyland wrote:
>> Does anyone have a handy function to unquote a string.format %q transformation? I.e. reverse all the quoting performed by %q to revert to the original string, before quoting?
>
>
> print(loadstring("return "..("%q"):format("here's a string"))()) ?
Yes, that's a possibility. Unfortunately, as the string content is of unknown nature, simply executing it would be a bit reckless :)
I would rather have a simple string pattern or something.
lstrlib.c addquoted [1] performs quite a few substitutions, so if someone already went through the trouble of reversing it, that would be much appreciated :))
Also, looks like 5.2 implements quoting slightly differently:
static void addquoted (lua_State *L, luaL_Buffer *b, int arg) {
size_t l;
const char *s = luaL_checklstring(L, arg, &l);
luaL_addchar(b, '"');
while (l--) {
if (*s == '"' || *s == '\\' || *s == '\n') {
luaL_addchar(b, '\\');
luaL_addchar(b, *s);
}
else if (*s == '\0' || iscntrl(uchar(*s))) {
char buff[10];
if (!isdigit(uchar(*(s+1))))
sprintf(buff, "\\%d", (int)uchar(*s));
else
sprintf(buff, "\\%03d", (int)uchar(*s));
luaL_addstring(b, buff);
}
else
luaL_addchar(b, *s);
s++;
}
luaL_addchar(b, '"');
}
[1] http://www.lua.org/source/5.1/lstrlib.c.html#addquoted