commit ad446a0eb06f570f826fd313f5ed225b04e3719a
parent 176cb39feb0421069e930f004412c25549724f1f
Author: Roberto Ierusalimschy <roberto@inf.puc-rio.br>
Date: Sun, 12 Jul 1998 13:13:24 -0300
"%q" can handle strings with '\0'.
Diffstat:
1 file changed, 15 insertions(+), 8 deletions(-)
diff --git a/lstrlib.c b/lstrlib.c
@@ -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
** 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('"');
- for (; *s; s++) {
- if (strchr("\"\\\n", *s))
- luaL_addchar('\\');
- luaL_addchar(*s);
+ while (l--) {
+ switch (*s) {
+ case '"': case '\\': case '\n':
+ luaL_addchar('\\');
+ luaL_addchar(*s);
+ break;
+ case '\0': addnchar("\\000", 4); break;
+ default: luaL_addchar(*s);
+ }
+ s++;
}
luaL_addchar('"');
}
@@ -490,7 +497,7 @@ static void str_format (void)
buff = luaL_openspace(1000); /* to store the formatted value */
switch (*strfrmt++) {
case 'q':
- luaI_addquoted(luaL_check_string(arg));
+ luaI_addquoted(arg);
continue;
case 's': {
char *s = luaL_check_string(arg);