Commit 497bb9c0 authored by Rob Pike's avatar Rob Pike

casify fmt and its dependents.

R=rsc
DELTA=224  (0 added, 2 deleted, 222 changed)
OCL=22875
CL=22875
parent 8559e3ad
...@@ -12,24 +12,22 @@ import ( ...@@ -12,24 +12,22 @@ import (
Raw formatter. See print.go for a more palatable interface. Raw formatter. See print.go for a more palatable interface.
f := fmt.New(); f := fmt.New();
print f.d(1234).s("\n").str(); // create string, print it print f.Fmt_d(1234).Fmt_s("\n").Str(); // create string, print it
f.d(-1234).s("\n").put(); // print string f.Fmt_d(-1234).Fmt_s("\n").put(); // print string
f.ud(1<<63).putnl(); // print string with automatic newline f.Fmt_ud(1<<63).Putnl(); // print string with automatic newline
*/ */
// export Fmt, New; const nByte = 64;
const nPows10 = 160;
const NByte = 64;
const NPows10 = 160;
var ldigits string = "0123456789abcdef" // var not const because we take its address var ldigits string = "0123456789abcdef" // var not const because we take its address
var udigits string = "0123456789ABCDEF" var udigits string = "0123456789ABCDEF"
var pows10 [NPows10] float64; var pows10 [nPows10] float64;
func init() { func init() {
pows10[0] = 1.0e0; pows10[0] = 1.0e0;
pows10[1] = 1.0e1; pows10[1] = 1.0e1;
for i:=2; i<NPows10; i++ { for i:=2; i<nPows10; i++ {
m := i/2; m := i/2;
pows10[i] = pows10[m] * pows10[i-m]; pows10[i] = pows10[m] * pows10[i-m];
} }
...@@ -76,7 +74,7 @@ export func New() *Fmt { ...@@ -76,7 +74,7 @@ export func New() *Fmt {
return f; return f;
} }
func (f *Fmt) str() string { func (f *Fmt) Str() string {
s := f.buf; s := f.buf;
f.clearbuf(); f.clearbuf();
f.clearflags(); f.clearflags();
...@@ -84,19 +82,19 @@ func (f *Fmt) str() string { ...@@ -84,19 +82,19 @@ func (f *Fmt) str() string {
return s; return s;
} }
func (f *Fmt) put() { func (f *Fmt) Put() {
print(f.buf); print(f.buf);
f.clearbuf(); f.clearbuf();
f.clearflags(); f.clearflags();
} }
func (f *Fmt) putnl() { func (f *Fmt) Putnl() {
print(f.buf, "\n"); print(f.buf, "\n");
f.clearbuf(); f.clearbuf();
f.clearflags(); f.clearflags();
} }
func (f *Fmt) wp(w, p int) *Fmt { func (f *Fmt) Wp(w, p int) *Fmt {
f.wid_present = true; f.wid_present = true;
f.wid = w; f.wid = w;
f.prec_present = true; f.prec_present = true;
...@@ -104,13 +102,13 @@ func (f *Fmt) wp(w, p int) *Fmt { ...@@ -104,13 +102,13 @@ func (f *Fmt) wp(w, p int) *Fmt {
return f; return f;
} }
func (f *Fmt) p(p int) *Fmt { func (f *Fmt) P(p int) *Fmt {
f.prec_present = true; f.prec_present = true;
f.prec = p; f.prec = p;
return f; return f;
} }
func (f *Fmt) w(x int) *Fmt { func (f *Fmt) W(x int) *Fmt {
f.wid_present = true; f.wid_present = true;
f.wid = x; f.wid = x;
return f; return f;
...@@ -132,8 +130,8 @@ func (f *Fmt) pad(s string) { ...@@ -132,8 +130,8 @@ func (f *Fmt) pad(s string) {
padchar = '0'; padchar = '0';
} }
if w > 0 { if w > 0 {
if w > NByte { if w > nByte {
w = NByte; w = nByte;
} }
buf := make([]byte, w); buf := make([]byte, w);
for i := 0; i < w; i++ { for i := 0; i < w; i++ {
...@@ -154,7 +152,7 @@ func (f *Fmt) pad(s string) { ...@@ -154,7 +152,7 @@ func (f *Fmt) pad(s string) {
// never mind.) val is known to be unsigned. we could make things maybe // never mind.) val is known to be unsigned. we could make things maybe
// marginally faster by splitting the 32-bit case out into a separate function // marginally faster by splitting the 32-bit case out into a separate function
// but it's not worth the duplication, so val has 64 bits. // but it's not worth the duplication, so val has 64 bits.
func putint(buf *[NByte]byte, i int, base, val uint64, digits *string) int { func putint(buf *[nByte]byte, i int, base, val uint64, digits *string) int {
for val >= base { for val >= base {
buf[i] = digits[val%base]; buf[i] = digits[val%base];
i--; i--;
...@@ -165,7 +163,7 @@ func putint(buf *[NByte]byte, i int, base, val uint64, digits *string) int { ...@@ -165,7 +163,7 @@ func putint(buf *[NByte]byte, i int, base, val uint64, digits *string) int {
} }
// boolean // boolean
func (f *Fmt) boolean(a bool) *Fmt { func (f *Fmt) Fmt_boolean(a bool) *Fmt {
if a { if a {
f.pad("true"); f.pad("true");
} else { } else {
...@@ -177,7 +175,7 @@ func (f *Fmt) boolean(a bool) *Fmt { ...@@ -177,7 +175,7 @@ func (f *Fmt) boolean(a bool) *Fmt {
// integer; interprets prec but not wid. // integer; interprets prec but not wid.
func (f *Fmt) integer(a int64, base uint, is_signed bool, digits *string) string { func (f *Fmt) integer(a int64, base uint, is_signed bool, digits *string) string {
var buf [NByte]byte; var buf [nByte]byte;
negative := is_signed && a < 0; negative := is_signed && a < 0;
if negative { if negative {
a = -a; a = -a;
...@@ -196,8 +194,8 @@ func (f *Fmt) integer(a int64, base uint, is_signed bool, digits *string) string ...@@ -196,8 +194,8 @@ func (f *Fmt) integer(a int64, base uint, is_signed bool, digits *string) string
} }
} }
i := putint(&buf, NByte-1, uint64(base), uint64(a), digits); i := putint(&buf, nByte-1, uint64(base), uint64(a), digits);
for i > 0 && prec > (NByte-1-i) { for i > 0 && prec > (nByte-1-i) {
buf[i] = '0'; buf[i] = '0';
i--; i--;
} }
...@@ -212,156 +210,156 @@ func (f *Fmt) integer(a int64, base uint, is_signed bool, digits *string) string ...@@ -212,156 +210,156 @@ func (f *Fmt) integer(a int64, base uint, is_signed bool, digits *string) string
buf[i] = ' '; buf[i] = ' ';
i--; i--;
} }
return string(buf)[i+1:NByte]; return string(buf)[i+1:nByte];
} }
// decimal // decimal
func (f *Fmt) d64(a int64) *Fmt { func (f *Fmt) Fmt_d64(a int64) *Fmt {
f.pad(f.integer(a, 10, true, &ldigits)); f.pad(f.integer(a, 10, true, &ldigits));
f.clearflags(); f.clearflags();
return f; return f;
} }
func (f *Fmt) d32(a int32) *Fmt { func (f *Fmt) Fmt_d32(a int32) *Fmt {
return f.d64(int64(a)); return f.Fmt_d64(int64(a));
} }
func (f *Fmt) d(a int) *Fmt { func (f *Fmt) Fmt_d(a int) *Fmt {
return f.d64(int64(a)); return f.Fmt_d64(int64(a));
} }
// unsigned decimal // unsigned Fmt_decimal
func (f *Fmt) ud64(a uint64) *Fmt { func (f *Fmt) Fmt_ud64(a uint64) *Fmt {
f.pad(f.integer(int64(a), 10, false, &ldigits)); f.pad(f.integer(int64(a), 10, false, &ldigits));
f.clearflags(); f.clearflags();
return f; return f;
} }
func (f *Fmt) ud32(a uint32) *Fmt { func (f *Fmt) Fmt_ud32(a uint32) *Fmt {
return f.ud64(uint64(a)); return f.Fmt_ud64(uint64(a));
} }
func (f *Fmt) ud(a uint) *Fmt { func (f *Fmt) Fmt_ud(a uint) *Fmt {
return f.ud64(uint64(a)); return f.Fmt_ud64(uint64(a));
} }
// hexdecimal // hexdecimal
func (f *Fmt) x64(a int64) *Fmt { func (f *Fmt) Fmt_x64(a int64) *Fmt {
f.pad(f.integer(a, 16, true, &ldigits)); f.pad(f.integer(a, 16, true, &ldigits));
f.clearflags(); f.clearflags();
return f; return f;
} }
func (f *Fmt) x32(a int32) *Fmt { func (f *Fmt) Fmt_x32(a int32) *Fmt {
return f.x64(int64(a)); return f.Fmt_x64(int64(a));
} }
func (f *Fmt) x(a int) *Fmt { func (f *Fmt) Fmt_x(a int) *Fmt {
return f.x64(int64(a)); return f.Fmt_x64(int64(a));
} }
// unsigned hexdecimal // unsigned hexdecimal
func (f *Fmt) ux64(a uint64) *Fmt { func (f *Fmt) Fmt_ux64(a uint64) *Fmt {
f.pad(f.integer(int64(a), 16, false, &ldigits)); f.pad(f.integer(int64(a), 16, false, &ldigits));
f.clearflags(); f.clearflags();
return f; return f;
} }
func (f *Fmt) ux32(a uint32) *Fmt { func (f *Fmt) Fmt_ux32(a uint32) *Fmt {
return f.ux64(uint64(a)); return f.Fmt_ux64(uint64(a));
} }
func (f *Fmt) ux(a uint) *Fmt { func (f *Fmt) Fmt_ux(a uint) *Fmt {
return f.ux64(uint64(a)); return f.Fmt_ux64(uint64(a));
} }
// HEXADECIMAL // HEXADECIMAL
func (f *Fmt) X64(a int64) *Fmt { func (f *Fmt) Fmt_X64(a int64) *Fmt {
f.pad(f.integer(a, 16, true, &udigits)); f.pad(f.integer(a, 16, true, &udigits));
f.clearflags(); f.clearflags();
return f; return f;
} }
func (f *Fmt) X32(a int32) *Fmt { func (f *Fmt) Fmt_X32(a int32) *Fmt {
return f.X64(int64(a)); return f.Fmt_X64(int64(a));
} }
func (f *Fmt) X(a int) *Fmt { func (f *Fmt) Fmt_X(a int) *Fmt {
return f.X64(int64(a)); return f.Fmt_X64(int64(a));
} }
// unsigned HEXADECIMAL // unsigned HEXADECIMAL
func (f *Fmt) uX64(a uint64) *Fmt { func (f *Fmt) Fmt_uX64(a uint64) *Fmt {
f.pad(f.integer(int64(a), 16, false, &udigits)); f.pad(f.integer(int64(a), 16, false, &udigits));
f.clearflags(); f.clearflags();
return f; return f;
} }
func (f *Fmt) uX32(a uint32) *Fmt { func (f *Fmt) Fmt_uX32(a uint32) *Fmt {
return f.uX64(uint64(a)); return f.Fmt_uX64(uint64(a));
} }
func (f *Fmt) uX(a uint) *Fmt { func (f *Fmt) Fmt_uX(a uint) *Fmt {
return f.uX64(uint64(a)); return f.Fmt_uX64(uint64(a));
} }
// octal // octal
func (f *Fmt) o64(a int64) *Fmt { func (f *Fmt) Fmt_o64(a int64) *Fmt {
f.pad(f.integer(a, 8, true, &ldigits)); f.pad(f.integer(a, 8, true, &ldigits));
f.clearflags(); f.clearflags();
return f; return f;
} }
func (f *Fmt) o32(a int32) *Fmt { func (f *Fmt) Fmt_o32(a int32) *Fmt {
return f.o64(int64(a)); return f.Fmt_o64(int64(a));
} }
func (f *Fmt) o(a int) *Fmt { func (f *Fmt) Fmt_o(a int) *Fmt {
return f.o64(int64(a)); return f.Fmt_o64(int64(a));
} }
// unsigned octal // unsigned octal
func (f *Fmt) uo64(a uint64) *Fmt { func (f *Fmt) Fmt_uo64(a uint64) *Fmt {
f.pad(f.integer(int64(a), 8, false, &ldigits)); f.pad(f.integer(int64(a), 8, false, &ldigits));
f.clearflags(); f.clearflags();
return f; return f;
} }
func (f *Fmt) uo32(a uint32) *Fmt { func (f *Fmt) Fmt_uo32(a uint32) *Fmt {
return f.uo64(uint64(a)); return f.Fmt_uo64(uint64(a));
} }
func (f *Fmt) uo(a uint) *Fmt { func (f *Fmt) Fmt_uo(a uint) *Fmt {
return f.uo64(uint64(a)); return f.Fmt_uo64(uint64(a));
} }
// unsigned binary // unsigned binary
func (f *Fmt) b64(a uint64) *Fmt { func (f *Fmt) Fmt_b64(a uint64) *Fmt {
f.pad(f.integer(int64(a), 2, false, &ldigits)); f.pad(f.integer(int64(a), 2, false, &ldigits));
f.clearflags(); f.clearflags();
return f; return f;
} }
func (f *Fmt) b32(a uint32) *Fmt { func (f *Fmt) Fmt_b32(a uint32) *Fmt {
return f.b64(uint64(a)); return f.Fmt_b64(uint64(a));
} }
func (f *Fmt) b(a uint) *Fmt { func (f *Fmt) Fmt_b(a uint) *Fmt {
return f.b64(uint64(a)); return f.Fmt_b64(uint64(a));
} }
// character // character
func (f *Fmt) c(a int) *Fmt { func (f *Fmt) Fmt_c(a int) *Fmt {
f.pad(string(a)); f.pad(string(a));
f.clearflags(); f.clearflags();
return f; return f;
} }
// string // string
func (f *Fmt) s(s string) *Fmt { func (f *Fmt) Fmt_s(s string) *Fmt {
if f.prec_present { if f.prec_present {
if f.prec < len(s) { if f.prec < len(s) {
s = s[0:f.prec]; s = s[0:f.prec];
...@@ -373,7 +371,7 @@ func (f *Fmt) s(s string) *Fmt { ...@@ -373,7 +371,7 @@ func (f *Fmt) s(s string) *Fmt {
} }
// hexadecimal string // hexadecimal string
func (f *Fmt) sx(s string) *Fmt { func (f *Fmt) Fmt_sx(s string) *Fmt {
t := ""; t := "";
for i := 0; i < len(s); i++ { for i := 0; i < len(s); i++ {
if i > 0 && f.space { if i > 0 && f.space {
...@@ -388,7 +386,7 @@ func (f *Fmt) sx(s string) *Fmt { ...@@ -388,7 +386,7 @@ func (f *Fmt) sx(s string) *Fmt {
return f; return f;
} }
func (f *Fmt) sX(s string) *Fmt { func (f *Fmt) Fmt_sX(s string) *Fmt {
t := ""; t := "";
for i := 0; i < len(s); i++ { for i := 0; i < len(s); i++ {
v := s[i]; v := s[i];
...@@ -401,7 +399,7 @@ func (f *Fmt) sX(s string) *Fmt { ...@@ -401,7 +399,7 @@ func (f *Fmt) sX(s string) *Fmt {
} }
// quoted string // quoted string
func (f *Fmt) q(s string) *Fmt { func (f *Fmt) Fmt_q(s string) *Fmt {
var quoted string; var quoted string;
if f.sharp && strconv.CanBackquote(s) { if f.sharp && strconv.CanBackquote(s) {
quoted = "`"+s+"`"; quoted = "`"+s+"`";
...@@ -422,73 +420,73 @@ func Prec(f *Fmt, def int) int { ...@@ -422,73 +420,73 @@ func Prec(f *Fmt, def int) int {
return def; return def;
} }
func FmtString(f *Fmt, s string) *Fmt { func fmtString(f *Fmt, s string) *Fmt {
f.pad(s); f.pad(s);
f.clearflags(); f.clearflags();
return f; return f;
} }
// float64 // float64
func (f *Fmt) e64(a float64) *Fmt { func (f *Fmt) Fmt_e64(a float64) *Fmt {
return FmtString(f, strconv.ftoa64(a, 'e', Prec(f, 6))); return fmtString(f, strconv.ftoa64(a, 'e', Prec(f, 6)));
} }
func (f *Fmt) f64(a float64) *Fmt { func (f *Fmt) Fmt_f64(a float64) *Fmt {
return FmtString(f, strconv.ftoa64(a, 'f', Prec(f, 6))); return fmtString(f, strconv.ftoa64(a, 'f', Prec(f, 6)));
} }
func (f *Fmt) g64(a float64) *Fmt { func (f *Fmt) Fmt_g64(a float64) *Fmt {
return FmtString(f, strconv.ftoa64(a, 'g', Prec(f, -1))); return fmtString(f, strconv.ftoa64(a, 'g', Prec(f, -1)));
} }
func (f *Fmt) fb64(a float64) *Fmt { func (f *Fmt) Fmt_fb64(a float64) *Fmt {
return FmtString(f, strconv.ftoa64(a, 'b', 0)); return fmtString(f, strconv.ftoa64(a, 'b', 0));
} }
// float32 // float32
// cannot defer to float64 versions // cannot defer to float64 versions
// because it will get rounding wrong in corner cases. // because it will get rounding wrong in corner cases.
func (f *Fmt) e32(a float32) *Fmt { func (f *Fmt) Fmt_e32(a float32) *Fmt {
return FmtString(f, strconv.ftoa32(a, 'e', Prec(f, 6))); return fmtString(f, strconv.ftoa32(a, 'e', Prec(f, 6)));
} }
func (f *Fmt) f32(a float32) *Fmt { func (f *Fmt) Fmt_f32(a float32) *Fmt {
return FmtString(f, strconv.ftoa32(a, 'f', Prec(f, 6))); return fmtString(f, strconv.ftoa32(a, 'f', Prec(f, 6)));
} }
func (f *Fmt) g32(a float32) *Fmt { func (f *Fmt) Fmt_g32(a float32) *Fmt {
return FmtString(f, strconv.ftoa32(a, 'g', Prec(f, -1))); return fmtString(f, strconv.ftoa32(a, 'g', Prec(f, -1)));
} }
func (f *Fmt) fb32(a float32) *Fmt { func (f *Fmt) Fmt_fb32(a float32) *Fmt {
return FmtString(f, strconv.ftoa32(a, 'b', 0)); return fmtString(f, strconv.ftoa32(a, 'b', 0));
} }
// float // float
func (x *Fmt) f(a float) *Fmt { func (x *Fmt) f(a float) *Fmt {
if strconv.floatsize == 32 { if strconv.floatsize == 32 {
return x.f32(float32(a)) return x.Fmt_f32(float32(a))
} }
return x.f64(float64(a)) return x.Fmt_f64(float64(a))
} }
func (x *Fmt) e(a float) *Fmt { func (x *Fmt) e(a float) *Fmt {
if strconv.floatsize == 32 { if strconv.floatsize == 32 {
return x.e32(float32(a)) return x.Fmt_e32(float32(a))
} }
return x.e64(float64(a)) return x.Fmt_e64(float64(a))
} }
func (x *Fmt) g(a float) *Fmt { func (x *Fmt) g(a float) *Fmt {
if strconv.floatsize == 32 { if strconv.floatsize == 32 {
return x.g32(float32(a)) return x.Fmt_g32(float32(a))
} }
return x.g64(float64(a)) return x.Fmt_g64(float64(a))
} }
func (x *Fmt) fb(a float) *Fmt { func (x *Fmt) fb(a float) *Fmt {
if strconv.floatsize == 32 { if strconv.floatsize == 32 {
return x.fb32(float32(a)) return x.Fmt_fb32(float32(a))
} }
return x.fb64(float64(a)) return x.Fmt_fb64(float64(a))
} }
...@@ -28,38 +28,38 @@ export type Formatter interface { ...@@ -28,38 +28,38 @@ export type Formatter interface {
Flag(int) bool; Flag(int) bool;
} }
type Format interface { export type Format interface {
Format(f Formatter, c int); Format(f Formatter, c int);
} }
type String interface { export type String interface {
String() string String() string
} }
const Runeself = 0x80 const runeSelf = 0x80
const AllocSize = 32 const allocSize = 32
type P struct { type pp struct {
n int; n int;
buf []byte; buf []byte;
fmt *Fmt; fmt *Fmt;
} }
func Printer() *P { func Printer() *pp {
p := new(P); p := new(pp);
p.fmt = fmt.New(); p.fmt = fmt.New();
return p; return p;
} }
func (p *P) Width() (wid int, ok bool) { func (p *pp) Width() (wid int, ok bool) {
return p.fmt.wid, p.fmt.wid_present return p.fmt.wid, p.fmt.wid_present
} }
func (p *P) Precision() (prec int, ok bool) { func (p *pp) Precision() (prec int, ok bool) {
return p.fmt.prec, p.fmt.prec_present return p.fmt.prec, p.fmt.prec_present
} }
func (p *P) Flag(b int) bool { func (p *pp) Flag(b int) bool {
switch b { switch b {
case '-': case '-':
return p.fmt.minus; return p.fmt.minus;
...@@ -75,11 +75,11 @@ func (p *P) Flag(b int) bool { ...@@ -75,11 +75,11 @@ func (p *P) Flag(b int) bool {
return false return false
} }
func (p *P) ensure(n int) { func (p *pp) ensure(n int) {
if len(p.buf) < n { if len(p.buf) < n {
newn := AllocSize + len(p.buf); newn := allocSize + len(p.buf);
if newn < n { if newn < n {
newn = n + AllocSize newn = n + allocSize
} }
b := make([]byte, newn); b := make([]byte, newn);
for i := 0; i < p.n; i++ { for i := 0; i < p.n; i++ {
...@@ -89,7 +89,7 @@ func (p *P) ensure(n int) { ...@@ -89,7 +89,7 @@ func (p *P) ensure(n int) {
} }
} }
func (p *P) addstr(s string) { func (p *pp) addstr(s string) {
n := len(s); n := len(s);
p.ensure(p.n + n); p.ensure(p.n + n);
for i := 0; i < n; i++ { for i := 0; i < n; i++ {
...@@ -98,7 +98,7 @@ func (p *P) addstr(s string) { ...@@ -98,7 +98,7 @@ func (p *P) addstr(s string) {
} }
} }
func (p *P) addbytes(b []byte, start, end int) { func (p *pp) addbytes(b []byte, start, end int) {
p.ensure(p.n + end-start); p.ensure(p.n + end-start);
for i := start; i < end; i++ { for i := start; i < end; i++ {
p.buf[p.n] = b[i]; p.buf[p.n] = b[i];
...@@ -106,9 +106,9 @@ func (p *P) addbytes(b []byte, start, end int) { ...@@ -106,9 +106,9 @@ func (p *P) addbytes(b []byte, start, end int) {
} }
} }
func (p *P) add(c int) { func (p *pp) add(c int) {
p.ensure(p.n + 1); p.ensure(p.n + 1);
if c < Runeself { if c < runeSelf {
p.buf[p.n] = byte(c); p.buf[p.n] = byte(c);
p.n++; p.n++;
} else { } else {
...@@ -118,13 +118,13 @@ func (p *P) add(c int) { ...@@ -118,13 +118,13 @@ func (p *P) add(c int) {
// Implement Write so we can call fprintf on a P, for // Implement Write so we can call fprintf on a P, for
// recursive use in custom verbs. // recursive use in custom verbs.
func (p *P) Write(b []byte) (ret int, err *os.Error) { func (p *pp) Write(b []byte) (ret int, err *os.Error) {
p.addbytes(b, 0, len(b)); p.addbytes(b, 0, len(b));
return len(b), nil; return len(b), nil;
} }
func (p *P) doprintf(format string, v reflect.StructValue); func (p *pp) doprintf(format string, v reflect.StructValue);
func (p *P) doprint(v reflect.StructValue, addspace, addnewline bool); func (p *pp) 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.
...@@ -329,7 +329,7 @@ func parsenum(s string, start, end int) (n int, got bool, newi int) { ...@@ -329,7 +329,7 @@ func parsenum(s string, start, end int) (n int, got bool, newi int) {
return num, isnum, start; return num, isnum, start;
} }
func (p *P) printField(field reflect.Value) (was_string bool) { func (p *pp) printField(field reflect.Value) (was_string bool) {
inter := field.Interface(); inter := field.Interface();
if inter != nil { if inter != nil {
if stringer, ok := inter.(String); ok { if stringer, ok := inter.(String); ok {
...@@ -340,34 +340,34 @@ func (p *P) printField(field reflect.Value) (was_string bool) { ...@@ -340,34 +340,34 @@ func (p *P) printField(field reflect.Value) (was_string bool) {
s := ""; s := "";
switch field.Kind() { switch field.Kind() {
case reflect.BoolKind: case reflect.BoolKind:
s = p.fmt.boolean(field.(reflect.BoolValue).Get()).str(); s = p.fmt.Fmt_boolean(field.(reflect.BoolValue).Get()).Str();
case reflect.IntKind, reflect.Int8Kind, reflect.Int16Kind, reflect.Int32Kind, reflect.Int64Kind: case reflect.IntKind, reflect.Int8Kind, reflect.Int16Kind, reflect.Int32Kind, reflect.Int64Kind:
v, signed, ok := getInt(field); v, signed, ok := getInt(field);
s = p.fmt.d64(v).str(); s = p.fmt.Fmt_d64(v).Str();
case reflect.UintKind, reflect.Uint8Kind, reflect.Uint16Kind, reflect.Uint32Kind, reflect.Uint64Kind: case reflect.UintKind, reflect.Uint8Kind, reflect.Uint16Kind, reflect.Uint32Kind, reflect.Uint64Kind:
v, signed, ok := getInt(field); v, signed, ok := getInt(field);
s = p.fmt.ud64(uint64(v)).str(); s = p.fmt.Fmt_ud64(uint64(v)).Str();
case reflect.UintptrKind: case reflect.UintptrKind:
v, signed, ok := getInt(field); v, signed, ok := getInt(field);
p.fmt.sharp = !p.fmt.sharp; // turn 0x on by default p.fmt.sharp = !p.fmt.sharp; // turn 0x on by default
s = p.fmt.ux64(uint64(v)).str(); s = p.fmt.Fmt_ux64(uint64(v)).Str();
case reflect.Float32Kind: case reflect.Float32Kind:
v, ok := getFloat32(field); v, ok := getFloat32(field);
s = p.fmt.g32(v).str(); s = p.fmt.Fmt_g32(v).Str();
case reflect.Float64Kind, reflect.Float80Kind: case reflect.Float64Kind, reflect.Float80Kind:
v, ok := getFloat64(field); v, ok := getFloat64(field);
s = p.fmt.g64(v).str(); s = p.fmt.Fmt_g64(v).Str();
case reflect.FloatKind: case reflect.FloatKind:
if field.Type().Size()*8 == 32 { if field.Type().Size()*8 == 32 {
v, ok := getFloat32(field); v, ok := getFloat32(field);
s = p.fmt.g32(v).str(); s = p.fmt.Fmt_g32(v).Str();
} else { } else {
v, ok := getFloat64(field); v, ok := getFloat64(field);
s = p.fmt.g64(v).str(); s = p.fmt.Fmt_g64(v).Str();
} }
case reflect.StringKind: case reflect.StringKind:
v, ok := getString(field); v, ok := getString(field);
s = p.fmt.s(v).str(); s = p.fmt.Fmt_s(v).Str();
was_string = true; was_string = true;
case reflect.PtrKind: case reflect.PtrKind:
if v, ok := getPtr(field); v == 0 { if v, ok := getPtr(field); v == 0 {
...@@ -385,7 +385,7 @@ func (p *P) printField(field reflect.Value) (was_string bool) { ...@@ -385,7 +385,7 @@ func (p *P) printField(field reflect.Value) (was_string bool) {
p.addstr("]"); p.addstr("]");
} else { } else {
p.fmt.sharp = !p.fmt.sharp; // turn 0x on by default p.fmt.sharp = !p.fmt.sharp; // turn 0x on by default
s = p.fmt.uX64(uint64(v)).str(); s = p.fmt.Fmt_uX64(uint64(v)).Str();
} }
} }
case reflect.ArrayKind: case reflect.ArrayKind:
...@@ -433,7 +433,7 @@ func (p *P) printField(field reflect.Value) (was_string bool) { ...@@ -433,7 +433,7 @@ func (p *P) printField(field reflect.Value) (was_string bool) {
return was_string; return was_string;
} }
func (p *P) doprintf(format string, v reflect.StructValue) { func (p *pp) doprintf(format string, v reflect.StructValue) {
p.ensure(len(format)); // a good starting size p.ensure(len(format)); // a good starting size
end := len(format) - 1; end := len(format) - 1;
fieldnum := 0; // we process one field per non-trivial format fieldnum := 0; // we process one field per non-trivial format
...@@ -508,26 +508,26 @@ func (p *P) doprintf(format string, v reflect.StructValue) { ...@@ -508,26 +508,26 @@ func (p *P) doprintf(format string, v reflect.StructValue) {
// int // int
case 'b': case 'b':
if v, signed, ok := getInt(field); ok { if v, signed, ok := getInt(field); ok {
s = p.fmt.b64(uint64(v)).str() // always unsigned s = p.fmt.Fmt_b64(uint64(v)).Str() // always unsigned
} else if v, ok := getFloat32(field); ok { } else if v, ok := getFloat32(field); ok {
s = p.fmt.fb32(v).str() s = p.fmt.Fmt_fb32(v).Str()
} else if v, ok := getFloat64(field); ok { } else if v, ok := getFloat64(field); ok {
s = p.fmt.fb64(v).str() s = p.fmt.Fmt_fb64(v).Str()
} else { } else {
goto badtype goto badtype
} }
case 'c': case 'c':
if v, signed, ok := getInt(field); ok { if v, signed, ok := getInt(field); ok {
s = p.fmt.c(int(v)).str() s = p.fmt.Fmt_c(int(v)).Str()
} else { } else {
goto badtype goto badtype
} }
case 'd': case 'd':
if v, signed, ok := getInt(field); ok { if v, signed, ok := getInt(field); ok {
if signed { if signed {
s = p.fmt.d64(v).str() s = p.fmt.Fmt_d64(v).Str()
} else { } else {
s = p.fmt.ud64(uint64(v)).str() s = p.fmt.Fmt_ud64(uint64(v)).Str()
} }
} else { } else {
goto badtype goto badtype
...@@ -535,9 +535,9 @@ func (p *P) doprintf(format string, v reflect.StructValue) { ...@@ -535,9 +535,9 @@ func (p *P) doprintf(format string, v reflect.StructValue) {
case 'o': case 'o':
if v, signed, ok := getInt(field); ok { if v, signed, ok := getInt(field); ok {
if signed { if signed {
s = p.fmt.o64(v).str() s = p.fmt.Fmt_o64(v).Str()
} else { } else {
s = p.fmt.uo64(uint64(v)).str() s = p.fmt.Fmt_uo64(uint64(v)).Str()
} }
} else { } else {
goto badtype goto badtype
...@@ -545,24 +545,24 @@ func (p *P) doprintf(format string, v reflect.StructValue) { ...@@ -545,24 +545,24 @@ func (p *P) doprintf(format string, v reflect.StructValue) {
case 'x': case 'x':
if v, signed, ok := getInt(field); ok { if v, signed, ok := getInt(field); ok {
if signed { if signed {
s = p.fmt.x64(v).str() s = p.fmt.Fmt_x64(v).Str()
} else { } else {
s = p.fmt.ux64(uint64(v)).str() s = p.fmt.Fmt_ux64(uint64(v)).Str()
} }
} else if v, ok := getString(field); ok { } else if v, ok := getString(field); ok {
s = p.fmt.sx(v).str(); s = p.fmt.Fmt_sx(v).Str();
} else { } else {
goto badtype goto badtype
} }
case 'X': case 'X':
if v, signed, ok := getInt(field); ok { if v, signed, ok := getInt(field); ok {
if signed { if signed {
s = p.fmt.X64(v).str() s = p.fmt.Fmt_X64(v).Str()
} else { } else {
s = p.fmt.uX64(uint64(v)).str() s = p.fmt.Fmt_uX64(uint64(v)).Str()
} }
} else if v, ok := getString(field); ok { } else if v, ok := getString(field); ok {
s = p.fmt.sX(v).str(); s = p.fmt.Fmt_sX(v).Str();
} else { } else {
goto badtype goto badtype
} }
...@@ -570,25 +570,25 @@ func (p *P) doprintf(format string, v reflect.StructValue) { ...@@ -570,25 +570,25 @@ func (p *P) doprintf(format string, v reflect.StructValue) {
// float // float
case 'e': case 'e':
if v, ok := getFloat32(field); ok { if v, ok := getFloat32(field); ok {
s = p.fmt.e32(v).str() s = p.fmt.Fmt_e32(v).Str()
} else if v, ok := getFloat64(field); ok { } else if v, ok := getFloat64(field); ok {
s = p.fmt.e64(v).str() s = p.fmt.Fmt_e64(v).Str()
} else { } else {
goto badtype goto badtype
} }
case 'f': case 'f':
if v, ok := getFloat32(field); ok { if v, ok := getFloat32(field); ok {
s = p.fmt.f32(v).str() s = p.fmt.Fmt_f32(v).Str()
} else if v, ok := getFloat64(field); ok { } else if v, ok := getFloat64(field); ok {
s = p.fmt.f64(v).str() s = p.fmt.Fmt_f64(v).Str()
} else { } else {
goto badtype goto badtype
} }
case 'g': case 'g':
if v, ok := getFloat32(field); ok { if v, ok := getFloat32(field); ok {
s = p.fmt.g32(v).str() s = p.fmt.Fmt_g32(v).Str()
} else if v, ok := getFloat64(field); ok { } else if v, ok := getFloat64(field); ok {
s = p.fmt.g64(v).str() s = p.fmt.Fmt_g64(v).Str()
} else { } else {
goto badtype goto badtype
} }
...@@ -596,13 +596,13 @@ func (p *P) doprintf(format string, v reflect.StructValue) { ...@@ -596,13 +596,13 @@ func (p *P) doprintf(format string, v reflect.StructValue) {
// string // string
case 's': case 's':
if v, ok := getString(field); ok { if v, ok := getString(field); ok {
s = p.fmt.s(v).str() s = p.fmt.Fmt_s(v).Str()
} else { } else {
goto badtype goto badtype
} }
case 'q': case 'q':
if v, ok := getString(field); ok { if v, ok := getString(field); ok {
s = p.fmt.q(v).str() s = p.fmt.Fmt_q(v).Str()
} else { } else {
goto badtype goto badtype
} }
...@@ -613,7 +613,7 @@ func (p *P) doprintf(format string, v reflect.StructValue) { ...@@ -613,7 +613,7 @@ func (p *P) doprintf(format string, v reflect.StructValue) {
if v == 0 { if v == 0 {
s = "<nil>" s = "<nil>"
} else { } else {
s = "0x" + p.fmt.uX64(uint64(v)).str() s = "0x" + p.fmt.Fmt_uX64(uint64(v)).Str()
} }
} else { } else {
goto badtype goto badtype
...@@ -645,7 +645,7 @@ func (p *P) doprintf(format string, v reflect.StructValue) { ...@@ -645,7 +645,7 @@ func (p *P) doprintf(format string, v reflect.StructValue) {
} }
} }
func (p *P) doprint(v reflect.StructValue, addspace, addnewline bool) { func (p *pp) doprint(v reflect.StructValue, addspace, addnewline bool) {
prev_string := false; prev_string := false;
for fieldnum := 0; fieldnum < v.Len(); fieldnum++ { for fieldnum := 0; fieldnum < v.Len(); fieldnum++ {
// always add spaces if we're doing println // always add spaces if we're doing println
......
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