Commit 8a1ad756 authored by Russ Cox's avatar Russ Cox

make %Z handle embedded NULs; remove fixed-size buffer

R=r
DELTA=44  (2 added, 15 deleted, 27 changed)
OCL=18270
CL=18273
parent 2eb17d78
...@@ -1325,54 +1325,41 @@ treecopy(Node *n) ...@@ -1325,54 +1325,41 @@ treecopy(Node *n)
int int
Zconv(Fmt *fp) Zconv(Fmt *fp)
{ {
char *s, *se; Rune r;
char *p;
char buf[500];
int c;
String *sp; String *sp;
char *s, *se;
sp = va_arg(fp->args, String*); sp = va_arg(fp->args, String*);
if(sp == nil) { if(sp == nil)
snprint(buf, sizeof(buf), "<nil>"); return fmtstrcpy(fp, "<nil>");
goto out;
}
s = sp->s; s = sp->s;
se = s + sp->len; se = s + sp->len;
while(s < se) {
p = buf; s += chartorune(&r, s);
switch(r) {
loop:
c = *s++;
if(s > se)
c = 0;
switch(c) {
default: default:
*p++ = c; fmtrune(fp, r);
break;
case '\0':
fmtstrcpy(fp, "\\x00");
break; break;
case 0:
*p = 0;
goto out;
case '\t': case '\t':
*p++ = '\\'; fmtstrcpy(fp, "\\t");
*p++ = 't';
break; break;
case '\n': case '\n':
*p++ = '\\'; fmtstrcpy(fp, "\\n");
*p++ = 'n';
break; break;
case '\"': case '\"':
case '\\': case '\\':
*p++ = '\\'; fmtrune(fp, '\\');
*p++ = c; fmtrune(fp, r);
break; break;
} }
goto loop; }
return 0;
out:
return fmtstrcpy(fp, buf);
} }
static char* static char*
wnames[] = wnames[] =
{ {
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment