"%q" can handle strings with '\0'.

This commit is contained in:
Roberto Ierusalimschy 1998-07-12 13:13:45 -03:00
parent 176cb39feb
commit ad446a0eb0
1 changed files with 15 additions and 8 deletions

View File

@ -1,5 +1,5 @@
/* /*
** $Id: lstrlib.c,v 1.17 1998/06/29 18:24:06 roberto Exp roberto $ ** $Id: lstrlib.c,v 1.18 1998/07/01 14:21:57 roberto Exp roberto $
** Standard library for strings and pattern-matching ** Standard library for strings and pattern-matching
** See Copyright Notice in lua.h ** See Copyright Notice in lua.h
*/ */
@ -446,13 +446,20 @@ static void str_gsub (void)
} }
static void luaI_addquoted (char *s) static void luaI_addquoted (int arg) {
{ long l;
char *s = luaL_check_lstr(arg, &l);
luaL_addchar('"'); luaL_addchar('"');
for (; *s; s++) { while (l--) {
if (strchr("\"\\\n", *s)) switch (*s) {
luaL_addchar('\\'); case '"': case '\\': case '\n':
luaL_addchar(*s); luaL_addchar('\\');
luaL_addchar(*s);
break;
case '\0': addnchar("\\000", 4); break;
default: luaL_addchar(*s);
}
s++;
} }
luaL_addchar('"'); luaL_addchar('"');
} }
@ -490,7 +497,7 @@ static void str_format (void)
buff = luaL_openspace(1000); /* to store the formatted value */ buff = luaL_openspace(1000); /* to store the formatted value */
switch (*strfrmt++) { switch (*strfrmt++) {
case 'q': case 'q':
luaI_addquoted(luaL_check_string(arg)); luaI_addquoted(arg);
continue; continue;
case 's': { case 's': {
char *s = luaL_check_string(arg); char *s = luaL_check_string(arg);