mirror of
https://github.com/golang/go.git
synced 2025-12-08 06:10:04 +00:00
document fmt.
the description of the format verbs still needs to be done. R=rsc DELTA=288 (88 added, 12 deleted, 188 changed) OCL=25814 CL=25833
This commit is contained in:
parent
b18e418410
commit
85647c94e6
2 changed files with 187 additions and 111 deletions
|
|
@ -8,14 +8,6 @@ import (
|
||||||
"strconv";
|
"strconv";
|
||||||
)
|
)
|
||||||
|
|
||||||
/*
|
|
||||||
Raw formatter. See print.go for a more palatable interface.
|
|
||||||
|
|
||||||
f := fmt.New();
|
|
||||||
print f.Fmt_d(1234).Fmt_s("\n").Str(); // create string, print it
|
|
||||||
f.Fmt_d(-1234).Fmt_s("\n").Put(); // print string
|
|
||||||
f.Fmt_ud(1<<63).Putnl(); // print string with automatic newline
|
|
||||||
*/
|
|
||||||
|
|
||||||
const nByte = 64;
|
const nByte = 64;
|
||||||
const nPows10 = 160;
|
const nPows10 = 160;
|
||||||
|
|
@ -33,6 +25,19 @@ func init() {
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/*
|
||||||
|
Fmt is the raw formatter used by Printf etc. Not meant for normal use.
|
||||||
|
See print.go for a more palatable interface.
|
||||||
|
|
||||||
|
Model is to accumulate operands into an internal buffer and then
|
||||||
|
retrieve the buffer in one hit using Str(), Putnl(), etc. The formatting
|
||||||
|
methods return ``self'' so the operations can be chained.
|
||||||
|
|
||||||
|
f := fmt.New();
|
||||||
|
print f.Fmt_d(1234).Fmt_s("\n").Str(); // create string, print it
|
||||||
|
f.Fmt_d(-1234).Fmt_s("\n").Put(); // print string
|
||||||
|
f.Fmt_ud(1<<63).Putnl(); // print string with automatic newline
|
||||||
|
*/
|
||||||
type Fmt struct {
|
type Fmt struct {
|
||||||
buf string;
|
buf string;
|
||||||
wid int;
|
wid int;
|
||||||
|
|
@ -68,12 +73,14 @@ func (f *Fmt) init() {
|
||||||
f.clearflags();
|
f.clearflags();
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// New returns a new initialized Fmt
|
||||||
func New() *Fmt {
|
func New() *Fmt {
|
||||||
f := new(Fmt);
|
f := new(Fmt);
|
||||||
f.init();
|
f.init();
|
||||||
return f;
|
return f;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// Str returns the buffered contents as a string and resets the Fmt.
|
||||||
func (f *Fmt) Str() string {
|
func (f *Fmt) Str() string {
|
||||||
s := f.buf;
|
s := f.buf;
|
||||||
f.clearbuf();
|
f.clearbuf();
|
||||||
|
|
@ -82,18 +89,21 @@ func (f *Fmt) Str() string {
|
||||||
return s;
|
return s;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// Put writes the buffered contents to stdout and resets the Fmt.
|
||||||
func (f *Fmt) Put() {
|
func (f *Fmt) Put() {
|
||||||
print(f.buf);
|
print(f.buf);
|
||||||
f.clearbuf();
|
f.clearbuf();
|
||||||
f.clearflags();
|
f.clearflags();
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// Putnl writes the buffered contents to stdout, followed by a newline, and resets the Fmt.
|
||||||
func (f *Fmt) Putnl() {
|
func (f *Fmt) Putnl() {
|
||||||
print(f.buf, "\n");
|
print(f.buf, "\n");
|
||||||
f.clearbuf();
|
f.clearbuf();
|
||||||
f.clearflags();
|
f.clearflags();
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// Wp sets the width and precision for formatting the next item.
|
||||||
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;
|
||||||
|
|
@ -102,12 +112,14 @@ func (f *Fmt) Wp(w, p int) *Fmt {
|
||||||
return f;
|
return f;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// P sets the precision for formatting the next item.
|
||||||
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;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// W sets the width for formatting the next item.
|
||||||
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;
|
||||||
|
|
@ -162,9 +174,9 @@ func putint(buf *[nByte]byte, i int, base, val uint64, digits *string) int {
|
||||||
return i-1;
|
return i-1;
|
||||||
}
|
}
|
||||||
|
|
||||||
// boolean
|
// Fmt_boolean formats a boolean.
|
||||||
func (f *Fmt) Fmt_boolean(a bool) *Fmt {
|
func (f *Fmt) Fmt_boolean(v bool) *Fmt {
|
||||||
if a {
|
if v {
|
||||||
f.pad("true");
|
f.pad("true");
|
||||||
} else {
|
} else {
|
||||||
f.pad("false");
|
f.pad("false");
|
||||||
|
|
@ -213,152 +225,167 @@ func (f *Fmt) integer(a int64, base uint, is_signed bool, digits *string) string
|
||||||
return string(buf)[i+1:nByte];
|
return string(buf)[i+1:nByte];
|
||||||
}
|
}
|
||||||
|
|
||||||
// decimal
|
// Fmt_d64 formats an int64 in decimal.
|
||||||
func (f *Fmt) Fmt_d64(a int64) *Fmt {
|
func (f *Fmt) Fmt_d64(v int64) *Fmt {
|
||||||
f.pad(f.integer(a, 10, true, &ldigits));
|
f.pad(f.integer(v, 10, true, &ldigits));
|
||||||
f.clearflags();
|
f.clearflags();
|
||||||
return f;
|
return f;
|
||||||
}
|
}
|
||||||
|
|
||||||
func (f *Fmt) Fmt_d32(a int32) *Fmt {
|
// Fmt_d32 formats an int32 in decimal.
|
||||||
return f.Fmt_d64(int64(a));
|
func (f *Fmt) Fmt_d32(v int32) *Fmt {
|
||||||
|
return f.Fmt_d64(int64(v));
|
||||||
}
|
}
|
||||||
|
|
||||||
func (f *Fmt) Fmt_d(a int) *Fmt {
|
// Fmt_d formats an int in decimal.
|
||||||
return f.Fmt_d64(int64(a));
|
func (f *Fmt) Fmt_d(v int) *Fmt {
|
||||||
|
return f.Fmt_d64(int64(v));
|
||||||
}
|
}
|
||||||
|
|
||||||
// unsigned Fmt_decimal
|
// Fmt_ud64 formats a uint64 in decimal.
|
||||||
func (f *Fmt) Fmt_ud64(a uint64) *Fmt {
|
func (f *Fmt) Fmt_ud64(v uint64) *Fmt {
|
||||||
f.pad(f.integer(int64(a), 10, false, &ldigits));
|
f.pad(f.integer(int64(v), 10, false, &ldigits));
|
||||||
f.clearflags();
|
f.clearflags();
|
||||||
return f;
|
return f;
|
||||||
}
|
}
|
||||||
|
|
||||||
func (f *Fmt) Fmt_ud32(a uint32) *Fmt {
|
// Fmt_ud32 formats a uint32 in decimal.
|
||||||
return f.Fmt_ud64(uint64(a));
|
func (f *Fmt) Fmt_ud32(v uint32) *Fmt {
|
||||||
|
return f.Fmt_ud64(uint64(v));
|
||||||
}
|
}
|
||||||
|
|
||||||
func (f *Fmt) Fmt_ud(a uint) *Fmt {
|
// Fmt_ud formats a uint in decimal.
|
||||||
return f.Fmt_ud64(uint64(a));
|
func (f *Fmt) Fmt_ud(v uint) *Fmt {
|
||||||
|
return f.Fmt_ud64(uint64(v));
|
||||||
}
|
}
|
||||||
|
|
||||||
// hexdecimal
|
// Fmt_x64 formats an int64 in hexadecimal.
|
||||||
func (f *Fmt) Fmt_x64(a int64) *Fmt {
|
func (f *Fmt) Fmt_x64(v int64) *Fmt {
|
||||||
f.pad(f.integer(a, 16, true, &ldigits));
|
f.pad(f.integer(v, 16, true, &ldigits));
|
||||||
f.clearflags();
|
f.clearflags();
|
||||||
return f;
|
return f;
|
||||||
}
|
}
|
||||||
|
|
||||||
func (f *Fmt) Fmt_x32(a int32) *Fmt {
|
// Fmt_x32 formats an int32 in hexadecimal.
|
||||||
return f.Fmt_x64(int64(a));
|
func (f *Fmt) Fmt_x32(v int32) *Fmt {
|
||||||
|
return f.Fmt_x64(int64(v));
|
||||||
}
|
}
|
||||||
|
|
||||||
func (f *Fmt) Fmt_x(a int) *Fmt {
|
// Fmt_x formats an int in hexadecimal.
|
||||||
return f.Fmt_x64(int64(a));
|
func (f *Fmt) Fmt_x(v int) *Fmt {
|
||||||
|
return f.Fmt_x64(int64(v));
|
||||||
}
|
}
|
||||||
|
|
||||||
// unsigned hexdecimal
|
// Fmt_ux64 formats a uint64 in hexadecimal.
|
||||||
func (f *Fmt) Fmt_ux64(a uint64) *Fmt {
|
func (f *Fmt) Fmt_ux64(v uint64) *Fmt {
|
||||||
f.pad(f.integer(int64(a), 16, false, &ldigits));
|
f.pad(f.integer(int64(v), 16, false, &ldigits));
|
||||||
f.clearflags();
|
f.clearflags();
|
||||||
return f;
|
return f;
|
||||||
}
|
}
|
||||||
|
|
||||||
func (f *Fmt) Fmt_ux32(a uint32) *Fmt {
|
// Fmt_ux32 formats a uint32 in hexadecimal.
|
||||||
return f.Fmt_ux64(uint64(a));
|
func (f *Fmt) Fmt_ux32(v uint32) *Fmt {
|
||||||
|
return f.Fmt_ux64(uint64(v));
|
||||||
}
|
}
|
||||||
|
|
||||||
func (f *Fmt) Fmt_ux(a uint) *Fmt {
|
// Fmt_ux formats a uint in hexadecimal.
|
||||||
return f.Fmt_ux64(uint64(a));
|
func (f *Fmt) Fmt_ux(v uint) *Fmt {
|
||||||
|
return f.Fmt_ux64(uint64(v));
|
||||||
}
|
}
|
||||||
|
|
||||||
// HEXADECIMAL
|
// Fmt_X64 formats an int64 in upper case hexadecimal.
|
||||||
func (f *Fmt) Fmt_X64(a int64) *Fmt {
|
func (f *Fmt) Fmt_X64(v int64) *Fmt {
|
||||||
f.pad(f.integer(a, 16, true, &udigits));
|
f.pad(f.integer(v, 16, true, &udigits));
|
||||||
f.clearflags();
|
f.clearflags();
|
||||||
return f;
|
return f;
|
||||||
}
|
}
|
||||||
|
|
||||||
func (f *Fmt) Fmt_X32(a int32) *Fmt {
|
// Fmt_X32 formats an int32 in upper case hexadecimal.
|
||||||
return f.Fmt_X64(int64(a));
|
func (f *Fmt) Fmt_X32(v int32) *Fmt {
|
||||||
|
return f.Fmt_X64(int64(v));
|
||||||
}
|
}
|
||||||
|
|
||||||
func (f *Fmt) Fmt_X(a int) *Fmt {
|
// Fmt_X formats an int in upper case hexadecimal.
|
||||||
return f.Fmt_X64(int64(a));
|
func (f *Fmt) Fmt_X(v int) *Fmt {
|
||||||
|
return f.Fmt_X64(int64(v));
|
||||||
}
|
}
|
||||||
|
|
||||||
// unsigned HEXADECIMAL
|
// Fmt_uX64 formats a uint64 in upper case hexadecimal.
|
||||||
func (f *Fmt) Fmt_uX64(a uint64) *Fmt {
|
func (f *Fmt) Fmt_uX64(v uint64) *Fmt {
|
||||||
f.pad(f.integer(int64(a), 16, false, &udigits));
|
f.pad(f.integer(int64(v), 16, false, &udigits));
|
||||||
f.clearflags();
|
f.clearflags();
|
||||||
return f;
|
return f;
|
||||||
}
|
}
|
||||||
|
|
||||||
func (f *Fmt) Fmt_uX32(a uint32) *Fmt {
|
// Fmt_uX32 formats a uint32 in upper case hexadecimal.
|
||||||
return f.Fmt_uX64(uint64(a));
|
func (f *Fmt) Fmt_uX32(v uint32) *Fmt {
|
||||||
|
return f.Fmt_uX64(uint64(v));
|
||||||
}
|
}
|
||||||
|
|
||||||
func (f *Fmt) Fmt_uX(a uint) *Fmt {
|
// Fmt_uX formats a uint in upper case hexadecimal.
|
||||||
return f.Fmt_uX64(uint64(a));
|
func (f *Fmt) Fmt_uX(v uint) *Fmt {
|
||||||
|
return f.Fmt_uX64(uint64(v));
|
||||||
}
|
}
|
||||||
|
|
||||||
// octal
|
// Fmt_o64 formats an int64 in octal.
|
||||||
func (f *Fmt) Fmt_o64(a int64) *Fmt {
|
func (f *Fmt) Fmt_o64(v int64) *Fmt {
|
||||||
f.pad(f.integer(a, 8, true, &ldigits));
|
f.pad(f.integer(v, 8, true, &ldigits));
|
||||||
f.clearflags();
|
f.clearflags();
|
||||||
return f;
|
return f;
|
||||||
}
|
}
|
||||||
|
|
||||||
func (f *Fmt) Fmt_o32(a int32) *Fmt {
|
// Fmt_o32 formats an int32 in octal.
|
||||||
return f.Fmt_o64(int64(a));
|
func (f *Fmt) Fmt_o32(v int32) *Fmt {
|
||||||
|
return f.Fmt_o64(int64(v));
|
||||||
}
|
}
|
||||||
|
|
||||||
func (f *Fmt) Fmt_o(a int) *Fmt {
|
// Fmt_o formats an int in octal.
|
||||||
return f.Fmt_o64(int64(a));
|
func (f *Fmt) Fmt_o(v int) *Fmt {
|
||||||
|
return f.Fmt_o64(int64(v));
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// Fmt_uo64 formats a uint64 in octal.
|
||||||
// unsigned octal
|
func (f *Fmt) Fmt_uo64(v uint64) *Fmt {
|
||||||
func (f *Fmt) Fmt_uo64(a uint64) *Fmt {
|
f.pad(f.integer(int64(v), 8, false, &ldigits));
|
||||||
f.pad(f.integer(int64(a), 8, false, &ldigits));
|
|
||||||
f.clearflags();
|
f.clearflags();
|
||||||
return f;
|
return f;
|
||||||
}
|
}
|
||||||
|
|
||||||
func (f *Fmt) Fmt_uo32(a uint32) *Fmt {
|
// Fmt_uo32 formats a uint32 in octal.
|
||||||
return f.Fmt_uo64(uint64(a));
|
func (f *Fmt) Fmt_uo32(v uint32) *Fmt {
|
||||||
|
return f.Fmt_uo64(uint64(v));
|
||||||
}
|
}
|
||||||
|
|
||||||
func (f *Fmt) Fmt_uo(a uint) *Fmt {
|
// Fmt_uo formats a uint in octal.
|
||||||
return f.Fmt_uo64(uint64(a));
|
func (f *Fmt) Fmt_uo(v uint) *Fmt {
|
||||||
|
return f.Fmt_uo64(uint64(v));
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// Fmt_b64 formats a uint64 in binary.
|
||||||
// unsigned binary
|
func (f *Fmt) Fmt_b64(v uint64) *Fmt {
|
||||||
func (f *Fmt) Fmt_b64(a uint64) *Fmt {
|
f.pad(f.integer(int64(v), 2, false, &ldigits));
|
||||||
f.pad(f.integer(int64(a), 2, false, &ldigits));
|
|
||||||
f.clearflags();
|
f.clearflags();
|
||||||
return f;
|
return f;
|
||||||
}
|
}
|
||||||
|
|
||||||
func (f *Fmt) Fmt_b32(a uint32) *Fmt {
|
// Fmt_b32 formats a uint32 in binary.
|
||||||
return f.Fmt_b64(uint64(a));
|
func (f *Fmt) Fmt_b32(v uint32) *Fmt {
|
||||||
|
return f.Fmt_b64(uint64(v));
|
||||||
}
|
}
|
||||||
|
|
||||||
func (f *Fmt) Fmt_b(a uint) *Fmt {
|
// Fmt_b formats a uint in binary.
|
||||||
return f.Fmt_b64(uint64(a));
|
func (f *Fmt) Fmt_b(v uint) *Fmt {
|
||||||
|
return f.Fmt_b64(uint64(v));
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// Fmt_c formats a Unicode character.
|
||||||
// character
|
func (f *Fmt) Fmt_c(v int) *Fmt {
|
||||||
func (f *Fmt) Fmt_c(a int) *Fmt {
|
f.pad(string(v));
|
||||||
f.pad(string(a));
|
|
||||||
f.clearflags();
|
f.clearflags();
|
||||||
return f;
|
return f;
|
||||||
}
|
}
|
||||||
|
|
||||||
// string
|
// Fmt_s formats a string.
|
||||||
func (f *Fmt) 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) {
|
||||||
|
|
@ -370,7 +397,7 @@ func (f *Fmt) Fmt_s(s string) *Fmt {
|
||||||
return f;
|
return f;
|
||||||
}
|
}
|
||||||
|
|
||||||
// hexadecimal string
|
// Fmt_sx formats a string as a hexadecimal encoding of its bytes.
|
||||||
func (f *Fmt) 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++ {
|
||||||
|
|
@ -386,6 +413,7 @@ func (f *Fmt) Fmt_sx(s string) *Fmt {
|
||||||
return f;
|
return f;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// Fmt_sX formats a string as an uppercase hexadecimal encoding of its bytes.
|
||||||
func (f *Fmt) 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++ {
|
||||||
|
|
@ -398,7 +426,7 @@ func (f *Fmt) Fmt_sX(s string) *Fmt {
|
||||||
return f;
|
return f;
|
||||||
}
|
}
|
||||||
|
|
||||||
// quoted string
|
// Fmt_q formats a string as a double-quoted, escaped Go string constant.
|
||||||
func (f *Fmt) 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) {
|
||||||
|
|
@ -426,40 +454,48 @@ func fmtString(f *Fmt, s string) *Fmt {
|
||||||
return f;
|
return f;
|
||||||
}
|
}
|
||||||
|
|
||||||
// float64
|
// Fmt_e64 formats a float64 in the form -1.23e+12.
|
||||||
func (f *Fmt) Fmt_e64(a float64) *Fmt {
|
func (f *Fmt) Fmt_e64(v float64) *Fmt {
|
||||||
return fmtString(f, strconv.Ftoa64(a, 'e', doPrec(f, 6)));
|
return fmtString(f, strconv.Ftoa64(v, 'e', doPrec(f, 6)));
|
||||||
}
|
}
|
||||||
|
|
||||||
func (f *Fmt) Fmt_f64(a float64) *Fmt {
|
// Fmt_f64 formats a float64 in the form -1.23.
|
||||||
return fmtString(f, strconv.Ftoa64(a, 'f', doPrec(f, 6)));
|
func (f *Fmt) Fmt_f64(v float64) *Fmt {
|
||||||
|
return fmtString(f, strconv.Ftoa64(v, 'f', doPrec(f, 6)));
|
||||||
}
|
}
|
||||||
|
|
||||||
func (f *Fmt) Fmt_g64(a float64) *Fmt {
|
// Fmt_g64 formats a float64 in the 'f' or 'e' form according to size.
|
||||||
return fmtString(f, strconv.Ftoa64(a, 'g', doPrec(f, -1)));
|
func (f *Fmt) Fmt_g64(v float64) *Fmt {
|
||||||
|
return fmtString(f, strconv.Ftoa64(v, 'g', doPrec(f, -1)));
|
||||||
}
|
}
|
||||||
|
|
||||||
func (f *Fmt) Fmt_fb64(a float64) *Fmt {
|
// Fmt_fb64 formats a float64 in the form -123p3 (exponent is power of 2).
|
||||||
return fmtString(f, strconv.Ftoa64(a, 'b', 0));
|
func (f *Fmt) Fmt_fb64(v float64) *Fmt {
|
||||||
|
return fmtString(f, strconv.Ftoa64(v, '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) Fmt_e32(a float32) *Fmt {
|
|
||||||
return fmtString(f, strconv.Ftoa32(a, 'e', doPrec(f, 6)));
|
// Fmt_e32 formats a float32 in the form -1.23e+12.
|
||||||
|
func (f *Fmt) Fmt_e32(v float32) *Fmt {
|
||||||
|
return fmtString(f, strconv.Ftoa32(v, 'e', doPrec(f, 6)));
|
||||||
}
|
}
|
||||||
|
|
||||||
func (f *Fmt) Fmt_f32(a float32) *Fmt {
|
// Fmt_f32 formats a float32 in the form -1.23.
|
||||||
return fmtString(f, strconv.Ftoa32(a, 'f', doPrec(f, 6)));
|
func (f *Fmt) Fmt_f32(v float32) *Fmt {
|
||||||
|
return fmtString(f, strconv.Ftoa32(v, 'f', doPrec(f, 6)));
|
||||||
}
|
}
|
||||||
|
|
||||||
func (f *Fmt) Fmt_g32(a float32) *Fmt {
|
// Fmt_g32 formats a float32 in the 'f' or 'e' form according to size.
|
||||||
return fmtString(f, strconv.Ftoa32(a, 'g', doPrec(f, -1)));
|
func (f *Fmt) Fmt_g32(v float32) *Fmt {
|
||||||
|
return fmtString(f, strconv.Ftoa32(v, 'g', doPrec(f, -1)));
|
||||||
}
|
}
|
||||||
|
|
||||||
func (f *Fmt) Fmt_fb32(a float32) *Fmt {
|
// Fmt_fb32 formats a float32 in the form -123p3 (exponent is power of 2).
|
||||||
return fmtString(f, strconv.Ftoa32(a, 'b', 0));
|
func (f *Fmt) Fmt_fb32(v float32) *Fmt {
|
||||||
|
return fmtString(f, strconv.Ftoa32(v, 'b', 0));
|
||||||
}
|
}
|
||||||
|
|
||||||
// float
|
// float
|
||||||
|
|
|
||||||
|
|
@ -2,12 +2,21 @@
|
||||||
// Use of this source code is governed by a BSD-style
|
// Use of this source code is governed by a BSD-style
|
||||||
// license that can be found in the LICENSE file.
|
// license that can be found in the LICENSE file.
|
||||||
|
|
||||||
|
// Package fmt implements formatted I/O with functions analogous
|
||||||
|
// to C's printf. Because of reflection knowledge it does not need
|
||||||
|
// to be told about sizes and signedness (no %llud etc. - just %d).
|
||||||
|
// Still to do: document the formats properly. For now, like C but:
|
||||||
|
// - don't need l or u flags - type of integer tells that.
|
||||||
|
// - %v prints any value using its native format.
|
||||||
|
// - for each Printf-like fn, there is also a Print fn that takes no format
|
||||||
|
// and is equivalent to saying %v for every operand.
|
||||||
|
// - another variant Println inserts blanks and appends a newline.
|
||||||
|
// - if an operand implements method String() that method will
|
||||||
|
// be used for %v, %s, or Print etc.
|
||||||
|
// - if an operand implements interface Formatter, that interface can
|
||||||
|
// be used for fine control of formatting.
|
||||||
package fmt
|
package fmt
|
||||||
|
|
||||||
/*
|
|
||||||
C-like printf, but because of reflection knowledge does not need
|
|
||||||
to be told about sizes and signedness (no %llud etc. - just %d).
|
|
||||||
*/
|
|
||||||
|
|
||||||
import (
|
import (
|
||||||
"fmt";
|
"fmt";
|
||||||
|
|
@ -17,27 +26,37 @@ import (
|
||||||
"utf8";
|
"utf8";
|
||||||
)
|
)
|
||||||
|
|
||||||
// Representation of printer state passed to custom formatters.
|
// Formatter represents the printer state passed to custom formatters.
|
||||||
// Provides access to the io.Write interface plus information about
|
// It provides access to the io.Write interface plus information about
|
||||||
// the active formatting verb.
|
// the flags and options for the operand's format specifier.
|
||||||
type Formatter interface {
|
type Formatter interface {
|
||||||
|
// Write is the function to call to emit formatted output to be printed.
|
||||||
Write(b []byte) (ret int, err *os.Error);
|
Write(b []byte) (ret int, err *os.Error);
|
||||||
|
// Width returns the value of the width option and whether it has been set.
|
||||||
Width() (wid int, ok bool);
|
Width() (wid int, ok bool);
|
||||||
|
// Precision returns the value of the precision option and whether it has been set.
|
||||||
Precision() (prec int, ok bool);
|
Precision() (prec int, ok bool);
|
||||||
|
|
||||||
// flags
|
// Flag returns whether the flag c, a character, has been set.
|
||||||
Flag(int) bool;
|
Flag(int) bool;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// Format is the interface implemented by objects with a custom formatter.
|
||||||
|
// The implementation of Format may call Sprintf or Fprintf(f) etc.
|
||||||
|
// to generate its output.
|
||||||
type Format interface {
|
type Format interface {
|
||||||
Format(f Formatter, c int);
|
Format(f Formatter, c int);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// String represents any object being printed that has a String() method that
|
||||||
|
// returns a string, which defines the ``native'' format for that object.
|
||||||
|
// Any such object will be printed using that method if passed
|
||||||
|
// as operand to a %s or %v format or to an unformatted printer such as Print.
|
||||||
type String interface {
|
type String interface {
|
||||||
String() string
|
String() string
|
||||||
}
|
}
|
||||||
|
|
||||||
const runeSelf = 0x80
|
const runeSelf = utf8.RuneSelf
|
||||||
const allocSize = 32
|
const allocSize = 32
|
||||||
|
|
||||||
type pp struct {
|
type pp struct {
|
||||||
|
|
@ -129,6 +148,7 @@ 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.
|
||||||
|
|
||||||
|
// Fprintf formats according to a format specifier and writes to w.
|
||||||
func Fprintf(w io.Write, format string, a ...) (n int, error *os.Error) {
|
func Fprintf(w io.Write, format string, a ...) (n int, error *os.Error) {
|
||||||
v := reflect.NewValue(a).(reflect.StructValue);
|
v := reflect.NewValue(a).(reflect.StructValue);
|
||||||
p := newPrinter();
|
p := newPrinter();
|
||||||
|
|
@ -137,11 +157,13 @@ func Fprintf(w io.Write, format string, a ...) (n int, error *os.Error) {
|
||||||
return n, error;
|
return n, error;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// Printf formats according to a format specifier and writes to standard output.
|
||||||
func Printf(format string, v ...) (n int, errno *os.Error) {
|
func Printf(format string, v ...) (n int, errno *os.Error) {
|
||||||
n, errno = Fprintf(os.Stdout, format, v);
|
n, errno = Fprintf(os.Stdout, format, v);
|
||||||
return n, errno;
|
return n, errno;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// Sprintf formats according to a format specifier and returns the resulting string.
|
||||||
func Sprintf(format string, a ...) string {
|
func Sprintf(format string, a ...) string {
|
||||||
v := reflect.NewValue(a).(reflect.StructValue);
|
v := reflect.NewValue(a).(reflect.StructValue);
|
||||||
p := newPrinter();
|
p := newPrinter();
|
||||||
|
|
@ -150,9 +172,10 @@ func Sprintf(format string, a ...) string {
|
||||||
return s;
|
return s;
|
||||||
}
|
}
|
||||||
|
|
||||||
// These routines do not take a format string and add spaces only
|
// These routines do not take a format string
|
||||||
// when the operand on neither side is a string.
|
|
||||||
|
|
||||||
|
// Fprint formats using the default formats for its operands and writes to w.
|
||||||
|
// Spaces are added between operands when neither is a string.
|
||||||
func Fprint(w io.Write, a ...) (n int, error *os.Error) {
|
func Fprint(w io.Write, a ...) (n int, error *os.Error) {
|
||||||
v := reflect.NewValue(a).(reflect.StructValue);
|
v := reflect.NewValue(a).(reflect.StructValue);
|
||||||
p := newPrinter();
|
p := newPrinter();
|
||||||
|
|
@ -161,11 +184,15 @@ func Fprint(w io.Write, a ...) (n int, error *os.Error) {
|
||||||
return n, error;
|
return n, error;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// Print formats using the default formats for its operands and writes to standard output.
|
||||||
|
// Spaces are added between operands when neither is a string.
|
||||||
func Print(v ...) (n int, errno *os.Error) {
|
func Print(v ...) (n int, errno *os.Error) {
|
||||||
n, errno = Fprint(os.Stdout, v);
|
n, errno = Fprint(os.Stdout, v);
|
||||||
return n, errno;
|
return n, errno;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// Sprint formats using the default formats for its operands and returns the resulting string.
|
||||||
|
// Spaces are added between operands when neither is a string.
|
||||||
func Sprint(a ...) string {
|
func Sprint(a ...) string {
|
||||||
v := reflect.NewValue(a).(reflect.StructValue);
|
v := reflect.NewValue(a).(reflect.StructValue);
|
||||||
p := newPrinter();
|
p := newPrinter();
|
||||||
|
|
@ -178,6 +205,8 @@ func Sprint(a ...) 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.
|
||||||
|
|
||||||
|
// Fprintln formats using the default formats for its operands and writes to w.
|
||||||
|
// Spaces are always added between operands and a newline is appended.
|
||||||
func Fprintln(w io.Write, a ...) (n int, error *os.Error) {
|
func Fprintln(w io.Write, a ...) (n int, error *os.Error) {
|
||||||
v := reflect.NewValue(a).(reflect.StructValue);
|
v := reflect.NewValue(a).(reflect.StructValue);
|
||||||
p := newPrinter();
|
p := newPrinter();
|
||||||
|
|
@ -186,11 +215,15 @@ func Fprintln(w io.Write, a ...) (n int, error *os.Error) {
|
||||||
return n, error;
|
return n, error;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// Println formats using the default formats for its operands and writes to standard output.
|
||||||
|
// Spaces are always added between operands and a newline is appended.
|
||||||
func Println(v ...) (n int, errno *os.Error) {
|
func Println(v ...) (n int, errno *os.Error) {
|
||||||
n, errno = Fprintln(os.Stdout, v);
|
n, errno = Fprintln(os.Stdout, v);
|
||||||
return n, errno;
|
return n, errno;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// Sprintln formats using the default formats for its operands and returns the resulting string.
|
||||||
|
// Spaces are always added between operands and a newline is appended.
|
||||||
func Sprintln(a ...) string {
|
func Sprintln(a ...) string {
|
||||||
v := reflect.NewValue(a).(reflect.StructValue);
|
v := reflect.NewValue(a).(reflect.StructValue);
|
||||||
p := newPrinter();
|
p := newPrinter();
|
||||||
|
|
@ -596,6 +629,13 @@ func (p *pp) doprintf(format string, v reflect.StructValue) {
|
||||||
|
|
||||||
// string
|
// string
|
||||||
case 's':
|
case 's':
|
||||||
|
if inter != nil {
|
||||||
|
// if object implements String, use the result.
|
||||||
|
if stringer, ok := inter.(String); ok {
|
||||||
|
s = p.fmt.Fmt_s(stringer.String()).Str();
|
||||||
|
break;
|
||||||
|
}
|
||||||
|
}
|
||||||
if v, ok := getString(field); ok {
|
if v, ok := getString(field); ok {
|
||||||
s = p.fmt.Fmt_s(v).Str()
|
s = p.fmt.Fmt_s(v).Str()
|
||||||
} else {
|
} else {
|
||||||
|
|
|
||||||
Loading…
Add table
Add a link
Reference in a new issue