1
0
mirror of https://github.com/golang/go synced 2024-11-23 00:40:08 -07:00

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

R=r
DELTA=44  (2 added, 15 deleted, 27 changed)
OCL=18270
CL=18273
This commit is contained in:
Russ Cox 2008-10-31 14:57:57 -07:00
parent 2eb17d7894
commit 8a1ad75644

View File

@ -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: default:
c = *s++; fmtrune(fp, r);
if(s > se) break;
c = 0; case '\0':
switch(c) { fmtstrcpy(fp, "\\x00");
default: break;
*p++ = c; case '\t':
break; fmtstrcpy(fp, "\\t");
case 0: break;
*p = 0; case '\n':
goto out; fmtstrcpy(fp, "\\n");
case '\t': break;
*p++ = '\\'; case '\"':
*p++ = 't'; case '\\':
break; fmtrune(fp, '\\');
case '\n': fmtrune(fp, r);
*p++ = '\\'; break;
*p++ = 'n'; }
break;
case '\"':
case '\\':
*p++ = '\\';
*p++ = c;
break;
} }
goto loop; return 0;
out:
return fmtstrcpy(fp, buf);
} }
static char* static char*
wnames[] = wnames[] =
{ {