1
0
mirror of https://github.com/golang/go synced 2024-11-22 21:10:03 -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
Zconv(Fmt *fp)
{
char *s, *se;
char *p;
char buf[500];
int c;
Rune r;
String *sp;
char *s, *se;
sp = va_arg(fp->args, String*);
if(sp == nil) {
snprint(buf, sizeof(buf), "<nil>");
goto out;
}
if(sp == nil)
return fmtstrcpy(fp, "<nil>");
s = sp->s;
se = s + sp->len;
p = buf;
loop:
c = *s++;
if(s > se)
c = 0;
switch(c) {
default:
*p++ = c;
break;
case 0:
*p = 0;
goto out;
case '\t':
*p++ = '\\';
*p++ = 't';
break;
case '\n':
*p++ = '\\';
*p++ = 'n';
break;
case '\"':
case '\\':
*p++ = '\\';
*p++ = c;
break;
while(s < se) {
s += chartorune(&r, s);
switch(r) {
default:
fmtrune(fp, r);
break;
case '\0':
fmtstrcpy(fp, "\\x00");
break;
case '\t':
fmtstrcpy(fp, "\\t");
break;
case '\n':
fmtstrcpy(fp, "\\n");
break;
case '\"':
case '\\':
fmtrune(fp, '\\');
fmtrune(fp, r);
break;
}
}
goto loop;
out:
return fmtstrcpy(fp, buf);
return 0;
}
static char*
wnames[] =
{