udigits = "0123456789ABCDEF";
)
+const (
+ signed = true;
+ unsigned = false;
+)
+
var padZeroBytes = make([]byte, nByte)
var padSpaceBytes = make([]byte, nByte)
// A fmt is the raw formatter used by Printf etc.
// It prints into a bytes.Buffer that must be set up externally.
type fmt struct {
- intbuf [nByte]byte;
- buf *bytes.Buffer;
- wid int;
+ intbuf [nByte]byte;
+ buf *bytes.Buffer;
+ // width, precision
+ wid int;
+ prec int;
+ // flags
widPresent bool;
- prec int;
precPresent bool;
- // flags
- minus bool;
- plus bool;
- sharp bool;
- space bool;
- zero bool;
+ minus bool;
+ plus bool;
+ sharp bool;
+ space bool;
+ zero bool;
}
func (f *fmt) clearflags() {
}
// Append b to f.buf, padded on left (w > 0) or right (w < 0 or f.minus)
-func (f *fmt) padBytes(b []byte) {
+// clear flags aftewards.
+func (f *fmt) pad(b []byte) {
var padding []byte;
var left, right int;
if f.widPresent && f.wid != 0 {
if right > 0 {
f.writePadding(right, padding)
}
+ f.clearflags();
}
-// append s to buf, padded on left (w > 0) or right (w < 0 or f.minus)
-func (f *fmt) pad(s string) {
+// append s to buf, padded on left (w > 0) or right (w < 0 or f.minus).
+// clear flags aftewards.
+func (f *fmt) padString(s string) {
var padding []byte;
var left, right int;
if f.widPresent && f.wid != 0 {
if right > 0 {
f.writePadding(right, padding)
}
+ f.clearflags();
}
-// format val into buf, ending at buf[i]. (printing is easier right-to-left;
-// that's why the bidi languages are right-to-left except for numbers. wait,
-// 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
-// but it's not worth the duplication, so val has 64 bits.
func putint(buf []byte, base, val uint64, digits string) int {
i := len(buf) - 1;
for val >= base {
// fmt_boolean formats a boolean.
func (f *fmt) fmt_boolean(v bool) {
if v {
- f.pad("true")
+ f.padString("true")
} else {
- f.pad("false")
+ f.padString("false")
}
- f.clearflags();
}
-// integer; interprets prec but not wid.
-func (f *fmt) integer(a int64, base uint, is_signed bool, digits string) []byte {
+// integer; interprets prec but not wid. Once formatted, result is sent to pad()
+// and then flags are cleared.
+func (f *fmt) integer(a int64, base uint64, signedness bool, digits string) {
var buf []byte = &f.intbuf;
- negative := is_signed && a < 0;
+ negative := signedness == signed && a < 0;
if negative {
a = -a
}
}
}
- i := putint(buf, uint64(base), uint64(a), digits);
- for i > 0 && prec > (nByte-1-i) {
- buf[i] = '0';
+ // format a into buf, ending at buf[i]. (printing is easier right-to-left.)
+ // a is made into unsigned ua. we could make things
+ // marginally faster by splitting the 32-bit case out into a separate
+ // block but it's not worth the duplication, so ua has 64 bits.
+ i := len(f.intbuf);
+ ua := uint64(a);
+ for ua >= base {
+ i--;
+ buf[i] = digits[ua%base];
+ ua /= base;
+ }
+ i--;
+ buf[i] = digits[ua];
+ for i > 0 && prec > nByte-i {
i--;
+ buf[i] = '0';
}
+ // Various prefixes: 0x, -, etc.
if f.sharp {
switch base {
case 8:
- if buf[i+1] != '0' {
- buf[i] = '0';
+ if buf[i] != '0' {
i--;
+ buf[i] = '0';
}
case 16:
+ i--;
buf[i] = 'x' + digits[10] - 'a';
i--;
buf[i] = '0';
- i--;
}
}
if negative {
- buf[i] = '-';
i--;
+ buf[i] = '-';
} else if f.plus {
- buf[i] = '+';
i--;
+ buf[i] = '+';
} else if f.space {
- buf[i] = ' ';
i--;
+ buf[i] = ' ';
}
- return buf[i+1 : nByte];
+ f.pad(buf[i:]);
}
// fmt_d64 formats an int64 in decimal.
-func (f *fmt) fmt_d64(v int64) {
- f.padBytes(f.integer(v, 10, true, ldigits));
- f.clearflags();
-}
+func (f *fmt) fmt_d64(v int64) { f.integer(v, 10, signed, ldigits) }
// fmt_d32 formats an int32 in decimal.
-func (f *fmt) fmt_d32(v int32) { f.fmt_d64(int64(v)) }
+func (f *fmt) fmt_d32(v int32) { f.integer(int64(v), 10, signed, ldigits) }
// fmt_d formats an int in decimal.
-func (f *fmt) fmt_d(v int) { f.fmt_d64(int64(v)) }
+func (f *fmt) fmt_d(v int) { f.integer(int64(v), 10, signed, ldigits) }
// fmt_ud64 formats a uint64 in decimal.
-func (f *fmt) fmt_ud64(v uint64) *fmt {
- f.padBytes(f.integer(int64(v), 10, false, ldigits));
- f.clearflags();
- return f;
-}
+func (f *fmt) fmt_ud64(v uint64) { f.integer(int64(v), 10, unsigned, ldigits) }
// fmt_ud32 formats a uint32 in decimal.
-func (f *fmt) fmt_ud32(v uint32) { f.fmt_ud64(uint64(v)) }
+func (f *fmt) fmt_ud32(v uint32) { f.integer(int64(v), 10, unsigned, ldigits) }
// fmt_ud formats a uint in decimal.
-func (f *fmt) fmt_ud(v uint) { f.fmt_ud64(uint64(v)) }
+func (f *fmt) fmt_ud(v uint) { f.integer(int64(v), 10, unsigned, ldigits) }
// fmt_x64 formats an int64 in hexadecimal.
-func (f *fmt) fmt_x64(v int64) {
- f.padBytes(f.integer(v, 16, true, ldigits));
- f.clearflags();
-}
+func (f *fmt) fmt_x64(v int64) { f.integer(v, 16, signed, ldigits) }
// fmt_x32 formats an int32 in hexadecimal.
-func (f *fmt) fmt_x32(v int32) { f.fmt_x64(int64(v)) }
+func (f *fmt) fmt_x32(v int32) { f.integer(int64(v), 16, signed, ldigits) }
// fmt_x formats an int in hexadecimal.
-func (f *fmt) fmt_x(v int) { f.fmt_x64(int64(v)) }
+func (f *fmt) fmt_x(v int) { f.integer(int64(v), 16, signed, ldigits) }
// fmt_ux64 formats a uint64 in hexadecimal.
-func (f *fmt) fmt_ux64(v uint64) {
- f.padBytes(f.integer(int64(v), 16, false, ldigits));
- f.clearflags();
-}
+func (f *fmt) fmt_ux64(v uint64) { f.integer(int64(v), 16, unsigned, ldigits) }
// fmt_ux32 formats a uint32 in hexadecimal.
-func (f *fmt) fmt_ux32(v uint32) { f.fmt_ux64(uint64(v)) }
+func (f *fmt) fmt_ux32(v uint32) { f.integer(int64(v), 16, unsigned, ldigits) }
// fmt_ux formats a uint in hexadecimal.
-func (f *fmt) fmt_ux(v uint) { f.fmt_ux64(uint64(v)) }
+func (f *fmt) fmt_ux(v uint) { f.integer(int64(v), 16, unsigned, ldigits) }
// fmt_X64 formats an int64 in upper case hexadecimal.
-func (f *fmt) fmt_X64(v int64) {
- f.padBytes(f.integer(v, 16, true, udigits));
- f.clearflags();
-}
+func (f *fmt) fmt_X64(v int64) { f.integer(v, 16, signed, udigits) }
// fmt_X32 formats an int32 in upper case hexadecimal.
-func (f *fmt) fmt_X32(v int32) { f.fmt_X64(int64(v)) }
+func (f *fmt) fmt_X32(v int32) { f.integer(int64(v), 16, signed, udigits) }
// fmt_X formats an int in upper case hexadecimal.
-func (f *fmt) fmt_X(v int) { f.fmt_X64(int64(v)) }
+func (f *fmt) fmt_X(v int) { f.integer(int64(v), 16, signed, udigits) }
// fmt_uX64 formats a uint64 in upper case hexadecimal.
-func (f *fmt) fmt_uX64(v uint64) {
- f.padBytes(f.integer(int64(v), 16, false, udigits));
- f.clearflags();
-}
+func (f *fmt) fmt_uX64(v uint64) { f.integer(int64(v), 16, unsigned, udigits) }
// fmt_uX32 formats a uint32 in upper case hexadecimal.
-func (f *fmt) fmt_uX32(v uint32) { f.fmt_uX64(uint64(v)) }
+func (f *fmt) fmt_uX32(v uint32) { f.integer(int64(v), 16, unsigned, udigits) }
// fmt_uX formats a uint in upper case hexadecimal.
-func (f *fmt) fmt_uX(v uint) { f.fmt_uX64(uint64(v)) }
+func (f *fmt) fmt_uX(v uint) { f.integer(int64(v), 16, unsigned, udigits) }
// fmt_o64 formats an int64 in octal.
-func (f *fmt) fmt_o64(v int64) {
- f.padBytes(f.integer(v, 8, true, ldigits));
- f.clearflags();
-}
+func (f *fmt) fmt_o64(v int64) { f.integer(v, 8, signed, ldigits) }
// fmt_o32 formats an int32 in octal.
-func (f *fmt) fmt_o32(v int32) { f.fmt_o64(int64(v)) }
+func (f *fmt) fmt_o32(v int32) { f.integer(int64(v), 8, signed, ldigits) }
// fmt_o formats an int in octal.
-func (f *fmt) fmt_o(v int) { f.fmt_o64(int64(v)) }
+func (f *fmt) fmt_o(v int) { f.integer(int64(v), 8, signed, ldigits) }
// fmt_uo64 formats a uint64 in octal.
-func (f *fmt) fmt_uo64(v uint64) {
- f.padBytes(f.integer(int64(v), 8, false, ldigits));
- f.clearflags();
-}
+func (f *fmt) fmt_uo64(v uint64) { f.integer(int64(v), 8, unsigned, ldigits) }
// fmt_uo32 formats a uint32 in octal.
-func (f *fmt) fmt_uo32(v uint32) { f.fmt_uo64(uint64(v)) }
+func (f *fmt) fmt_uo32(v uint32) { f.integer(int64(v), 8, unsigned, ldigits) }
// fmt_uo formats a uint in octal.
-func (f *fmt) fmt_uo(v uint) { f.fmt_uo64(uint64(v)) }
+func (f *fmt) fmt_uo(v uint) { f.integer(int64(v), 8, unsigned, ldigits) }
// fmt_b64 formats a uint64 in binary.
-func (f *fmt) fmt_b64(v uint64) {
- f.padBytes(f.integer(int64(v), 2, false, ldigits));
- f.clearflags();
-}
+func (f *fmt) fmt_b64(v uint64) { f.integer(int64(v), 2, unsigned, ldigits) }
// fmt_b32 formats a uint32 in binary.
-func (f *fmt) fmt_b32(v uint32) { f.fmt_b64(uint64(v)) }
+func (f *fmt) fmt_b32(v uint32) { f.integer(int64(v), 2, unsigned, ldigits) }
// fmt_b formats a uint in binary.
-func (f *fmt) fmt_b(v uint) { f.fmt_b64(uint64(v)) }
+func (f *fmt) fmt_b(v uint) { f.integer(int64(v), 2, unsigned, ldigits) }
// fmt_c formats a Unicode character.
-func (f *fmt) fmt_c(v int) {
- f.pad(string(v));
- f.clearflags();
-}
+func (f *fmt) fmt_c(v int) { f.padString(string(v)) }
// fmt_s formats a string.
func (f *fmt) fmt_s(s string) {
s = s[0:f.prec]
}
}
- f.pad(s);
- f.clearflags();
+ f.padString(s);
}
// fmt_sx formats a string as a hexadecimal encoding of its bytes.
t += string(ldigits[v>>4]);
t += string(ldigits[v&0xF]);
}
- f.pad(t);
- f.clearflags();
+ f.padString(t);
}
// fmt_sX formats a string as an uppercase hexadecimal encoding of its bytes.
t += string(udigits[v>>4]);
t += string(udigits[v&0xF]);
}
- f.pad(t);
- f.clearflags();
+ f.padString(t);
}
// fmt_q formats a string as a double-quoted, escaped Go string constant.
} else {
quoted = strconv.Quote(s)
}
- f.pad(quoted);
- f.clearflags();
+ f.padString(quoted);
}
// floating-point
return def;
}
-func fmtString(f *fmt, s string) {
- f.pad(s);
- f.clearflags();
-}
-
-// Add a plus sign or space to the string if missing and required.
+// Add a plus sign or space to the floating-point string representation if missing and required.
func (f *fmt) plusSpace(s string) {
if s[0] != '-' {
if f.plus {
s = " " + s
}
}
- fmtString(f, s);
+ f.padString(s);
}
// fmt_e64 formats a float64 in the form -1.23e+12.
func (f *fmt) fmt_G32(v float32) { f.plusSpace(strconv.Ftoa32(v, 'G', doPrec(f, -1))) }
// fmt_fb32 formats a float32 in the form -123p3 (exponent is power of 2).
-func (f *fmt) fmt_fb32(v float32) { fmtString(f, strconv.Ftoa32(v, 'b', 0)) }
+func (f *fmt) fmt_fb32(v float32) { f.padString(strconv.Ftoa32(v, 'b', 0)) }
// float
func (x *fmt) f(a float) {