Commit 3200b06b authored by Rob Pike's avatar Rob Pike

prepare for recursive printfs

R=rsc
DELTA=31  (9 added, 6 deleted, 16 changed)
OCL=18470
CL=18472
parent e2eccf3b
...@@ -18,13 +18,13 @@ import ( ...@@ -18,13 +18,13 @@ import (
const Runeself = 0x80 const Runeself = 0x80
const AllocSize = 32 const AllocSize = 32
export type P struct { type P struct {
n int; n int;
buf *[]byte; buf *[]byte;
fmt *Fmt; fmt *Fmt;
} }
export func Printer() *P { func Printer() *P {
p := new(P); p := new(P);
p.fmt = fmt.New(); p.fmt = fmt.New();
return p; return p;
...@@ -74,8 +74,11 @@ func (p *P) add(c int) { ...@@ -74,8 +74,11 @@ func (p *P) add(c int) {
} }
} }
func (p *P) reset() { // Implement Write so we can call fprintf on a P, for
p.n = 0; // recursive use in custom verbs.
func (p *P) Write(b *[]byte) (ret int, err *os.Error) {
p.addbytes(b, 0, len(b));
return len(b), nil;
} }
export type Writer interface { export type Writer interface {
...@@ -87,46 +90,46 @@ func (p *P) doprint(v reflect.StructValue, addspace, addnewline bool); ...@@ -87,46 +90,46 @@ func (p *P) doprint(v reflect.StructValue, addspace, addnewline bool);
// These routines end in 'f' and take a format string. // These routines end in 'f' and take a format string.
func (p *P) fprintf(w Writer, format string, a ...) (n int, error *os.Error) { export func fprintf(w Writer, format string, a ...) (n int, error *os.Error) {
v := reflect.NewValue(a).(reflect.PtrValue).Sub().(reflect.StructValue); v := reflect.NewValue(a).(reflect.PtrValue).Sub().(reflect.StructValue);
p := Printer();
p.doprintf(format, v); p.doprintf(format, v);
n, error = w.Write(p.buf[0:p.n]); n, error = w.Write(p.buf[0:p.n]);
p.reset();
return n, error; return n, error;
} }
func (p *P) printf(format string, v ...) (n int, errno *os.Error) { export func printf(format string, v ...) (n int, errno *os.Error) {
n, errno = p.fprintf(os.Stdout, format, v); n, errno = fprintf(os.Stdout, format, v);
return n, errno; return n, errno;
} }
func (p *P) sprintf(format string, v ...) string { export func sprintf(format string, v ...) string {
p := Printer();
p.doprintf(format, reflect.NewValue(v).(reflect.StructValue)); p.doprintf(format, reflect.NewValue(v).(reflect.StructValue));
s := string(p.buf)[0 : p.n]; s := string(p.buf)[0 : p.n];
p.reset();
return s; return s;
} }
// These routines do not take a format string and add spaces only // These routines do not take a format string and add spaces only
// when the operand on neither side is a string. // when the operand on neither side is a string.
func (p *P) fprint(w Writer, a ...) (n int, error *os.Error) { export func fprint(w Writer, a ...) (n int, error *os.Error) {
v := reflect.NewValue(a).(reflect.PtrValue).Sub().(reflect.StructValue); v := reflect.NewValue(a).(reflect.PtrValue).Sub().(reflect.StructValue);
p := Printer();
p.doprint(v, false, false); p.doprint(v, false, false);
n, error = w.Write(p.buf[0:p.n]); n, error = w.Write(p.buf[0:p.n]);
p.reset();
return n, error; return n, error;
} }
func (p *P) print(v ...) (n int, errno *os.Error) { export func print(v ...) (n int, errno *os.Error) {
n, errno = p.fprint(os.Stdout, v); n, errno = fprint(os.Stdout, v);
return n, errno; return n, errno;
} }
func (p *P) sprint(v ...) string { export func sprint(v ...) string {
p := Printer();
p.doprint(reflect.NewValue(v).(reflect.StructValue), false, false); p.doprint(reflect.NewValue(v).(reflect.StructValue), false, false);
s := string(p.buf)[0 : p.n]; s := string(p.buf)[0 : p.n];
p.reset();
return s; return s;
} }
...@@ -134,23 +137,23 @@ func (p *P) sprint(v ...) string { ...@@ -134,23 +137,23 @@ func (p *P) sprint(v ...) string {
// always add spaces between operands, and add a newline // always add spaces between operands, and add a newline
// after the last operand. // after the last operand.
func (p *P) fprintln(w Writer, a ...) (n int, error *os.Error) { export func fprintln(w Writer, a ...) (n int, error *os.Error) {
v := reflect.NewValue(a).(reflect.PtrValue).Sub().(reflect.StructValue); v := reflect.NewValue(a).(reflect.PtrValue).Sub().(reflect.StructValue);
p := Printer();
p.doprint(v, true, true); p.doprint(v, true, true);
n, error = w.Write(p.buf[0:p.n]); n, error = w.Write(p.buf[0:p.n]);
p.reset();
return n, error; return n, error;
} }
func (p *P) println(v ...) (n int, errno *os.Error) { export func println(v ...) (n int, errno *os.Error) {
n, errno = p.fprintln(os.Stdout, v); n, errno = fprintln(os.Stdout, v);
return n, errno; return n, errno;
} }
func (p *P) sprintln(v ...) string { export func sprintln(v ...) string {
p := Printer();
p.doprint(reflect.NewValue(v).(reflect.StructValue), true, true); p.doprint(reflect.NewValue(v).(reflect.StructValue), true, true);
s := string(p.buf)[0 : p.n]; s := string(p.buf)[0 : p.n];
p.reset();
return s; return s;
} }
......
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