Commit 91212bd1 authored by Rob Pike's avatar Rob Pike

If a value implements String(), use that in fmt.print (not fmt.printf)

R=rsc
DELTA=13  (9 added, 0 deleted, 4 changed)
OCL=18682
CL=18684
parent f15dfa78
...@@ -22,14 +22,18 @@ export type Writer interface { ...@@ -22,14 +22,18 @@ export type Writer interface {
// Representation of printer state passed to custom formatters. // Representation of printer state passed to custom formatters.
// Provides access to the Writer interface plus information about // Provides access to the Writer interface plus information about
// the active formatting verb. // the active formatting verb.
export type FormatHelper interface { export type Formatter interface {
Write(b *[]byte) (ret int, err *os.Error); Write(b *[]byte) (ret int, err *os.Error);
Width() (wid int, ok bool); Width() (wid int, ok bool);
Precision() (prec int, ok bool); Precision() (prec int, ok bool);
} }
export type Formatter interface { export type Format interface {
Format(f FormatHelper, c int); Format(f Formatter, c int);
}
export type String interface {
String() string
} }
const Runeself = 0x80 const Runeself = 0x80
...@@ -303,7 +307,7 @@ func (p *P) doprintf(format string, v reflect.StructValue) { ...@@ -303,7 +307,7 @@ func (p *P) doprintf(format string, v reflect.StructValue) {
} }
field := v.Field(fieldnum); field := v.Field(fieldnum);
fieldnum++; fieldnum++;
if formatter, ok := field.Interface().(Formatter); ok { if formatter, ok := field.Interface().(Format); ok {
formatter.Format(p, c); formatter.Format(p, c);
continue; continue;
} }
...@@ -439,6 +443,11 @@ func (p *P) doprint(v reflect.StructValue, addspace, addnewline bool) { ...@@ -439,6 +443,11 @@ func (p *P) doprint(v reflect.StructValue, addspace, addnewline bool) {
p.add(' ') p.add(' ')
} }
} }
if stringer, ok := field.Interface().(String); ok {
p.addstr(stringer.String());
prev_string = false; // this value is not a string
continue;
}
switch field.Kind() { switch field.Kind() {
case reflect.BoolKind: case reflect.BoolKind:
s = p.fmt.boolean(field.(reflect.BoolValue).Get()).str(); s = p.fmt.boolean(field.(reflect.BoolValue).Get()).str();
......
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