2008-03-27 01:06:21 -06:00
|
|
|
// Copyright 2009 The Go Authors. All rights reserved.
|
|
|
|
// Use of this source code is governed by a BSD-style
|
|
|
|
// license that can be found in the LICENSE file.
|
|
|
|
|
|
|
|
package fmt
|
|
|
|
|
2008-11-24 15:51:33 -07:00
|
|
|
import (
|
2009-12-06 13:03:52 -07:00
|
|
|
"bytes";
|
2008-11-24 15:51:33 -07:00
|
|
|
"strconv";
|
|
|
|
)
|
2008-11-17 13:34:03 -07:00
|
|
|
|
2009-12-03 01:04:40 -07:00
|
|
|
const (
|
|
|
|
nByte = 64;
|
|
|
|
nPows10 = 160;
|
2008-03-27 01:06:21 -06:00
|
|
|
|
2009-12-03 01:04:40 -07:00
|
|
|
ldigits = "0123456789abcdef";
|
|
|
|
udigits = "0123456789ABCDEF";
|
|
|
|
)
|
2008-07-08 15:20:04 -06:00
|
|
|
|
2009-12-06 13:03:52 -07:00
|
|
|
var padZeroBytes = make([]byte, nByte)
|
|
|
|
var padSpaceBytes = make([]byte, nByte)
|
|
|
|
|
|
|
|
var newline = []byte{'\n'}
|
2009-12-03 01:04:40 -07:00
|
|
|
|
|
|
|
func init() {
|
2009-12-06 13:03:52 -07:00
|
|
|
for i := 0; i < nByte; i++ {
|
|
|
|
padZeroBytes[i] = '0';
|
|
|
|
padSpaceBytes[i] = ' ';
|
2009-12-03 01:04:40 -07:00
|
|
|
}
|
|
|
|
}
|
2008-07-19 15:52:01 -06:00
|
|
|
|
2009-03-06 04:35:38 -07:00
|
|
|
/*
|
|
|
|
Fmt is the raw formatter used by Printf etc. Not meant for normal use.
|
2009-12-06 13:03:52 -07:00
|
|
|
It prints into a bytes.Buffer that must be set up externally.
|
2009-03-06 04:35:38 -07:00
|
|
|
See print.go for a more palatable interface.
|
|
|
|
*/
|
2009-01-20 15:40:40 -07:00
|
|
|
type Fmt struct {
|
2009-12-06 13:03:52 -07:00
|
|
|
intbuf [nByte]byte;
|
|
|
|
buf *bytes.Buffer;
|
2009-10-06 16:38:57 -06:00
|
|
|
wid int;
|
2009-12-06 13:03:52 -07:00
|
|
|
widPresent bool;
|
2009-10-06 16:38:57 -06:00
|
|
|
prec int;
|
2009-12-06 13:03:52 -07:00
|
|
|
precPresent bool;
|
2008-11-24 15:51:33 -07:00
|
|
|
// flags
|
2009-10-06 16:38:57 -06:00
|
|
|
minus bool;
|
|
|
|
plus bool;
|
|
|
|
sharp bool;
|
|
|
|
space bool;
|
|
|
|
zero bool;
|
2008-03-27 01:06:21 -06:00
|
|
|
}
|
|
|
|
|
2009-12-06 13:03:52 -07:00
|
|
|
func (f *Fmt) ClearFlags() {
|
2008-12-11 17:53:33 -07:00
|
|
|
f.wid = 0;
|
2009-12-06 13:03:52 -07:00
|
|
|
f.widPresent = false;
|
2008-12-11 17:53:33 -07:00
|
|
|
f.prec = 0;
|
2009-12-06 13:03:52 -07:00
|
|
|
f.precPresent = false;
|
2008-11-24 15:51:33 -07:00
|
|
|
f.minus = false;
|
|
|
|
f.plus = false;
|
|
|
|
f.sharp = false;
|
|
|
|
f.space = false;
|
|
|
|
f.zero = false;
|
2008-03-27 01:06:21 -06:00
|
|
|
}
|
|
|
|
|
2009-12-06 13:03:52 -07:00
|
|
|
func (f *Fmt) Init(buf *bytes.Buffer) {
|
|
|
|
f.buf = buf;
|
|
|
|
f.ClearFlags();
|
2008-03-27 01:06:21 -06:00
|
|
|
}
|
|
|
|
|
2009-12-06 13:03:52 -07:00
|
|
|
func (f *Fmt) Reset() { f.ClearFlags() }
|
2008-03-27 01:06:21 -06:00
|
|
|
|
2009-03-06 04:35:38 -07:00
|
|
|
// Wp sets the width and precision for formatting the next item.
|
2009-12-06 13:03:52 -07:00
|
|
|
func (f *Fmt) Wp(w, p int) {
|
|
|
|
f.widPresent = true;
|
2008-03-27 01:06:21 -06:00
|
|
|
f.wid = w;
|
2009-12-06 13:03:52 -07:00
|
|
|
f.precPresent = true;
|
2008-03-27 01:06:21 -06:00
|
|
|
f.prec = p;
|
|
|
|
}
|
|
|
|
|
2009-03-06 04:35:38 -07:00
|
|
|
// P sets the precision for formatting the next item.
|
2009-12-06 13:03:52 -07:00
|
|
|
func (f *Fmt) P(p int) {
|
|
|
|
f.precPresent = true;
|
2008-03-27 01:06:21 -06:00
|
|
|
f.prec = p;
|
|
|
|
}
|
|
|
|
|
2009-03-06 04:35:38 -07:00
|
|
|
// W sets the width for formatting the next item.
|
2009-12-06 13:03:52 -07:00
|
|
|
func (f *Fmt) W(x int) {
|
|
|
|
f.widPresent = true;
|
2008-03-27 01:06:21 -06:00
|
|
|
f.wid = x;
|
|
|
|
}
|
|
|
|
|
2009-12-06 13:03:52 -07:00
|
|
|
// Compute left and right padding widths (only one will be non-zero).
|
|
|
|
func (f *Fmt) computePadding(width int) (padding []byte, leftWidth, rightWidth int) {
|
|
|
|
left := !f.minus;
|
|
|
|
w := f.wid;
|
|
|
|
if w < 0 {
|
|
|
|
left = false;
|
|
|
|
w = -w;
|
|
|
|
}
|
|
|
|
w -= width;
|
|
|
|
if w > 0 {
|
2008-11-24 15:51:33 -07:00
|
|
|
if left && f.zero {
|
2009-12-06 13:03:52 -07:00
|
|
|
return padZeroBytes, w, 0
|
2008-11-24 15:51:33 -07:00
|
|
|
}
|
2009-12-06 13:03:52 -07:00
|
|
|
if left {
|
|
|
|
return padSpaceBytes, w, 0
|
|
|
|
} else {
|
|
|
|
// can't be zero padding on the right
|
|
|
|
return padSpaceBytes, 0, w
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
// Generate n bytes of padding.
|
|
|
|
func (f *Fmt) writePadding(n int, padding []byte) {
|
|
|
|
for n > 0 {
|
|
|
|
m := n;
|
|
|
|
if m > nByte {
|
|
|
|
m = nByte
|
2008-03-27 01:06:21 -06:00
|
|
|
}
|
2009-12-06 13:03:52 -07:00
|
|
|
f.buf.Write(padding[0:m]);
|
|
|
|
n -= m;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// Append b to f.buf, padded on left (w > 0) or right (w < 0 or f.minus)
|
|
|
|
func (f *Fmt) padBytes(b []byte) {
|
|
|
|
var padding []byte;
|
|
|
|
var left, right int;
|
|
|
|
if f.widPresent && f.wid != 0 {
|
|
|
|
padding, left, right = f.computePadding(len(b))
|
|
|
|
}
|
|
|
|
if left > 0 {
|
|
|
|
f.writePadding(left, padding)
|
|
|
|
}
|
|
|
|
f.buf.Write(b);
|
|
|
|
if right > 0 {
|
|
|
|
f.writePadding(right, padding)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// append s to buf, padded on left (w > 0) or right (w < 0 or f.minus)
|
|
|
|
func (f *Fmt) pad(s string) {
|
|
|
|
var padding []byte;
|
|
|
|
var left, right int;
|
|
|
|
if f.widPresent && f.wid != 0 {
|
|
|
|
padding, left, right = f.computePadding(len(s))
|
|
|
|
}
|
|
|
|
if left > 0 {
|
|
|
|
f.writePadding(left, padding)
|
|
|
|
}
|
|
|
|
f.buf.WriteString(s);
|
|
|
|
if right > 0 {
|
|
|
|
f.writePadding(right, padding)
|
2008-03-27 01:06:21 -06:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// 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.
|
2009-06-22 14:34:21 -06:00
|
|
|
func putint(buf []byte, base, val uint64, digits string) int {
|
2009-11-09 22:13:17 -07:00
|
|
|
i := len(buf) - 1;
|
2008-03-27 01:06:21 -06:00
|
|
|
for val >= base {
|
2008-07-08 15:20:04 -06:00
|
|
|
buf[i] = digits[val%base];
|
2008-03-27 01:06:21 -06:00
|
|
|
i--;
|
|
|
|
val /= base;
|
|
|
|
}
|
2008-07-08 15:20:04 -06:00
|
|
|
buf[i] = digits[val];
|
2009-11-09 22:13:17 -07:00
|
|
|
return i - 1;
|
2008-03-27 01:06:21 -06:00
|
|
|
}
|
|
|
|
|
2009-03-06 04:35:38 -07:00
|
|
|
// Fmt_boolean formats a boolean.
|
2009-12-06 13:03:52 -07:00
|
|
|
func (f *Fmt) Fmt_boolean(v bool) {
|
2009-03-06 04:35:38 -07:00
|
|
|
if v {
|
2009-11-09 13:07:39 -07:00
|
|
|
f.pad("true")
|
2008-06-24 13:54:26 -06:00
|
|
|
} else {
|
2009-11-09 13:07:39 -07:00
|
|
|
f.pad("false")
|
2008-06-24 13:54:26 -06:00
|
|
|
}
|
2009-12-06 13:03:52 -07:00
|
|
|
f.ClearFlags();
|
2008-06-24 13:54:26 -06:00
|
|
|
}
|
|
|
|
|
2008-03-27 01:06:21 -06:00
|
|
|
// integer; interprets prec but not wid.
|
2009-12-06 13:03:52 -07:00
|
|
|
func (f *Fmt) integer(a int64, base uint, is_signed bool, digits string) []byte {
|
|
|
|
var buf []byte = &f.intbuf;
|
2008-03-27 01:06:21 -06:00
|
|
|
negative := is_signed && a < 0;
|
|
|
|
if negative {
|
2009-11-09 13:07:39 -07:00
|
|
|
a = -a
|
2008-03-27 01:06:21 -06:00
|
|
|
}
|
2008-11-24 15:51:33 -07:00
|
|
|
|
|
|
|
// two ways to ask for extra leading zero digits: %.3d or %03d.
|
|
|
|
// apparently the first cancels the second.
|
|
|
|
prec := 0;
|
2009-12-06 13:03:52 -07:00
|
|
|
if f.precPresent {
|
2008-11-24 15:51:33 -07:00
|
|
|
prec = f.prec;
|
|
|
|
f.zero = false;
|
2009-12-06 13:03:52 -07:00
|
|
|
} else if f.zero && f.widPresent && !f.minus && f.wid > 0 {
|
2008-11-24 15:51:33 -07:00
|
|
|
prec = f.wid;
|
|
|
|
if negative || f.plus || f.space {
|
2009-11-09 13:07:39 -07:00
|
|
|
prec-- // leave room for sign
|
2008-03-27 01:06:21 -06:00
|
|
|
}
|
|
|
|
}
|
2008-11-24 15:51:33 -07:00
|
|
|
|
2009-12-06 13:03:52 -07:00
|
|
|
i := putint(buf, uint64(base), uint64(a), digits);
|
2009-01-15 16:40:27 -07:00
|
|
|
for i > 0 && prec > (nByte-1-i) {
|
2008-11-24 15:51:33 -07:00
|
|
|
buf[i] = '0';
|
|
|
|
i--;
|
|
|
|
}
|
|
|
|
|
2009-05-15 16:18:09 -06:00
|
|
|
if f.sharp {
|
|
|
|
switch base {
|
|
|
|
case 8:
|
|
|
|
if buf[i+1] != '0' {
|
|
|
|
buf[i] = '0';
|
|
|
|
i--;
|
|
|
|
}
|
|
|
|
case 16:
|
2009-11-09 22:13:17 -07:00
|
|
|
buf[i] = 'x' + digits[10] - 'a';
|
2009-05-15 16:18:09 -06:00
|
|
|
i--;
|
|
|
|
buf[i] = '0';
|
|
|
|
i--;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2008-03-27 01:06:21 -06:00
|
|
|
if negative {
|
|
|
|
buf[i] = '-';
|
|
|
|
i--;
|
2008-11-24 15:51:33 -07:00
|
|
|
} else if f.plus {
|
|
|
|
buf[i] = '+';
|
|
|
|
i--;
|
|
|
|
} else if f.space {
|
|
|
|
buf[i] = ' ';
|
|
|
|
i--;
|
2008-03-27 01:06:21 -06:00
|
|
|
}
|
2009-12-06 13:03:52 -07:00
|
|
|
return buf[i+1 : nByte];
|
2008-03-27 01:06:21 -06:00
|
|
|
}
|
|
|
|
|
2009-03-06 04:35:38 -07:00
|
|
|
// Fmt_d64 formats an int64 in decimal.
|
2009-12-06 13:03:52 -07:00
|
|
|
func (f *Fmt) Fmt_d64(v int64) {
|
|
|
|
f.padBytes(f.integer(v, 10, true, ldigits));
|
|
|
|
f.ClearFlags();
|
2008-03-27 01:06:21 -06:00
|
|
|
}
|
2008-11-17 13:34:03 -07:00
|
|
|
|
2009-03-06 04:35:38 -07:00
|
|
|
// Fmt_d32 formats an int32 in decimal.
|
2009-12-06 13:03:52 -07:00
|
|
|
func (f *Fmt) Fmt_d32(v int32) { f.Fmt_d64(int64(v)) }
|
2008-03-27 01:06:21 -06:00
|
|
|
|
2009-03-06 04:35:38 -07:00
|
|
|
// Fmt_d formats an int in decimal.
|
2009-12-06 13:03:52 -07:00
|
|
|
func (f *Fmt) Fmt_d(v int) { f.Fmt_d64(int64(v)) }
|
2008-03-27 01:06:21 -06:00
|
|
|
|
2009-03-06 04:35:38 -07:00
|
|
|
// Fmt_ud64 formats a uint64 in decimal.
|
|
|
|
func (f *Fmt) Fmt_ud64(v uint64) *Fmt {
|
2009-12-06 13:03:52 -07:00
|
|
|
f.padBytes(f.integer(int64(v), 10, false, ldigits));
|
|
|
|
f.ClearFlags();
|
2008-03-27 01:06:21 -06:00
|
|
|
return f;
|
|
|
|
}
|
|
|
|
|
2009-03-06 04:35:38 -07:00
|
|
|
// Fmt_ud32 formats a uint32 in decimal.
|
2009-12-06 13:03:52 -07:00
|
|
|
func (f *Fmt) Fmt_ud32(v uint32) { f.Fmt_ud64(uint64(v)) }
|
2008-03-27 01:06:21 -06:00
|
|
|
|
2009-03-06 04:35:38 -07:00
|
|
|
// Fmt_ud formats a uint in decimal.
|
2009-12-06 13:03:52 -07:00
|
|
|
func (f *Fmt) Fmt_ud(v uint) { f.Fmt_ud64(uint64(v)) }
|
2008-03-27 01:06:21 -06:00
|
|
|
|
2009-03-06 04:35:38 -07:00
|
|
|
// Fmt_x64 formats an int64 in hexadecimal.
|
2009-12-06 13:03:52 -07:00
|
|
|
func (f *Fmt) Fmt_x64(v int64) {
|
|
|
|
f.padBytes(f.integer(v, 16, true, ldigits));
|
|
|
|
f.ClearFlags();
|
2008-03-27 01:06:21 -06:00
|
|
|
}
|
|
|
|
|
2009-03-06 04:35:38 -07:00
|
|
|
// Fmt_x32 formats an int32 in hexadecimal.
|
2009-12-06 13:03:52 -07:00
|
|
|
func (f *Fmt) Fmt_x32(v int32) { f.Fmt_x64(int64(v)) }
|
2008-10-29 15:28:19 -06:00
|
|
|
|
2009-03-06 04:35:38 -07:00
|
|
|
// Fmt_x formats an int in hexadecimal.
|
2009-12-06 13:03:52 -07:00
|
|
|
func (f *Fmt) Fmt_x(v int) { f.Fmt_x64(int64(v)) }
|
2008-10-29 15:28:19 -06:00
|
|
|
|
2009-03-06 04:35:38 -07:00
|
|
|
// Fmt_ux64 formats a uint64 in hexadecimal.
|
2009-12-06 13:03:52 -07:00
|
|
|
func (f *Fmt) Fmt_ux64(v uint64) {
|
|
|
|
f.padBytes(f.integer(int64(v), 16, false, ldigits));
|
|
|
|
f.ClearFlags();
|
2008-03-27 01:06:21 -06:00
|
|
|
}
|
|
|
|
|
2009-03-06 04:35:38 -07:00
|
|
|
// Fmt_ux32 formats a uint32 in hexadecimal.
|
2009-12-06 13:03:52 -07:00
|
|
|
func (f *Fmt) Fmt_ux32(v uint32) { f.Fmt_ux64(uint64(v)) }
|
2008-03-27 01:06:21 -06:00
|
|
|
|
2009-03-06 04:35:38 -07:00
|
|
|
// Fmt_ux formats a uint in hexadecimal.
|
2009-12-06 13:03:52 -07:00
|
|
|
func (f *Fmt) Fmt_ux(v uint) { f.Fmt_ux64(uint64(v)) }
|
2008-03-27 01:06:21 -06:00
|
|
|
|
2009-03-06 04:35:38 -07:00
|
|
|
// Fmt_X64 formats an int64 in upper case hexadecimal.
|
2009-12-06 13:03:52 -07:00
|
|
|
func (f *Fmt) Fmt_X64(v int64) {
|
|
|
|
f.padBytes(f.integer(v, 16, true, udigits));
|
|
|
|
f.ClearFlags();
|
2008-03-27 01:06:21 -06:00
|
|
|
}
|
|
|
|
|
2009-03-06 04:35:38 -07:00
|
|
|
// Fmt_X32 formats an int32 in upper case hexadecimal.
|
2009-12-06 13:03:52 -07:00
|
|
|
func (f *Fmt) Fmt_X32(v int32) { f.Fmt_X64(int64(v)) }
|
2008-10-29 15:28:19 -06:00
|
|
|
|
2009-03-06 04:35:38 -07:00
|
|
|
// Fmt_X formats an int in upper case hexadecimal.
|
2009-12-06 13:03:52 -07:00
|
|
|
func (f *Fmt) Fmt_X(v int) { f.Fmt_X64(int64(v)) }
|
2008-10-29 15:28:19 -06:00
|
|
|
|
2009-03-06 04:35:38 -07:00
|
|
|
// Fmt_uX64 formats a uint64 in upper case hexadecimal.
|
2009-12-06 13:03:52 -07:00
|
|
|
func (f *Fmt) Fmt_uX64(v uint64) {
|
|
|
|
f.padBytes(f.integer(int64(v), 16, false, udigits));
|
|
|
|
f.ClearFlags();
|
2008-03-27 01:06:21 -06:00
|
|
|
}
|
|
|
|
|
2009-03-06 04:35:38 -07:00
|
|
|
// Fmt_uX32 formats a uint32 in upper case hexadecimal.
|
2009-12-06 13:03:52 -07:00
|
|
|
func (f *Fmt) Fmt_uX32(v uint32) { f.Fmt_uX64(uint64(v)) }
|
2008-03-27 01:06:21 -06:00
|
|
|
|
2009-03-06 04:35:38 -07:00
|
|
|
// Fmt_uX formats a uint in upper case hexadecimal.
|
2009-12-06 13:03:52 -07:00
|
|
|
func (f *Fmt) Fmt_uX(v uint) { f.Fmt_uX64(uint64(v)) }
|
2008-03-27 01:06:21 -06:00
|
|
|
|
2009-03-06 04:35:38 -07:00
|
|
|
// Fmt_o64 formats an int64 in octal.
|
2009-12-06 13:03:52 -07:00
|
|
|
func (f *Fmt) Fmt_o64(v int64) {
|
|
|
|
f.padBytes(f.integer(v, 8, true, ldigits));
|
|
|
|
f.ClearFlags();
|
2008-03-27 01:06:21 -06:00
|
|
|
}
|
|
|
|
|
2009-03-06 04:35:38 -07:00
|
|
|
// Fmt_o32 formats an int32 in octal.
|
2009-12-06 13:03:52 -07:00
|
|
|
func (f *Fmt) Fmt_o32(v int32) { f.Fmt_o64(int64(v)) }
|
2008-10-29 15:28:19 -06:00
|
|
|
|
2009-03-06 04:35:38 -07:00
|
|
|
// Fmt_o formats an int in octal.
|
2009-12-06 13:03:52 -07:00
|
|
|
func (f *Fmt) Fmt_o(v int) { f.Fmt_o64(int64(v)) }
|
2008-10-29 15:28:19 -06:00
|
|
|
|
2009-03-06 04:35:38 -07:00
|
|
|
// Fmt_uo64 formats a uint64 in octal.
|
2009-12-06 13:03:52 -07:00
|
|
|
func (f *Fmt) Fmt_uo64(v uint64) {
|
|
|
|
f.padBytes(f.integer(int64(v), 8, false, ldigits));
|
|
|
|
f.ClearFlags();
|
2008-03-27 01:06:21 -06:00
|
|
|
}
|
|
|
|
|
2009-03-06 04:35:38 -07:00
|
|
|
// Fmt_uo32 formats a uint32 in octal.
|
2009-12-06 13:03:52 -07:00
|
|
|
func (f *Fmt) Fmt_uo32(v uint32) { f.Fmt_uo64(uint64(v)) }
|
2008-03-27 01:06:21 -06:00
|
|
|
|
2009-03-06 04:35:38 -07:00
|
|
|
// Fmt_uo formats a uint in octal.
|
2009-12-06 13:03:52 -07:00
|
|
|
func (f *Fmt) Fmt_uo(v uint) { f.Fmt_uo64(uint64(v)) }
|
2008-03-27 01:06:21 -06:00
|
|
|
|
2009-03-06 04:35:38 -07:00
|
|
|
// Fmt_b64 formats a uint64 in binary.
|
2009-12-06 13:03:52 -07:00
|
|
|
func (f *Fmt) Fmt_b64(v uint64) {
|
|
|
|
f.padBytes(f.integer(int64(v), 2, false, ldigits));
|
|
|
|
f.ClearFlags();
|
2008-03-27 01:06:21 -06:00
|
|
|
}
|
|
|
|
|
2009-03-06 04:35:38 -07:00
|
|
|
// Fmt_b32 formats a uint32 in binary.
|
2009-12-06 13:03:52 -07:00
|
|
|
func (f *Fmt) Fmt_b32(v uint32) { f.Fmt_b64(uint64(v)) }
|
2008-10-29 15:28:19 -06:00
|
|
|
|
2009-03-06 04:35:38 -07:00
|
|
|
// Fmt_b formats a uint in binary.
|
2009-12-06 13:03:52 -07:00
|
|
|
func (f *Fmt) Fmt_b(v uint) { f.Fmt_b64(uint64(v)) }
|
2008-10-29 15:28:19 -06:00
|
|
|
|
2009-03-06 04:35:38 -07:00
|
|
|
// Fmt_c formats a Unicode character.
|
2009-12-06 13:03:52 -07:00
|
|
|
func (f *Fmt) Fmt_c(v int) {
|
2009-03-06 04:35:38 -07:00
|
|
|
f.pad(string(v));
|
2009-12-06 13:03:52 -07:00
|
|
|
f.ClearFlags();
|
2008-03-27 01:06:21 -06:00
|
|
|
}
|
|
|
|
|
2009-03-06 04:35:38 -07:00
|
|
|
// Fmt_s formats a string.
|
2009-12-06 13:03:52 -07:00
|
|
|
func (f *Fmt) Fmt_s(s string) {
|
|
|
|
if f.precPresent {
|
2008-03-27 01:06:21 -06:00
|
|
|
if f.prec < len(s) {
|
2009-11-09 22:13:17 -07:00
|
|
|
s = s[0:f.prec]
|
2008-03-27 01:06:21 -06:00
|
|
|
}
|
|
|
|
}
|
|
|
|
f.pad(s);
|
2009-12-06 13:03:52 -07:00
|
|
|
f.ClearFlags();
|
2008-03-27 01:06:21 -06:00
|
|
|
}
|
|
|
|
|
2009-03-06 04:35:38 -07:00
|
|
|
// Fmt_sx formats a string as a hexadecimal encoding of its bytes.
|
2009-12-06 13:03:52 -07:00
|
|
|
func (f *Fmt) Fmt_sx(s string) {
|
2008-11-24 15:51:33 -07:00
|
|
|
t := "";
|
|
|
|
for i := 0; i < len(s); i++ {
|
2008-11-25 10:23:13 -07:00
|
|
|
if i > 0 && f.space {
|
2009-11-09 13:07:39 -07:00
|
|
|
t += " "
|
2008-11-25 10:23:13 -07:00
|
|
|
}
|
2008-11-24 15:51:33 -07:00
|
|
|
v := s[i];
|
|
|
|
t += string(ldigits[v>>4]);
|
|
|
|
t += string(ldigits[v&0xF]);
|
|
|
|
}
|
|
|
|
f.pad(t);
|
2009-12-06 13:03:52 -07:00
|
|
|
f.ClearFlags();
|
2008-11-24 15:51:33 -07:00
|
|
|
}
|
|
|
|
|
2009-03-06 04:35:38 -07:00
|
|
|
// Fmt_sX formats a string as an uppercase hexadecimal encoding of its bytes.
|
2009-12-06 13:03:52 -07:00
|
|
|
func (f *Fmt) Fmt_sX(s string) {
|
2008-11-24 15:51:33 -07:00
|
|
|
t := "";
|
|
|
|
for i := 0; i < len(s); i++ {
|
|
|
|
v := s[i];
|
|
|
|
t += string(udigits[v>>4]);
|
|
|
|
t += string(udigits[v&0xF]);
|
|
|
|
}
|
|
|
|
f.pad(t);
|
2009-12-06 13:03:52 -07:00
|
|
|
f.ClearFlags();
|
2008-11-24 15:51:33 -07:00
|
|
|
}
|
|
|
|
|
2009-03-06 04:35:38 -07:00
|
|
|
// Fmt_q formats a string as a double-quoted, escaped Go string constant.
|
2009-12-06 13:03:52 -07:00
|
|
|
func (f *Fmt) Fmt_q(s string) {
|
2008-11-24 15:51:33 -07:00
|
|
|
var quoted string;
|
|
|
|
if f.sharp && strconv.CanBackquote(s) {
|
2009-11-09 22:13:17 -07:00
|
|
|
quoted = "`" + s + "`"
|
2008-11-24 15:51:33 -07:00
|
|
|
} else {
|
2009-11-09 13:07:39 -07:00
|
|
|
quoted = strconv.Quote(s)
|
2008-11-24 15:51:33 -07:00
|
|
|
}
|
|
|
|
f.pad(quoted);
|
2009-12-06 13:03:52 -07:00
|
|
|
f.ClearFlags();
|
2008-11-24 15:51:33 -07:00
|
|
|
}
|
|
|
|
|
2008-11-17 13:34:03 -07:00
|
|
|
// floating-point
|
2008-03-27 01:06:21 -06:00
|
|
|
|
2009-01-16 14:29:43 -07:00
|
|
|
func doPrec(f *Fmt, def int) int {
|
2009-12-06 13:03:52 -07:00
|
|
|
if f.precPresent {
|
2009-11-09 13:07:39 -07:00
|
|
|
return f.prec
|
2008-08-11 16:04:54 -06:00
|
|
|
}
|
2008-11-17 13:34:03 -07:00
|
|
|
return def;
|
2008-08-11 16:04:54 -06:00
|
|
|
}
|
|
|
|
|
2009-12-06 13:03:52 -07:00
|
|
|
func fmtString(f *Fmt, s string) {
|
2008-03-27 01:06:21 -06:00
|
|
|
f.pad(s);
|
2009-12-06 13:03:52 -07:00
|
|
|
f.ClearFlags();
|
2008-03-27 01:06:21 -06:00
|
|
|
}
|
|
|
|
|
2009-11-20 12:04:51 -07:00
|
|
|
// Add a plus sign or space to the string if missing and required.
|
2009-12-06 13:03:52 -07:00
|
|
|
func (f *Fmt) plusSpace(s string) {
|
2009-11-20 12:04:51 -07:00
|
|
|
if s[0] != '-' {
|
|
|
|
if f.plus {
|
|
|
|
s = "+" + s
|
|
|
|
} else if f.space {
|
|
|
|
s = " " + s
|
|
|
|
}
|
|
|
|
}
|
2009-12-06 13:03:52 -07:00
|
|
|
fmtString(f, s);
|
2009-11-20 12:04:51 -07:00
|
|
|
}
|
|
|
|
|
2009-03-06 04:35:38 -07:00
|
|
|
// Fmt_e64 formats a float64 in the form -1.23e+12.
|
2009-12-06 13:03:52 -07:00
|
|
|
func (f *Fmt) Fmt_e64(v float64) { f.plusSpace(strconv.Ftoa64(v, 'e', doPrec(f, 6))) }
|
2008-11-17 13:34:03 -07:00
|
|
|
|
2009-08-31 17:38:30 -06:00
|
|
|
// Fmt_E64 formats a float64 in the form -1.23E+12.
|
2009-12-06 13:03:52 -07:00
|
|
|
func (f *Fmt) Fmt_E64(v float64) { f.plusSpace(strconv.Ftoa64(v, 'E', doPrec(f, 6))) }
|
2009-08-31 17:38:30 -06:00
|
|
|
|
2009-03-06 04:35:38 -07:00
|
|
|
// Fmt_f64 formats a float64 in the form -1.23.
|
2009-12-06 13:03:52 -07:00
|
|
|
func (f *Fmt) Fmt_f64(v float64) { f.plusSpace(strconv.Ftoa64(v, 'f', doPrec(f, 6))) }
|
2008-03-27 01:06:21 -06:00
|
|
|
|
2009-03-06 04:35:38 -07:00
|
|
|
// Fmt_g64 formats a float64 in the 'f' or 'e' form according to size.
|
2009-12-06 13:03:52 -07:00
|
|
|
func (f *Fmt) Fmt_g64(v float64) { f.plusSpace(strconv.Ftoa64(v, 'g', doPrec(f, -1))) }
|
2008-03-27 01:06:21 -06:00
|
|
|
|
2009-08-31 17:38:30 -06:00
|
|
|
// Fmt_g64 formats a float64 in the 'f' or 'E' form according to size.
|
2009-12-06 13:03:52 -07:00
|
|
|
func (f *Fmt) Fmt_G64(v float64) { f.plusSpace(strconv.Ftoa64(v, 'G', doPrec(f, -1))) }
|
2009-08-31 17:38:30 -06:00
|
|
|
|
2009-03-06 04:35:38 -07:00
|
|
|
// Fmt_fb64 formats a float64 in the form -123p3 (exponent is power of 2).
|
2009-12-06 13:03:52 -07:00
|
|
|
func (f *Fmt) Fmt_fb64(v float64) { f.plusSpace(strconv.Ftoa64(v, 'b', 0)) }
|
2008-10-29 15:28:19 -06:00
|
|
|
|
2008-11-17 13:34:03 -07:00
|
|
|
// float32
|
|
|
|
// cannot defer to float64 versions
|
|
|
|
// because it will get rounding wrong in corner cases.
|
2009-03-06 04:35:38 -07:00
|
|
|
|
|
|
|
// Fmt_e32 formats a float32 in the form -1.23e+12.
|
2009-12-06 13:03:52 -07:00
|
|
|
func (f *Fmt) Fmt_e32(v float32) { f.plusSpace(strconv.Ftoa32(v, 'e', doPrec(f, 6))) }
|
2008-11-17 13:34:03 -07:00
|
|
|
|
2009-08-31 17:38:30 -06:00
|
|
|
// Fmt_E32 formats a float32 in the form -1.23E+12.
|
2009-12-06 13:03:52 -07:00
|
|
|
func (f *Fmt) Fmt_E32(v float32) { f.plusSpace(strconv.Ftoa32(v, 'E', doPrec(f, 6))) }
|
2009-08-31 17:38:30 -06:00
|
|
|
|
2009-03-06 04:35:38 -07:00
|
|
|
// Fmt_f32 formats a float32 in the form -1.23.
|
2009-12-06 13:03:52 -07:00
|
|
|
func (f *Fmt) Fmt_f32(v float32) { f.plusSpace(strconv.Ftoa32(v, 'f', doPrec(f, 6))) }
|
2008-11-17 13:34:03 -07:00
|
|
|
|
2009-03-06 04:35:38 -07:00
|
|
|
// Fmt_g32 formats a float32 in the 'f' or 'e' form according to size.
|
2009-12-06 13:03:52 -07:00
|
|
|
func (f *Fmt) Fmt_g32(v float32) { f.plusSpace(strconv.Ftoa32(v, 'g', doPrec(f, -1))) }
|
2008-11-17 13:34:03 -07:00
|
|
|
|
2009-08-31 17:38:30 -06:00
|
|
|
// Fmt_G32 formats a float32 in the 'f' or 'E' form according to size.
|
2009-12-06 13:03:52 -07:00
|
|
|
func (f *Fmt) Fmt_G32(v float32) { f.plusSpace(strconv.Ftoa32(v, 'G', doPrec(f, -1))) }
|
2009-08-31 17:38:30 -06:00
|
|
|
|
2009-03-06 04:35:38 -07:00
|
|
|
// Fmt_fb32 formats a float32 in the form -123p3 (exponent is power of 2).
|
2009-12-06 13:03:52 -07:00
|
|
|
func (f *Fmt) Fmt_fb32(v float32) { fmtString(f, strconv.Ftoa32(v, 'b', 0)) }
|
2008-03-27 01:06:21 -06:00
|
|
|
|
|
|
|
// float
|
2009-12-06 13:03:52 -07:00
|
|
|
func (x *Fmt) f(a float) {
|
2009-01-15 17:16:42 -07:00
|
|
|
if strconv.FloatSize == 32 {
|
2009-12-06 13:03:52 -07:00
|
|
|
x.Fmt_f32(float32(a))
|
|
|
|
} else {
|
|
|
|
x.Fmt_f64(float64(a))
|
2008-11-17 13:34:03 -07:00
|
|
|
}
|
2008-10-29 15:28:19 -06:00
|
|
|
}
|
|
|
|
|
2009-12-06 13:03:52 -07:00
|
|
|
func (x *Fmt) e(a float) {
|
2009-01-15 17:16:42 -07:00
|
|
|
if strconv.FloatSize == 32 {
|
2009-12-06 13:03:52 -07:00
|
|
|
x.Fmt_e32(float32(a))
|
|
|
|
} else {
|
|
|
|
x.Fmt_e64(float64(a))
|
2008-11-17 13:34:03 -07:00
|
|
|
}
|
2008-03-27 01:06:21 -06:00
|
|
|
}
|
|
|
|
|
2009-12-06 13:03:52 -07:00
|
|
|
func (x *Fmt) g(a float) {
|
2009-01-15 17:16:42 -07:00
|
|
|
if strconv.FloatSize == 32 {
|
2009-12-06 13:03:52 -07:00
|
|
|
x.Fmt_g32(float32(a))
|
|
|
|
} else {
|
|
|
|
x.Fmt_g64(float64(a))
|
2008-11-17 13:34:03 -07:00
|
|
|
}
|
2008-10-29 15:28:19 -06:00
|
|
|
}
|
|
|
|
|
2009-12-06 13:03:52 -07:00
|
|
|
func (x *Fmt) fb(a float) {
|
2009-01-15 17:16:42 -07:00
|
|
|
if strconv.FloatSize == 32 {
|
2009-12-06 13:03:52 -07:00
|
|
|
x.Fmt_fb32(float32(a))
|
|
|
|
} else {
|
|
|
|
x.Fmt_fb64(float64(a))
|
2008-11-17 13:34:03 -07:00
|
|
|
}
|
2008-03-27 01:06:21 -06:00
|
|
|
}
|