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 (
|
|
|
|
"strconv";
|
|
|
|
)
|
2008-11-17 13:34:03 -07:00
|
|
|
|
2008-03-27 01:06:21 -06:00
|
|
|
/*
|
2008-10-26 09:27:50 -06:00
|
|
|
Raw formatter. See print.go for a more palatable interface.
|
|
|
|
|
2008-03-27 01:06:21 -06:00
|
|
|
f := fmt.New();
|
2009-01-15 16:40:27 -07:00
|
|
|
print f.Fmt_d(1234).Fmt_s("\n").Str(); // create string, print it
|
2009-01-23 16:57:52 -07:00
|
|
|
f.Fmt_d(-1234).Fmt_s("\n").Put(); // print string
|
2009-01-15 16:40:27 -07:00
|
|
|
f.Fmt_ud(1<<63).Putnl(); // print string with automatic newline
|
2008-03-27 01:06:21 -06:00
|
|
|
*/
|
|
|
|
|
2009-01-15 16:40:27 -07:00
|
|
|
const nByte = 64;
|
|
|
|
const nPows10 = 160;
|
2008-07-08 15:20:04 -06:00
|
|
|
|
2008-08-09 15:50:29 -06:00
|
|
|
var ldigits string = "0123456789abcdef" // var not const because we take its address
|
|
|
|
var udigits string = "0123456789ABCDEF"
|
2009-01-15 16:40:27 -07:00
|
|
|
var pows10 [nPows10] float64;
|
2008-03-27 01:06:21 -06:00
|
|
|
|
2008-07-19 15:52:01 -06:00
|
|
|
func init() {
|
|
|
|
pows10[0] = 1.0e0;
|
|
|
|
pows10[1] = 1.0e1;
|
2009-01-15 16:40:27 -07:00
|
|
|
for i:=2; i<nPows10; i++ {
|
2008-07-19 15:52:01 -06:00
|
|
|
m := i/2;
|
|
|
|
pows10[i] = pows10[m] * pows10[i-m];
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2009-01-20 15:40:40 -07:00
|
|
|
type Fmt struct {
|
2008-03-27 01:06:21 -06:00
|
|
|
buf string;
|
|
|
|
wid int;
|
|
|
|
wid_present bool;
|
|
|
|
prec int;
|
|
|
|
prec_present bool;
|
2008-11-24 15:51:33 -07:00
|
|
|
// flags
|
|
|
|
minus bool;
|
|
|
|
plus bool;
|
|
|
|
sharp bool;
|
|
|
|
space bool;
|
|
|
|
zero bool;
|
2008-03-27 01:06:21 -06:00
|
|
|
}
|
|
|
|
|
|
|
|
func (f *Fmt) clearflags() {
|
2008-12-11 17:53:33 -07:00
|
|
|
f.wid = 0;
|
2008-03-27 01:06:21 -06:00
|
|
|
f.wid_present = false;
|
2008-12-11 17:53:33 -07:00
|
|
|
f.prec = 0;
|
2008-03-27 01:06:21 -06:00
|
|
|
f.prec_present = 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
|
|
|
}
|
|
|
|
|
|
|
|
func (f *Fmt) clearbuf() {
|
|
|
|
f.buf = "";
|
|
|
|
}
|
|
|
|
|
|
|
|
func (f *Fmt) init() {
|
|
|
|
f.clearbuf();
|
|
|
|
f.clearflags();
|
|
|
|
}
|
|
|
|
|
2009-01-20 15:40:40 -07:00
|
|
|
func New() *Fmt {
|
2009-01-06 16:19:02 -07:00
|
|
|
f := new(Fmt);
|
2008-03-27 01:06:21 -06:00
|
|
|
f.init();
|
|
|
|
return f;
|
|
|
|
}
|
|
|
|
|
2009-01-15 16:40:27 -07:00
|
|
|
func (f *Fmt) Str() string {
|
2008-03-27 01:06:21 -06:00
|
|
|
s := f.buf;
|
|
|
|
f.clearbuf();
|
|
|
|
f.clearflags();
|
|
|
|
f.buf = "";
|
|
|
|
return s;
|
|
|
|
}
|
|
|
|
|
2009-01-15 16:40:27 -07:00
|
|
|
func (f *Fmt) Put() {
|
2008-08-11 22:37:46 -06:00
|
|
|
print(f.buf);
|
2008-03-27 01:06:21 -06:00
|
|
|
f.clearbuf();
|
|
|
|
f.clearflags();
|
|
|
|
}
|
|
|
|
|
2009-01-15 16:40:27 -07:00
|
|
|
func (f *Fmt) Putnl() {
|
2008-08-11 22:37:46 -06:00
|
|
|
print(f.buf, "\n");
|
2008-03-27 01:06:21 -06:00
|
|
|
f.clearbuf();
|
|
|
|
f.clearflags();
|
|
|
|
}
|
|
|
|
|
2009-01-15 16:40:27 -07:00
|
|
|
func (f *Fmt) Wp(w, p int) *Fmt {
|
2008-03-27 01:06:21 -06:00
|
|
|
f.wid_present = true;
|
|
|
|
f.wid = w;
|
|
|
|
f.prec_present = true;
|
|
|
|
f.prec = p;
|
|
|
|
return f;
|
|
|
|
}
|
|
|
|
|
2009-01-15 16:40:27 -07:00
|
|
|
func (f *Fmt) P(p int) *Fmt {
|
2008-03-27 01:06:21 -06:00
|
|
|
f.prec_present = true;
|
|
|
|
f.prec = p;
|
|
|
|
return f;
|
|
|
|
}
|
|
|
|
|
2009-01-15 16:40:27 -07:00
|
|
|
func (f *Fmt) W(x int) *Fmt {
|
2008-03-27 01:06:21 -06:00
|
|
|
f.wid_present = true;
|
|
|
|
f.wid = x;
|
|
|
|
return f;
|
|
|
|
}
|
|
|
|
|
2008-11-24 15:51:33 -07:00
|
|
|
// append s to buf, padded on left (w > 0) or right (w < 0 or f.minus)
|
2008-03-27 01:06:21 -06:00
|
|
|
// padding is in bytes, not characters (agrees with ANSIC C, not Plan 9 C)
|
|
|
|
func (f *Fmt) pad(s string) {
|
|
|
|
if f.wid_present && f.wid != 0 {
|
2008-11-24 15:51:33 -07:00
|
|
|
left := !f.minus;
|
2008-03-27 01:06:21 -06:00
|
|
|
w := f.wid;
|
|
|
|
if w < 0 {
|
|
|
|
left = false;
|
|
|
|
w = -w;
|
|
|
|
}
|
|
|
|
w -= len(s);
|
2008-11-24 15:51:33 -07:00
|
|
|
padchar := byte(' ');
|
|
|
|
if left && f.zero {
|
|
|
|
padchar = '0';
|
|
|
|
}
|
2008-03-27 01:06:21 -06:00
|
|
|
if w > 0 {
|
2009-01-15 16:40:27 -07:00
|
|
|
if w > nByte {
|
|
|
|
w = nByte;
|
2008-03-27 01:06:21 -06:00
|
|
|
}
|
2009-01-06 16:19:02 -07:00
|
|
|
buf := make([]byte, w);
|
2008-03-27 01:06:21 -06:00
|
|
|
for i := 0; i < w; i++ {
|
2008-11-24 15:51:33 -07:00
|
|
|
buf[i] = padchar;
|
2008-03-27 01:06:21 -06:00
|
|
|
}
|
|
|
|
if left {
|
2008-09-04 11:39:04 -06:00
|
|
|
s = string(buf) + s;
|
2008-03-27 01:06:21 -06:00
|
|
|
} else {
|
2008-09-04 11:39:04 -06:00
|
|
|
s = s + string(buf);
|
2008-03-27 01:06:21 -06:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2008-06-27 12:36:40 -06:00
|
|
|
f.buf += s;
|
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-01-15 16:40:27 -07:00
|
|
|
func putint(buf *[nByte]byte, i int, base, val uint64, digits *string) int {
|
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];
|
2008-03-27 01:06:21 -06:00
|
|
|
return i-1;
|
|
|
|
}
|
|
|
|
|
2008-06-24 13:54:26 -06:00
|
|
|
// boolean
|
2009-01-15 16:40:27 -07:00
|
|
|
func (f *Fmt) Fmt_boolean(a bool) *Fmt {
|
2008-06-24 13:54:26 -06:00
|
|
|
if a {
|
|
|
|
f.pad("true");
|
|
|
|
} else {
|
|
|
|
f.pad("false");
|
|
|
|
}
|
|
|
|
f.clearflags();
|
|
|
|
return f;
|
|
|
|
}
|
|
|
|
|
2008-03-27 01:06:21 -06:00
|
|
|
// integer; interprets prec but not wid.
|
2008-04-02 22:32:39 -06:00
|
|
|
func (f *Fmt) integer(a int64, base uint, is_signed bool, digits *string) string {
|
2009-01-15 16:40:27 -07:00
|
|
|
var buf [nByte]byte;
|
2008-03-27 01:06:21 -06:00
|
|
|
negative := is_signed && a < 0;
|
|
|
|
if negative {
|
|
|
|
a = -a;
|
|
|
|
}
|
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;
|
2008-03-27 01:06:21 -06:00
|
|
|
if f.prec_present {
|
2008-11-24 15:51:33 -07:00
|
|
|
prec = f.prec;
|
|
|
|
f.zero = false;
|
|
|
|
} else if f.zero && f.wid_present && !f.minus && f.wid > 0{
|
|
|
|
prec = f.wid;
|
|
|
|
if negative || f.plus || f.space {
|
|
|
|
prec--; // leave room for sign
|
2008-03-27 01:06:21 -06:00
|
|
|
}
|
|
|
|
}
|
2008-11-24 15:51:33 -07:00
|
|
|
|
2009-01-15 16:40:27 -07:00
|
|
|
i := putint(&buf, nByte-1, uint64(base), uint64(a), digits);
|
|
|
|
for i > 0 && prec > (nByte-1-i) {
|
2008-11-24 15:51:33 -07:00
|
|
|
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-01-15 16:40:27 -07:00
|
|
|
return string(buf)[i+1:nByte];
|
2008-03-27 01:06:21 -06:00
|
|
|
}
|
|
|
|
|
|
|
|
// decimal
|
2009-01-15 16:40:27 -07:00
|
|
|
func (f *Fmt) Fmt_d64(a int64) *Fmt {
|
2008-10-29 15:28:19 -06:00
|
|
|
f.pad(f.integer(a, 10, true, &ldigits));
|
2008-03-27 01:06:21 -06:00
|
|
|
f.clearflags();
|
|
|
|
return f;
|
|
|
|
}
|
2008-11-17 13:34:03 -07:00
|
|
|
|
2009-01-15 16:40:27 -07:00
|
|
|
func (f *Fmt) Fmt_d32(a int32) *Fmt {
|
|
|
|
return f.Fmt_d64(int64(a));
|
2008-10-29 15:28:19 -06:00
|
|
|
}
|
2008-03-27 01:06:21 -06:00
|
|
|
|
2009-01-15 16:40:27 -07:00
|
|
|
func (f *Fmt) Fmt_d(a int) *Fmt {
|
|
|
|
return f.Fmt_d64(int64(a));
|
2008-03-27 01:06:21 -06:00
|
|
|
}
|
|
|
|
|
2009-01-15 16:40:27 -07:00
|
|
|
// unsigned Fmt_decimal
|
|
|
|
func (f *Fmt) Fmt_ud64(a uint64) *Fmt {
|
2008-10-29 15:28:19 -06:00
|
|
|
f.pad(f.integer(int64(a), 10, false, &ldigits));
|
2008-03-27 01:06:21 -06:00
|
|
|
f.clearflags();
|
|
|
|
return f;
|
|
|
|
}
|
|
|
|
|
2009-01-15 16:40:27 -07:00
|
|
|
func (f *Fmt) Fmt_ud32(a uint32) *Fmt {
|
|
|
|
return f.Fmt_ud64(uint64(a));
|
2008-03-27 01:06:21 -06:00
|
|
|
}
|
|
|
|
|
2009-01-15 16:40:27 -07:00
|
|
|
func (f *Fmt) Fmt_ud(a uint) *Fmt {
|
|
|
|
return f.Fmt_ud64(uint64(a));
|
2008-03-27 01:06:21 -06:00
|
|
|
}
|
|
|
|
|
2008-10-29 15:28:19 -06:00
|
|
|
// hexdecimal
|
2009-01-15 16:40:27 -07:00
|
|
|
func (f *Fmt) Fmt_x64(a int64) *Fmt {
|
2008-03-27 01:06:21 -06:00
|
|
|
f.pad(f.integer(a, 16, true, &ldigits));
|
|
|
|
f.clearflags();
|
|
|
|
return f;
|
|
|
|
}
|
|
|
|
|
2009-01-15 16:40:27 -07:00
|
|
|
func (f *Fmt) Fmt_x32(a int32) *Fmt {
|
|
|
|
return f.Fmt_x64(int64(a));
|
2008-10-29 15:28:19 -06:00
|
|
|
}
|
|
|
|
|
2009-01-15 16:40:27 -07:00
|
|
|
func (f *Fmt) Fmt_x(a int) *Fmt {
|
|
|
|
return f.Fmt_x64(int64(a));
|
2008-10-29 15:28:19 -06:00
|
|
|
}
|
|
|
|
|
2008-03-27 01:06:21 -06:00
|
|
|
// unsigned hexdecimal
|
2009-01-15 16:40:27 -07:00
|
|
|
func (f *Fmt) Fmt_ux64(a uint64) *Fmt {
|
2008-10-29 15:28:19 -06:00
|
|
|
f.pad(f.integer(int64(a), 16, false, &ldigits));
|
2008-03-27 01:06:21 -06:00
|
|
|
f.clearflags();
|
|
|
|
return f;
|
|
|
|
}
|
|
|
|
|
2009-01-15 16:40:27 -07:00
|
|
|
func (f *Fmt) Fmt_ux32(a uint32) *Fmt {
|
|
|
|
return f.Fmt_ux64(uint64(a));
|
2008-03-27 01:06:21 -06:00
|
|
|
}
|
|
|
|
|
2009-01-15 16:40:27 -07:00
|
|
|
func (f *Fmt) Fmt_ux(a uint) *Fmt {
|
|
|
|
return f.Fmt_ux64(uint64(a));
|
2008-03-27 01:06:21 -06:00
|
|
|
}
|
|
|
|
|
2008-10-29 15:28:19 -06:00
|
|
|
// HEXADECIMAL
|
2009-01-15 16:40:27 -07:00
|
|
|
func (f *Fmt) Fmt_X64(a int64) *Fmt {
|
2008-03-27 01:06:21 -06:00
|
|
|
f.pad(f.integer(a, 16, true, &udigits));
|
|
|
|
f.clearflags();
|
|
|
|
return f;
|
|
|
|
}
|
|
|
|
|
2009-01-15 16:40:27 -07:00
|
|
|
func (f *Fmt) Fmt_X32(a int32) *Fmt {
|
|
|
|
return f.Fmt_X64(int64(a));
|
2008-10-29 15:28:19 -06:00
|
|
|
}
|
|
|
|
|
2009-01-15 16:40:27 -07:00
|
|
|
func (f *Fmt) Fmt_X(a int) *Fmt {
|
|
|
|
return f.Fmt_X64(int64(a));
|
2008-10-29 15:28:19 -06:00
|
|
|
}
|
|
|
|
|
2008-03-27 01:06:21 -06:00
|
|
|
// unsigned HEXADECIMAL
|
2009-01-15 16:40:27 -07:00
|
|
|
func (f *Fmt) Fmt_uX64(a uint64) *Fmt {
|
2008-10-29 15:28:19 -06:00
|
|
|
f.pad(f.integer(int64(a), 16, false, &udigits));
|
2008-03-27 01:06:21 -06:00
|
|
|
f.clearflags();
|
|
|
|
return f;
|
|
|
|
}
|
|
|
|
|
2009-01-15 16:40:27 -07:00
|
|
|
func (f *Fmt) Fmt_uX32(a uint32) *Fmt {
|
|
|
|
return f.Fmt_uX64(uint64(a));
|
2008-03-27 01:06:21 -06:00
|
|
|
}
|
|
|
|
|
2009-01-15 16:40:27 -07:00
|
|
|
func (f *Fmt) Fmt_uX(a uint) *Fmt {
|
|
|
|
return f.Fmt_uX64(uint64(a));
|
2008-03-27 01:06:21 -06:00
|
|
|
}
|
|
|
|
|
2008-10-29 15:28:19 -06:00
|
|
|
// octal
|
2009-01-15 16:40:27 -07:00
|
|
|
func (f *Fmt) Fmt_o64(a int64) *Fmt {
|
2008-03-27 01:06:21 -06:00
|
|
|
f.pad(f.integer(a, 8, true, &ldigits));
|
|
|
|
f.clearflags();
|
|
|
|
return f;
|
|
|
|
}
|
|
|
|
|
2009-01-15 16:40:27 -07:00
|
|
|
func (f *Fmt) Fmt_o32(a int32) *Fmt {
|
|
|
|
return f.Fmt_o64(int64(a));
|
2008-10-29 15:28:19 -06:00
|
|
|
}
|
|
|
|
|
2009-01-15 16:40:27 -07:00
|
|
|
func (f *Fmt) Fmt_o(a int) *Fmt {
|
|
|
|
return f.Fmt_o64(int64(a));
|
2008-10-29 15:28:19 -06:00
|
|
|
}
|
|
|
|
|
|
|
|
|
2008-03-27 01:06:21 -06:00
|
|
|
// unsigned octal
|
2009-01-15 16:40:27 -07:00
|
|
|
func (f *Fmt) Fmt_uo64(a uint64) *Fmt {
|
2008-10-29 15:28:19 -06:00
|
|
|
f.pad(f.integer(int64(a), 8, false, &ldigits));
|
2008-03-27 01:06:21 -06:00
|
|
|
f.clearflags();
|
|
|
|
return f;
|
|
|
|
}
|
|
|
|
|
2009-01-15 16:40:27 -07:00
|
|
|
func (f *Fmt) Fmt_uo32(a uint32) *Fmt {
|
|
|
|
return f.Fmt_uo64(uint64(a));
|
2008-03-27 01:06:21 -06:00
|
|
|
}
|
|
|
|
|
2009-01-15 16:40:27 -07:00
|
|
|
func (f *Fmt) Fmt_uo(a uint) *Fmt {
|
|
|
|
return f.Fmt_uo64(uint64(a));
|
2008-03-27 01:06:21 -06:00
|
|
|
}
|
|
|
|
|
2008-10-29 15:28:19 -06:00
|
|
|
|
|
|
|
// unsigned binary
|
2009-01-15 16:40:27 -07:00
|
|
|
func (f *Fmt) Fmt_b64(a uint64) *Fmt {
|
2008-10-29 15:28:19 -06:00
|
|
|
f.pad(f.integer(int64(a), 2, false, &ldigits));
|
2008-03-27 01:06:21 -06:00
|
|
|
f.clearflags();
|
|
|
|
return f;
|
|
|
|
}
|
|
|
|
|
2009-01-15 16:40:27 -07:00
|
|
|
func (f *Fmt) Fmt_b32(a uint32) *Fmt {
|
|
|
|
return f.Fmt_b64(uint64(a));
|
2008-10-29 15:28:19 -06:00
|
|
|
}
|
|
|
|
|
2009-01-15 16:40:27 -07:00
|
|
|
func (f *Fmt) Fmt_b(a uint) *Fmt {
|
|
|
|
return f.Fmt_b64(uint64(a));
|
2008-10-29 15:28:19 -06:00
|
|
|
}
|
|
|
|
|
|
|
|
|
2008-03-27 01:06:21 -06:00
|
|
|
// character
|
2009-01-15 16:40:27 -07:00
|
|
|
func (f *Fmt) Fmt_c(a int) *Fmt {
|
2008-03-27 01:06:21 -06:00
|
|
|
f.pad(string(a));
|
|
|
|
f.clearflags();
|
|
|
|
return f;
|
|
|
|
}
|
|
|
|
|
|
|
|
// string
|
2009-01-15 16:40:27 -07:00
|
|
|
func (f *Fmt) Fmt_s(s string) *Fmt {
|
2008-03-27 01:06:21 -06:00
|
|
|
if f.prec_present {
|
|
|
|
if f.prec < len(s) {
|
2008-04-02 22:32:39 -06:00
|
|
|
s = s[0:f.prec];
|
2008-03-27 01:06:21 -06:00
|
|
|
}
|
|
|
|
}
|
|
|
|
f.pad(s);
|
|
|
|
f.clearflags();
|
|
|
|
return f;
|
|
|
|
}
|
|
|
|
|
2008-11-24 15:51:33 -07:00
|
|
|
// hexadecimal string
|
2009-01-15 16:40:27 -07:00
|
|
|
func (f *Fmt) Fmt_sx(s string) *Fmt {
|
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 {
|
|
|
|
t += " ";
|
|
|
|
}
|
2008-11-24 15:51:33 -07:00
|
|
|
v := s[i];
|
|
|
|
t += string(ldigits[v>>4]);
|
|
|
|
t += string(ldigits[v&0xF]);
|
|
|
|
}
|
|
|
|
f.pad(t);
|
|
|
|
f.clearflags();
|
|
|
|
return f;
|
|
|
|
}
|
|
|
|
|
2009-01-15 16:40:27 -07:00
|
|
|
func (f *Fmt) Fmt_sX(s string) *Fmt {
|
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);
|
|
|
|
f.clearflags();
|
|
|
|
return f;
|
|
|
|
}
|
|
|
|
|
|
|
|
// quoted string
|
2009-01-15 16:40:27 -07:00
|
|
|
func (f *Fmt) Fmt_q(s string) *Fmt {
|
2008-11-24 15:51:33 -07:00
|
|
|
var quoted string;
|
|
|
|
if f.sharp && strconv.CanBackquote(s) {
|
|
|
|
quoted = "`"+s+"`";
|
|
|
|
} else {
|
|
|
|
quoted = strconv.Quote(s);
|
|
|
|
}
|
|
|
|
f.pad(quoted);
|
|
|
|
f.clearflags();
|
|
|
|
return f;
|
|
|
|
}
|
|
|
|
|
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 {
|
2008-11-17 13:34:03 -07:00
|
|
|
if f.prec_present {
|
|
|
|
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-01-15 16:40:27 -07:00
|
|
|
func fmtString(f *Fmt, s string) *Fmt {
|
2008-03-27 01:06:21 -06:00
|
|
|
f.pad(s);
|
|
|
|
f.clearflags();
|
|
|
|
return f;
|
|
|
|
}
|
|
|
|
|
2008-10-29 14:09:39 -06:00
|
|
|
// float64
|
2009-01-15 16:40:27 -07:00
|
|
|
func (f *Fmt) Fmt_e64(a float64) *Fmt {
|
2009-01-16 14:29:43 -07:00
|
|
|
return fmtString(f, strconv.Ftoa64(a, 'e', doPrec(f, 6)));
|
2008-11-17 13:34:03 -07:00
|
|
|
}
|
|
|
|
|
2009-01-15 16:40:27 -07:00
|
|
|
func (f *Fmt) Fmt_f64(a float64) *Fmt {
|
2009-01-16 14:29:43 -07:00
|
|
|
return fmtString(f, strconv.Ftoa64(a, 'f', doPrec(f, 6)));
|
2008-03-27 01:06:21 -06:00
|
|
|
}
|
|
|
|
|
2009-01-15 16:40:27 -07:00
|
|
|
func (f *Fmt) Fmt_g64(a float64) *Fmt {
|
2009-01-16 14:29:43 -07:00
|
|
|
return fmtString(f, strconv.Ftoa64(a, 'g', doPrec(f, -1)));
|
2008-03-27 01:06:21 -06:00
|
|
|
}
|
|
|
|
|
2009-01-15 16:40:27 -07:00
|
|
|
func (f *Fmt) Fmt_fb64(a float64) *Fmt {
|
2009-01-15 18:22:17 -07:00
|
|
|
return fmtString(f, strconv.Ftoa64(a, '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-01-15 16:40:27 -07:00
|
|
|
func (f *Fmt) Fmt_e32(a float32) *Fmt {
|
2009-01-16 14:29:43 -07:00
|
|
|
return fmtString(f, strconv.Ftoa32(a, 'e', doPrec(f, 6)));
|
2008-11-17 13:34:03 -07:00
|
|
|
}
|
|
|
|
|
2009-01-15 16:40:27 -07:00
|
|
|
func (f *Fmt) Fmt_f32(a float32) *Fmt {
|
2009-01-16 14:29:43 -07:00
|
|
|
return fmtString(f, strconv.Ftoa32(a, 'f', doPrec(f, 6)));
|
2008-11-17 13:34:03 -07:00
|
|
|
}
|
|
|
|
|
2009-01-15 16:40:27 -07:00
|
|
|
func (f *Fmt) Fmt_g32(a float32) *Fmt {
|
2009-01-16 14:29:43 -07:00
|
|
|
return fmtString(f, strconv.Ftoa32(a, 'g', doPrec(f, -1)));
|
2008-11-17 13:34:03 -07:00
|
|
|
}
|
|
|
|
|
2009-01-15 16:40:27 -07:00
|
|
|
func (f *Fmt) Fmt_fb32(a float32) *Fmt {
|
2009-01-15 18:22:17 -07:00
|
|
|
return fmtString(f, strconv.Ftoa32(a, 'b', 0));
|
2008-03-27 01:06:21 -06:00
|
|
|
}
|
|
|
|
|
|
|
|
// float
|
2008-11-17 13:34:03 -07:00
|
|
|
func (x *Fmt) f(a float) *Fmt {
|
2009-01-15 17:16:42 -07:00
|
|
|
if strconv.FloatSize == 32 {
|
2009-01-15 16:40:27 -07:00
|
|
|
return x.Fmt_f32(float32(a))
|
2008-11-17 13:34:03 -07:00
|
|
|
}
|
2009-01-15 16:40:27 -07:00
|
|
|
return x.Fmt_f64(float64(a))
|
2008-10-29 15:28:19 -06:00
|
|
|
}
|
|
|
|
|
2008-03-27 01:06:21 -06:00
|
|
|
func (x *Fmt) e(a float) *Fmt {
|
2009-01-15 17:16:42 -07:00
|
|
|
if strconv.FloatSize == 32 {
|
2009-01-15 16:40:27 -07:00
|
|
|
return x.Fmt_e32(float32(a))
|
2008-11-17 13:34:03 -07:00
|
|
|
}
|
2009-01-15 16:40:27 -07:00
|
|
|
return x.Fmt_e64(float64(a))
|
2008-03-27 01:06:21 -06:00
|
|
|
}
|
|
|
|
|
2008-11-17 13:34:03 -07:00
|
|
|
func (x *Fmt) g(a float) *Fmt {
|
2009-01-15 17:16:42 -07:00
|
|
|
if strconv.FloatSize == 32 {
|
2009-01-15 16:40:27 -07:00
|
|
|
return x.Fmt_g32(float32(a))
|
2008-11-17 13:34:03 -07:00
|
|
|
}
|
2009-01-15 16:40:27 -07:00
|
|
|
return x.Fmt_g64(float64(a))
|
2008-10-29 15:28:19 -06:00
|
|
|
}
|
|
|
|
|
2008-11-17 13:34:03 -07:00
|
|
|
func (x *Fmt) fb(a float) *Fmt {
|
2009-01-15 17:16:42 -07:00
|
|
|
if strconv.FloatSize == 32 {
|
2009-01-15 16:40:27 -07:00
|
|
|
return x.Fmt_fb32(float32(a))
|
2008-11-17 13:34:03 -07:00
|
|
|
}
|
2009-01-15 16:40:27 -07:00
|
|
|
return x.Fmt_fb64(float64(a))
|
2008-03-27 01:06:21 -06:00
|
|
|
}
|