Commit e5c114ef authored by Rob Pike's avatar Rob Pike

convert print to ToString.

file name change in next round.

R=rsc
DELTA=71  (18 added, 19 deleted, 34 changed)
OCL=17291
CL=17293
parent a09b7fdd
...@@ -5,97 +5,96 @@ ...@@ -5,97 +5,96 @@
package reflect package reflect
import ( import (
"reflect" "reflect";
"strings";
) )
// Implemented as a function rather than a method to keep the export func ToString(typ Type) string
// Type interface small. TODO: should this return a string?
export func Print(typ Type) { func FieldsToString(t Type) string {
s := t.(StructType);
var str string;
for i := 0; i < s.Len(); i++ {
str1, t := s.Field(i);
str1 += " " + ToString(t);
if i < s.Len() - 1 {
str1 += "; ";
}
str += str1;
}
return str;
}
func ToString(typ Type) string {
var str string;
switch(typ.Kind()) { switch(typ.Kind()) {
case Int8Kind: case Int8Kind:
print("int8"); return "int8";
case Int16Kind: case Int16Kind:
print("int16"); return "int16";
case Int32Kind: case Int32Kind:
print("int32"); return "int32";
case Int64Kind: case Int64Kind:
print("int64"); return "int64";
case Uint8Kind: case Uint8Kind:
print("uint8"); return "uint8";
case Uint16Kind: case Uint16Kind:
print("uint16"); return "uint16";
case Uint32Kind: case Uint32Kind:
print("uint32"); return "uint32";
case Uint64Kind: case Uint64Kind:
print("uint64"); return "uint64";
case Float32Kind: case Float32Kind:
print("float32"); return "float32";
case Float64Kind: case Float64Kind:
print("float64"); return "float64";
case Float80Kind: case Float80Kind:
print("float80"); return "float80";
case StringKind: case StringKind:
print("string"); return "string";
case PtrKind: case PtrKind:
p := typ.(PtrType); p := typ.(PtrType);
print("*"); return "*" + ToString(p.Sub());
Print(p.Sub());
case ArrayKind: case ArrayKind:
a := typ.(ArrayType); a := typ.(ArrayType);
if a.Len() >= 0 { if a.Len() < 0 {
print("[", a.Len(), "]") str = "[]"
} else { } else {
print("[]") str = "[" + strings.itoa(a.Len()) + "]"
} }
Print(a.Elem()); return str + ToString(a.Elem());
case MapKind: case MapKind:
m := typ.(MapType); m := typ.(MapType);
print("map["); str = "map[" + ToString(m.Key()) + "]";
Print(m.Key()); return str + ToString(m.Elem());
print("]");
Print(m.Elem());
case ChanKind: case ChanKind:
c := typ.(ChanType); c := typ.(ChanType);
switch c.Dir() { switch c.Dir() {
case RecvDir: case RecvDir:
print("<-chan"); str = "<-chan";
case SendDir: case SendDir:
print("chan<-"); str = "chan<-";
case BothDir: case BothDir:
print("chan"); str = "chan";
default: default:
panicln("reflect.Print: unknown chan direction"); panicln("reflect.ToString: unknown chan direction");
} }
Print(c.Elem()); return str + ToString(c.Elem());
case StructKind: case StructKind:
s := typ.(StructType); return "struct{" + FieldsToString(typ) + "}";
print("struct{");
for i := 0; i < s.Len(); i++ {
n, t := s.Field(i);
print(n, " ");
Print(t);
if i < s.Len() - 1 {
print("; ");
}
}
print("}");
case FuncKind: case FuncKind:
f := typ.(FuncType); f := typ.(FuncType);
print("func "); str = "func";
if f.Receiver() != nil { if f.Receiver() != nil {
print("("); str += "(" + FieldsToString(f.Receiver()) + ")";
Print(f.Receiver());
print(")");
} }
print("("); str += "(" + FieldsToString(f.In()) + ")";
Print(f.In());
print(")");
if f.Out() != nil { if f.Out() != nil {
print("("); str += "(" + FieldsToString(f.Out()) + ")";
Print(f.Out());
print(")");
} }
return str;
default: default:
panicln("can't print type ", typ.Kind()); panicln("reflect.ToString: can't print type ", typ.Kind());
} }
return "reflect.ToString: can't happen";
} }
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