string.format("%q", str) is now fully reversible (from Lua 5.2).

This commit is contained in:
Mike Pall 2010-11-19 18:22:08 +01:00
parent 29b8959df1
commit 7cc981c140

View File

@ -653,20 +653,18 @@ static void addquoted(lua_State *L, luaL_Buffer *b, int arg)
const char *s = strdata(str); const char *s = strdata(str);
luaL_addchar(b, '"'); luaL_addchar(b, '"');
while (len--) { while (len--) {
switch (*s) { if (*s == '"' || *s == '\\' || *s == '\n') {
case '"': case '\\': case '\n':
luaL_addchar(b, '\\'); luaL_addchar(b, '\\');
luaL_addchar(b, *s); luaL_addchar(b, *s);
break; } else if (lj_char_iscntrl(uchar(*s))) {
case '\r': uint32_t c1, c2, c3;
luaL_addlstring(b, "\\r", 2); luaL_addchar(b, '\\');
break; c1 = uchar(*s); c3 = c1 % 10; c1 /= 10; c2 = c1 % 10; c1 /= 10;
case '\0': if (c1 + lj_char_isdigit(uchar(s[1]))) luaL_addchar(b, '0' + c1);
luaL_addlstring(b, "\\000", 4); if (c2 + (c1 + lj_char_isdigit(uchar(s[1])))) luaL_addchar(b, '0' + c2);
break; luaL_addchar(b, '0' + c3);
default: } else {
luaL_addchar(b, *s); luaL_addchar(b, *s);
break;
} }
s++; s++;
} }