Commit 61f33020 authored by Rob Pike's avatar Rob Pike

printf->Printf etc.

the raw fmt routines will be another, smaller but subtler pass.

R=rsc
DELTA=157  (0 added, 0 deleted, 157 changed)
OCL=22851
CL=22851
parent c0f6144f
......@@ -8,13 +8,13 @@ import "fmt"
func main() {
var u64 uint64 = 1<<64-1;
fmt.printf("%d %d\n", u64, int64(u64));
fmt.Printf("%d %d\n", u64, int64(u64));
// harder stuff
type T struct { a int; b string };
t := T{77, "Sunset Strip"};
a := []int{1, 2, 3, 4};
fmt.printf("%v %v %v\n", u64, t, a);
fmt.print(u64, " ", t, " ", a, "\n");
fmt.println(u64, t, a);
fmt.Printf("%v %v %v\n", u64, t, a);
fmt.Print(u64, " ", t, " ", a, "\n");
fmt.Println(u64, t, a);
}
......@@ -9,10 +9,10 @@ import "fmt"
type T struct { a int; b string }
func (t *T) String() string {
return fmt.sprint(t.a) + " " + t.b
return fmt.Sprint(t.a) + " " + t.b
}
func main() {
t := &T{77, "Sunset Strip"};
fmt.println(t)
fmt.Println(t)
}
......@@ -7,5 +7,5 @@ package main
import "fmt"
func main() {
fmt.printf("hello, %s\n", "world");
fmt.Printf("hello, %s\n", "world");
}
......@@ -662,7 +662,7 @@ func FmtBase(c int) uint {
func (x Natural) Format(h Fmt.Formatter, c int) {
Fmt.fprintf(h, "%s", x.ToString(FmtBase(c)));
Fmt.Fprintf(h, "%s", x.ToString(FmtBase(c)));
}
......@@ -1096,7 +1096,7 @@ func (x *Integer) String() string {
func (x *Integer) Format(h Fmt.Formatter, c int) {
Fmt.fprintf(h, "%s", x.ToString(FmtBase(c)));
Fmt.Fprintf(h, "%s", x.ToString(FmtBase(c)));
}
......@@ -1226,7 +1226,7 @@ func (x *Rational) String() string {
func (x *Rational) Format(h Fmt.Formatter, c int) {
Fmt.fprintf(h, "%s", x.ToString(FmtBase(c)));
Fmt.Fprintf(h, "%s", x.ToString(FmtBase(c)));
}
......
......@@ -117,7 +117,7 @@ export func TestNatConv(t *testing.T) {
test_msg = "NatConvD";
x := bignum.Nat(100);
y, b := bignum.NatFromString(fmt.sprintf("%b", &x), 2, nil);
y, b := bignum.NatFromString(fmt.Sprintf("%b", &x), 2, nil);
NAT_EQ(100, y, x);
}
......
......@@ -309,7 +309,7 @@ export func TestBufWrite(t *testing.T) {
write := writers[k].fn();
buf, e := NewBufWriteSize(write, bs);
context := fmt.sprintf("write=%s nwrite=%d bufsize=%d", writers[k].name, nwrite, bs);
context := fmt.Sprintf("write=%s nwrite=%d bufsize=%d", writers[k].name, nwrite, bs);
if e != nil {
t.Errorf("%s: NewBufWriteSize %d: %v", context, bs, e);
continue;
......
......@@ -119,7 +119,7 @@ func (b *BoolValue) Set(val bool) {
}
func (b *BoolValue) Str() string {
return fmt.sprintf("%v", *b.p)
return fmt.Sprintf("%v", *b.p)
}
// -- Int Value
......@@ -137,7 +137,7 @@ func (i *IntValue) Set(val int) {
}
func (i *IntValue) Str() string {
return fmt.sprintf("%v", *i.p)
return fmt.Sprintf("%v", *i.p)
}
// -- Int64 Value
......@@ -155,7 +155,7 @@ func (i *Int64Value) Set(val int64) {
}
func (i *Int64Value) Str() string {
return fmt.sprintf("%v", *i.p)
return fmt.Sprintf("%v", *i.p)
}
// -- Uint Value
......@@ -173,7 +173,7 @@ func (i *UintValue) Set(val uint) {
}
func (i *UintValue) Str() string {
return fmt.sprintf("%v", *i.p)
return fmt.Sprintf("%v", *i.p)
}
// -- Uint64 Value
......@@ -191,7 +191,7 @@ func (i *Uint64Value) Set(val uint64) {
}
func (i *Uint64Value) Str() string {
return fmt.sprintf("%v", *i.p)
return fmt.Sprintf("%v", *i.p)
}
// -- String Value
......@@ -209,7 +209,7 @@ func (s *StringValue) Set(val string) {
}
func (s *StringValue) Str() string {
return fmt.sprintf("%#q", *s.p)
return fmt.Sprintf("%#q", *s.p)
}
// -- Value interface
......
......@@ -14,9 +14,9 @@ import (
export func TestFmtInterface(t *testing.T) {
var i1 interface{};
i1 = "abc";
s := fmt.sprintf("%s", i1);
s := fmt.Sprintf("%s", i1);
if s != "abc" {
t.Errorf(`fmt.sprintf("%%s", empty("abc")) = %q want %q`, s, "abc");
t.Errorf(`fmt.Sprintf("%%s", empty("abc")) = %q want %q`, s, "abc");
}
}
......@@ -153,14 +153,14 @@ var fmttests = []FmtTest{
export func TestSprintf(t *testing.T) {
for i := 0; i < len(fmttests); i++ {
tt := fmttests[i];
s := fmt.sprintf(tt.fmt, tt.val);
s := fmt.Sprintf(tt.fmt, tt.val);
if s != tt.out {
if ss, ok := tt.val.(string); ok {
// Don't requote the already-quoted strings.
// It's too confusing to read the errors.
t.Errorf("fmt.sprintf(%q, %q) = %s want %s", tt.fmt, tt.val, s, tt.out);
t.Errorf("fmt.Sprintf(%q, %q) = %s want %s", tt.fmt, tt.val, s, tt.out);
} else {
t.Errorf("fmt.sprintf(%q, %v) = %q want %q", tt.fmt, tt.val, s, tt.out);
t.Errorf("fmt.Sprintf(%q, %v) = %q want %q", tt.fmt, tt.val, s, tt.out);
}
}
}
......@@ -175,10 +175,10 @@ func (*FlagPrinter) Format(f fmt.Formatter, c int) {
}
}
if w, ok := f.Width(); ok {
s += fmt.sprintf("%d", w);
s += fmt.Sprintf("%d", w);
}
if p, ok := f.Precision(); ok {
s += fmt.sprintf(".%d", p);
s += fmt.Sprintf(".%d", p);
}
s += string(c);
io.WriteString(f, "["+s+"]");
......@@ -208,9 +208,9 @@ export func TestFlagParser(t *testing.T) {
var flagprinter FlagPrinter;
for i := 0; i < len(flagtests); i++ {
tt := flagtests[i];
s := fmt.sprintf(tt.in, &flagprinter);
s := fmt.Sprintf(tt.in, &flagprinter);
if s != tt.out {
t.Errorf("sprintf(%q, &flagprinter) => %q, want %q", tt.in, s, tt.out);
t.Errorf("Sprintf(%q, &flagprinter) => %q, want %q", tt.in, s, tt.out);
}
}
}
......@@ -234,9 +234,9 @@ export func TestStructPrinter(t *testing.T) {
};
for i := 0; i < len(tests); i++ {
tt := tests[i];
out := fmt.sprintf(tt.fmt, s);
out := fmt.Sprintf(tt.fmt, s);
if out != tt.out {
t.Errorf("sprintf(%q, &s) = %q, want %q", tt.fmt, out, tt.out);
t.Errorf("Sprintf(%q, &s) = %q, want %q", tt.fmt, out, tt.out);
}
}
}
......@@ -244,13 +244,13 @@ export func TestStructPrinter(t *testing.T) {
export func TestArrayPrinter(t *testing.T) {
a := []int{1, 2, 3, 4, 5};
want := "[1 2 3 4 5]";
out := fmt.sprintf("%v", a);
out := fmt.Sprintf("%v", a);
if out != want {
t.Errorf("sprintf(%%v, array) = %q, want %q", out, want);
t.Errorf("Sprintf(%%v, array) = %q, want %q", out, want);
}
want = "&" + want;
out = fmt.sprintf("%v", &a);
out = fmt.Sprintf("%v", &a);
if out != want {
t.Errorf("sprintf(%%v, &array) = %q, want %q", out, want);
t.Errorf("Sprintf(%%v, &array) = %q, want %q", out, want);
}
}
......@@ -128,7 +128,7 @@ func (p *P) doprint(v reflect.StructValue, addspace, addnewline bool);
// These routines end in 'f' and take a format string.
export func fprintf(w io.Write, format string, a ...) (n int, error *os.Error) {
export func Fprintf(w io.Write, format string, a ...) (n int, error *os.Error) {
v := reflect.NewValue(a).(reflect.PtrValue).Sub().(reflect.StructValue);
p := Printer();
p.doprintf(format, v);
......@@ -136,12 +136,12 @@ export func fprintf(w io.Write, format string, a ...) (n int, error *os.Error) {
return n, error;
}
export func printf(format string, v ...) (n int, errno *os.Error) {
n, errno = fprintf(os.Stdout, format, v);
export func Printf(format string, v ...) (n int, errno *os.Error) {
n, errno = Fprintf(os.Stdout, format, v);
return n, errno;
}
export func sprintf(format string, a ...) string {
export func Sprintf(format string, a ...) string {
v := reflect.NewValue(a).(reflect.PtrValue).Sub().(reflect.StructValue);
p := Printer();
p.doprintf(format, v);
......@@ -152,7 +152,7 @@ export func sprintf(format string, a ...) string {
// These routines do not take a format string and add spaces only
// when the operand on neither side is a string.
export func fprint(w io.Write, a ...) (n int, error *os.Error) {
export func Fprint(w io.Write, a ...) (n int, error *os.Error) {
v := reflect.NewValue(a).(reflect.PtrValue).Sub().(reflect.StructValue);
p := Printer();
p.doprint(v, false, false);
......@@ -160,12 +160,12 @@ export func fprint(w io.Write, a ...) (n int, error *os.Error) {
return n, error;
}
export func print(v ...) (n int, errno *os.Error) {
n, errno = fprint(os.Stdout, v);
export func Print(v ...) (n int, errno *os.Error) {
n, errno = Fprint(os.Stdout, v);
return n, errno;
}
export func sprint(a ...) string {
export func Sprint(a ...) string {
v := reflect.NewValue(a).(reflect.PtrValue).Sub().(reflect.StructValue);
p := Printer();
p.doprint(v, false, false);
......@@ -177,7 +177,7 @@ export func sprint(a ...) string {
// always add spaces between operands, and add a newline
// after the last operand.
export func fprintln(w io.Write, a ...) (n int, error *os.Error) {
export func Fprintln(w io.Write, a ...) (n int, error *os.Error) {
v := reflect.NewValue(a).(reflect.PtrValue).Sub().(reflect.StructValue);
p := Printer();
p.doprint(v, true, true);
......@@ -185,12 +185,12 @@ export func fprintln(w io.Write, a ...) (n int, error *os.Error) {
return n, error;
}
export func println(v ...) (n int, errno *os.Error) {
n, errno = fprintln(os.Stdout, v);
export func Println(v ...) (n int, errno *os.Error) {
n, errno = Fprintln(os.Stdout, v);
return n, errno;
}
export func sprintln(a ...) string {
export func Sprintln(a ...) string {
v := reflect.NewValue(a).(reflect.PtrValue).Sub().(reflect.StructValue);
p := Printer();
p.doprint(v, true, true);
......
......@@ -63,9 +63,9 @@ func (j *Number) Kind() int { return NumberKind }
func (j *Number) Number() float64 { return j.f }
func (j *Number) String() string {
if math.Floor(j.f) == j.f {
return fmt.sprintf("%.0f", j.f);
return fmt.Sprintf("%.0f", j.f);
}
return fmt.sprintf("%g", j.f);
return fmt.Sprintf("%g", j.f);
}
type Array struct { a *array.Array; Null }
......
......@@ -115,7 +115,7 @@ Cname:
n := len(addrs);
a := rr.(*DNS_RR_A).a;
addrs = addrs[0:n+1];
addrs[n] = fmt.sprintf("%d.%d.%d.%d", (a>>24), (a>>16)&0xFF, (a>>8)&0xFF, a&0xFF);
addrs[n] = fmt.Sprintf("%d.%d.%d.%d", (a>>24), (a>>16)&0xFF, (a>>8)&0xFF, a&0xFF);
case DNS_TypeCNAME:
// redirect to cname
name = rr.(*DNS_RR_CNAME).cname;
......
......@@ -323,7 +323,7 @@ func PackStructValue(val reflect.StructValue, msg []byte, off int) (off1 int, ok
name, typ, tag, xxx := val.Type().(reflect.StructType).Field(i);
switch fld.Kind() {
default:
fmt.fprintf(os.Stderr, "net: dns: unknown packing type %v", fld.Type());
fmt.Fprintf(os.Stderr, "net: dns: unknown packing type %v", fld.Type());
return len(msg), false;
case reflect.StructKind:
off, ok = PackStructValue(fld.(reflect.StructValue), msg, off);
......@@ -351,7 +351,7 @@ func PackStructValue(val reflect.StructValue, msg []byte, off int) (off1 int, ok
s := fld.(reflect.StringValue).Get();
switch tag {
default:
fmt.fprintf(os.Stderr, "net: dns: unknown string tag %v", tag);
fmt.Fprintf(os.Stderr, "net: dns: unknown string tag %v", tag);
return len(msg), false;
case "domain-name":
off, ok = PackDomainName(s, msg, off);
......@@ -389,7 +389,7 @@ func UnpackStructValue(val reflect.StructValue, msg []byte, off int) (off1 int,
fld := val.Field(i);
switch fld.Kind() {
default:
fmt.fprintf(os.Stderr, "net: dns: unknown packing type %v", fld.Type());
fmt.Fprintf(os.Stderr, "net: dns: unknown packing type %v", fld.Type());
return len(msg), false;
case reflect.StructKind:
off, ok = UnpackStructValue(fld.(reflect.StructValue), msg, off);
......@@ -411,7 +411,7 @@ func UnpackStructValue(val reflect.StructValue, msg []byte, off int) (off1 int,
var s string;
switch tag {
default:
fmt.fprintf(os.Stderr, "net: dns: unknown string tag %v", tag);
fmt.Fprintf(os.Stderr, "net: dns: unknown string tag %v", tag);
return len(msg), false;
case "domain-name":
s, off, ok = UnpackDomainName(msg, off);
......@@ -464,9 +464,9 @@ func PrintStructValue(val reflect.StructValue) string {
s += PrintStructValue(fld.(reflect.StructValue));
case kind == reflect.Uint32Kind && tag == "ipv4":
i := fld.(reflect.Uint32Value).Get();
s += fmt.sprintf("%d.%d.%d.%d", (i>>24)&0xFF, (i>>16)&0xFF, (i>>8)&0xFF, i&0xFF);
s += fmt.Sprintf("%d.%d.%d.%d", (i>>24)&0xFF, (i>>16)&0xFF, (i>>8)&0xFF, i&0xFF);
default:
s += fmt.sprint(fld.Interface())
s += fmt.Sprint(fld.Interface())
}
}
s += "}";
......
......@@ -213,7 +213,7 @@ export func TestBentleyMcIlroy(t *testing.T) {
}
}
desc := fmt.sprintf("n=%d m=%d dist=%s mode=%s", n, m, dists[dist], modes[mode]);
desc := fmt.Sprintf("n=%d m=%d dist=%s mode=%s", n, m, dists[dist], modes[mode]);
d := &TestingData{desc, t, mdata[0:n], n*Lg(n)*12/10, 0};
sort.Sort(d);
......
......@@ -131,14 +131,14 @@ export func TestFp(t *testing.T) {
t.Error("testfp.txt:", lineno, ": cannot atof64 ", a[2]);
continue;
}
s = fmt.sprintf(a[1], v);
s = fmt.Sprintf(a[1], v);
case "float32":
v1, ok := myatof32(a[2]);
if !ok {
t.Error("testfp.txt:", lineno, ": cannot atof32 ", a[2]);
continue;
}
s = fmt.sprintf(a[1], v1);
s = fmt.Sprintf(a[1], v1);
v = float64(v1);
}
if s != a[3] {
......
......@@ -38,11 +38,11 @@ func (t *T) FailNow() {
}
func (t *T) Log(args ...) {
t.errors += "\t" + Tabify(fmt.sprintln(args));
t.errors += "\t" + Tabify(fmt.Sprintln(args));
}
func (t *T) Logf(format string, args ...) {
t.errors += Tabify(fmt.sprintf("\t" + format, args));
t.errors += Tabify(fmt.Sprintf("\t" + format, args));
l := len(t.errors);
if l > 0 && t.errors[l-1] != '\n' {
t.errors += "\n"
......
......@@ -142,7 +142,7 @@ func (a *Matrix) String() string {
s := "";
for i := 0; i < a.n; i++ {
for j := 0; j < a.m; j++ {
s += Fmt.sprintf("\t%s", a.at(i, j));
s += Fmt.Sprintf("\t%s", a.at(i, j));
}
s += "\n";
}
......@@ -157,10 +157,10 @@ func main() {
I := NewUnit(n);
ab := a.Mul(b);
if !ab.Eql(I) {
Fmt.println("a =", a);
Fmt.println("b =", b);
Fmt.println("a*b =", ab);
Fmt.println("I =", I);
Fmt.Println("a =", a);
Fmt.Println("b =", b);
Fmt.Println("a*b =", ab);
Fmt.Println("I =", I);
panic("FAILED");
}
}
......@@ -19,7 +19,7 @@ var chatty = flag.Bool("v", false, "chatty");
func main() {
malloc.Free(malloc.Alloc(1));
if *chatty {
fmt.printf("%+v %v\n", *malloc.GetStats(), uint64(0));
fmt.Printf("%+v %v\n", *malloc.GetStats(), uint64(0));
}
}
......@@ -40,7 +40,7 @@ func OkAmount(size, n uint64) bool {
func AllocAndFree(size, count int) {
if *chatty {
fmt.printf("size=%d count=%d ...\n", size, count);
fmt.Printf("size=%d count=%d ...\n", size, count);
}
n1 := stats.alloc;
for i := 0; i < count; i++ {
......@@ -55,7 +55,7 @@ func AllocAndFree(size, count int) {
}
n2 := stats.alloc;
if *chatty {
fmt.printf("size=%d count=%d stats=%+v\n", size, count, *stats);
fmt.Printf("size=%d count=%d stats=%+v\n", size, count, *stats);
}
n3 := stats.alloc;
for j := 0; j < count; j++ {
......@@ -79,7 +79,7 @@ func AllocAndFree(size, count int) {
n4 := stats.alloc;
if *chatty {
fmt.printf("size=%d count=%d stats=%+v\n", size, count, *stats);
fmt.Printf("size=%d count=%d stats=%+v\n", size, count, *stats);
}
if n2-n1 != n3-n4 {
panicln("wrong alloc count: ", n2-n1, n3-n4);
......
This diff is collapsed.
......@@ -143,7 +143,7 @@ func Untabify(s string) string {
func (P *Printer) Printf(format string, s ...) {
n, err := fmt.fprintf(P.text, format, s);
n, err := fmt.Fprintf(P.text, format, s);
if err != nil {
panic("print error - exiting");
}
......
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