2010-12-03 05:34:57 +01: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
|
|
|
|
|
|
|
|
import (
|
2014-07-19 10:53:52 +02:00
|
|
|
"math"
|
2010-12-03 05:34:57 +01:00
|
|
|
"strconv"
|
2011-12-07 02:11:29 +01:00
|
|
|
"unicode/utf8"
|
2010-12-03 05:34:57 +01:00
|
|
|
)
|
|
|
|
|
|
|
|
const (
|
2014-06-07 00:37:27 +02:00
|
|
|
// %b of an int64, plus a sign.
|
|
|
|
// Hex can add 0x and we handle it specially.
|
|
|
|
nByte = 65
|
2010-12-03 05:34:57 +01:00
|
|
|
|
|
|
|
ldigits = "0123456789abcdef"
|
|
|
|
udigits = "0123456789ABCDEF"
|
|
|
|
)
|
|
|
|
|
|
|
|
const (
|
|
|
|
signed = true
|
|
|
|
unsigned = false
|
|
|
|
)
|
|
|
|
|
|
|
|
var padZeroBytes = make([]byte, nByte)
|
|
|
|
var padSpaceBytes = make([]byte, nByte)
|
|
|
|
|
|
|
|
func init() {
|
|
|
|
for i := 0; i < nByte; i++ {
|
|
|
|
padZeroBytes[i] = '0'
|
|
|
|
padSpaceBytes[i] = ' '
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// A fmt is the raw formatter used by Printf etc.
|
2012-03-30 23:27:11 +02:00
|
|
|
// It prints into a buffer that must be set up separately.
|
2010-12-03 05:34:57 +01:00
|
|
|
type fmt struct {
|
|
|
|
intbuf [nByte]byte
|
2012-03-30 23:27:11 +02:00
|
|
|
buf *buffer
|
2010-12-03 05:34:57 +01:00
|
|
|
// width, precision
|
|
|
|
wid int
|
|
|
|
prec int
|
|
|
|
// flags
|
|
|
|
widPresent bool
|
|
|
|
precPresent bool
|
|
|
|
minus bool
|
|
|
|
plus bool
|
|
|
|
sharp bool
|
|
|
|
space bool
|
2011-01-21 19:19:03 +01:00
|
|
|
unicode bool
|
2011-09-16 17:47:21 +02:00
|
|
|
uniQuote bool // Use 'x'= prefix for %U if printable.
|
2010-12-03 05:34:57 +01:00
|
|
|
zero bool
|
|
|
|
}
|
|
|
|
|
|
|
|
func (f *fmt) clearflags() {
|
|
|
|
f.wid = 0
|
|
|
|
f.widPresent = false
|
|
|
|
f.prec = 0
|
|
|
|
f.precPresent = false
|
|
|
|
f.minus = false
|
|
|
|
f.plus = false
|
|
|
|
f.sharp = false
|
|
|
|
f.space = false
|
2011-01-21 19:19:03 +01:00
|
|
|
f.unicode = false
|
2011-09-16 17:47:21 +02:00
|
|
|
f.uniQuote = false
|
2010-12-03 05:34:57 +01:00
|
|
|
f.zero = false
|
|
|
|
}
|
|
|
|
|
2012-03-30 23:27:11 +02:00
|
|
|
func (f *fmt) init(buf *buffer) {
|
2010-12-03 05:34:57 +01:00
|
|
|
f.buf = buf
|
|
|
|
f.clearflags()
|
|
|
|
}
|
|
|
|
|
2013-01-29 21:52:43 +01:00
|
|
|
// computePadding computes left and right padding widths (only one will be non-zero).
|
2010-12-03 05:34:57 +01:00
|
|
|
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 {
|
|
|
|
if left && f.zero {
|
|
|
|
return padZeroBytes, w, 0
|
|
|
|
}
|
|
|
|
if left {
|
|
|
|
return padSpaceBytes, w, 0
|
|
|
|
} else {
|
|
|
|
// can't be zero padding on the right
|
|
|
|
return padSpaceBytes, 0, w
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
2013-01-29 21:52:43 +01:00
|
|
|
// writePadding generates n bytes of padding.
|
2010-12-03 05:34:57 +01:00
|
|
|
func (f *fmt) writePadding(n int, padding []byte) {
|
|
|
|
for n > 0 {
|
|
|
|
m := n
|
|
|
|
if m > nByte {
|
|
|
|
m = nByte
|
|
|
|
}
|
|
|
|
f.buf.Write(padding[0:m])
|
|
|
|
n -= m
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2013-01-29 21:52:43 +01:00
|
|
|
// pad appends b to f.buf, padded on left (w > 0) or right (w < 0 or f.minus).
|
2010-12-03 05:34:57 +01:00
|
|
|
func (f *fmt) pad(b []byte) {
|
2012-10-23 06:31:11 +02:00
|
|
|
if !f.widPresent || f.wid == 0 {
|
|
|
|
f.buf.Write(b)
|
|
|
|
return
|
2010-12-03 05:34:57 +01:00
|
|
|
}
|
2012-10-23 06:31:11 +02:00
|
|
|
padding, left, right := f.computePadding(len(b))
|
2010-12-03 05:34:57 +01:00
|
|
|
if left > 0 {
|
|
|
|
f.writePadding(left, padding)
|
|
|
|
}
|
|
|
|
f.buf.Write(b)
|
|
|
|
if right > 0 {
|
|
|
|
f.writePadding(right, padding)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2013-01-29 21:52:43 +01:00
|
|
|
// padString appends s to buf, padded on left (w > 0) or right (w < 0 or f.minus).
|
2010-12-03 05:34:57 +01:00
|
|
|
func (f *fmt) padString(s string) {
|
2012-10-23 06:31:11 +02:00
|
|
|
if !f.widPresent || f.wid == 0 {
|
|
|
|
f.buf.WriteString(s)
|
|
|
|
return
|
2010-12-03 05:34:57 +01:00
|
|
|
}
|
2012-10-23 06:31:11 +02:00
|
|
|
padding, left, right := f.computePadding(utf8.RuneCountInString(s))
|
2010-12-03 05:34:57 +01:00
|
|
|
if left > 0 {
|
|
|
|
f.writePadding(left, padding)
|
|
|
|
}
|
|
|
|
f.buf.WriteString(s)
|
|
|
|
if right > 0 {
|
|
|
|
f.writePadding(right, padding)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2012-01-13 06:11:45 +01:00
|
|
|
var (
|
|
|
|
trueBytes = []byte("true")
|
|
|
|
falseBytes = []byte("false")
|
|
|
|
)
|
|
|
|
|
2010-12-03 05:34:57 +01:00
|
|
|
// fmt_boolean formats a boolean.
|
|
|
|
func (f *fmt) fmt_boolean(v bool) {
|
|
|
|
if v {
|
2012-01-13 06:11:45 +01:00
|
|
|
f.pad(trueBytes)
|
2010-12-03 05:34:57 +01:00
|
|
|
} else {
|
2012-01-13 06:11:45 +01:00
|
|
|
f.pad(falseBytes)
|
2010-12-03 05:34:57 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// 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) {
|
2011-09-16 17:47:21 +02:00
|
|
|
// precision of 0 and value of 0 means "print nothing"
|
|
|
|
if f.precPresent && f.prec == 0 && a == 0 {
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
2010-12-03 05:34:57 +01:00
|
|
|
var buf []byte = f.intbuf[0:]
|
2014-06-07 00:37:27 +02:00
|
|
|
if f.widPresent {
|
|
|
|
width := f.wid
|
|
|
|
if base == 16 && f.sharp {
|
|
|
|
// Also adds "0x".
|
|
|
|
width += 2
|
|
|
|
}
|
|
|
|
if width > nByte {
|
|
|
|
// We're going to need a bigger boat.
|
|
|
|
buf = make([]byte, width)
|
|
|
|
}
|
2013-11-06 20:49:01 +01:00
|
|
|
}
|
|
|
|
|
2010-12-03 05:34:57 +01:00
|
|
|
negative := signedness == signed && a < 0
|
|
|
|
if negative {
|
|
|
|
a = -a
|
|
|
|
}
|
|
|
|
|
|
|
|
// two ways to ask for extra leading zero digits: %.3d or %03d.
|
|
|
|
// apparently the first cancels the second.
|
|
|
|
prec := 0
|
|
|
|
if f.precPresent {
|
|
|
|
prec = f.prec
|
|
|
|
f.zero = false
|
|
|
|
} else if f.zero && f.widPresent && !f.minus && f.wid > 0 {
|
|
|
|
prec = f.wid
|
|
|
|
if negative || f.plus || f.space {
|
|
|
|
prec-- // leave room for sign
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// 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.
|
2013-11-06 20:49:01 +01:00
|
|
|
i := len(buf)
|
2010-12-03 05:34:57 +01:00
|
|
|
ua := uint64(a)
|
|
|
|
for ua >= base {
|
|
|
|
i--
|
|
|
|
buf[i] = digits[ua%base]
|
|
|
|
ua /= base
|
|
|
|
}
|
|
|
|
i--
|
|
|
|
buf[i] = digits[ua]
|
2013-11-06 20:49:01 +01:00
|
|
|
for i > 0 && prec > len(buf)-i {
|
2010-12-03 05:34:57 +01:00
|
|
|
i--
|
|
|
|
buf[i] = '0'
|
|
|
|
}
|
|
|
|
|
|
|
|
// Various prefixes: 0x, -, etc.
|
|
|
|
if f.sharp {
|
|
|
|
switch base {
|
|
|
|
case 8:
|
|
|
|
if buf[i] != '0' {
|
|
|
|
i--
|
|
|
|
buf[i] = '0'
|
|
|
|
}
|
|
|
|
case 16:
|
|
|
|
i--
|
|
|
|
buf[i] = 'x' + digits[10] - 'a'
|
|
|
|
i--
|
|
|
|
buf[i] = '0'
|
|
|
|
}
|
|
|
|
}
|
2011-01-21 19:19:03 +01:00
|
|
|
if f.unicode {
|
|
|
|
i--
|
|
|
|
buf[i] = '+'
|
|
|
|
i--
|
|
|
|
buf[i] = 'U'
|
|
|
|
}
|
2010-12-03 05:34:57 +01:00
|
|
|
|
|
|
|
if negative {
|
|
|
|
i--
|
|
|
|
buf[i] = '-'
|
|
|
|
} else if f.plus {
|
|
|
|
i--
|
|
|
|
buf[i] = '+'
|
|
|
|
} else if f.space {
|
|
|
|
i--
|
|
|
|
buf[i] = ' '
|
|
|
|
}
|
2011-09-16 17:47:21 +02:00
|
|
|
|
|
|
|
// If we want a quoted char for %#U, move the data up to make room.
|
2012-03-30 23:27:11 +02:00
|
|
|
if f.unicode && f.uniQuote && a >= 0 && a <= utf8.MaxRune && strconv.IsPrint(rune(a)) {
|
2011-12-02 20:34:41 +01:00
|
|
|
runeWidth := utf8.RuneLen(rune(a))
|
2011-09-16 17:47:21 +02:00
|
|
|
width := 1 + 1 + runeWidth + 1 // space, quote, rune, quote
|
|
|
|
copy(buf[i-width:], buf[i:]) // guaranteed to have enough room.
|
|
|
|
i -= width
|
|
|
|
// Now put " 'x'" at the end.
|
|
|
|
j := len(buf) - width
|
|
|
|
buf[j] = ' '
|
|
|
|
j++
|
|
|
|
buf[j] = '\''
|
|
|
|
j++
|
2011-12-02 20:34:41 +01:00
|
|
|
utf8.EncodeRune(buf[j:], rune(a))
|
2011-09-16 17:47:21 +02:00
|
|
|
j += runeWidth
|
|
|
|
buf[j] = '\''
|
|
|
|
}
|
|
|
|
|
2010-12-03 05:34:57 +01:00
|
|
|
f.pad(buf[i:])
|
|
|
|
}
|
|
|
|
|
2011-04-07 19:09:10 +02:00
|
|
|
// truncate truncates the string to the specified precision, if present.
|
|
|
|
func (f *fmt) truncate(s string) string {
|
|
|
|
if f.precPresent && f.prec < utf8.RuneCountInString(s) {
|
|
|
|
n := f.prec
|
|
|
|
for i := range s {
|
|
|
|
if n == 0 {
|
|
|
|
s = s[:i]
|
|
|
|
break
|
|
|
|
}
|
|
|
|
n--
|
2010-12-03 05:34:57 +01:00
|
|
|
}
|
|
|
|
}
|
2011-04-07 19:09:10 +02:00
|
|
|
return s
|
|
|
|
}
|
|
|
|
|
|
|
|
// fmt_s formats a string.
|
|
|
|
func (f *fmt) fmt_s(s string) {
|
|
|
|
s = f.truncate(s)
|
2010-12-03 05:34:57 +01:00
|
|
|
f.padString(s)
|
|
|
|
}
|
|
|
|
|
2012-10-23 06:31:11 +02:00
|
|
|
// fmt_sbx formats a string or byte slice as a hexadecimal encoding of its bytes.
|
|
|
|
func (f *fmt) fmt_sbx(s string, b []byte, digits string) {
|
|
|
|
n := len(b)
|
|
|
|
if b == nil {
|
|
|
|
n = len(s)
|
|
|
|
}
|
|
|
|
x := digits[10] - 'a' + 'x'
|
2012-01-13 06:11:45 +01:00
|
|
|
// TODO: Avoid buffer by pre-padding.
|
2012-10-23 06:31:11 +02:00
|
|
|
var buf []byte
|
|
|
|
for i := 0; i < n; i++ {
|
2010-12-03 05:34:57 +01:00
|
|
|
if i > 0 && f.space {
|
2012-10-23 06:31:11 +02:00
|
|
|
buf = append(buf, ' ')
|
|
|
|
}
|
|
|
|
if f.sharp {
|
|
|
|
buf = append(buf, '0', x)
|
|
|
|
}
|
|
|
|
var c byte
|
|
|
|
if b == nil {
|
|
|
|
c = s[i]
|
|
|
|
} else {
|
|
|
|
c = b[i]
|
2010-12-03 05:34:57 +01:00
|
|
|
}
|
2012-10-23 06:31:11 +02:00
|
|
|
buf = append(buf, digits[c>>4], digits[c&0xF])
|
2010-12-03 05:34:57 +01:00
|
|
|
}
|
2012-10-23 06:31:11 +02:00
|
|
|
f.pad(buf)
|
|
|
|
}
|
|
|
|
|
|
|
|
// fmt_sx formats a string as a hexadecimal encoding of its bytes.
|
|
|
|
func (f *fmt) fmt_sx(s, digits string) {
|
|
|
|
f.fmt_sbx(s, nil, digits)
|
|
|
|
}
|
|
|
|
|
|
|
|
// fmt_bx formats a byte slice as a hexadecimal encoding of its bytes.
|
|
|
|
func (f *fmt) fmt_bx(b []byte, digits string) {
|
|
|
|
f.fmt_sbx("", b, digits)
|
2010-12-03 05:34:57 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
// fmt_q formats a string as a double-quoted, escaped Go string constant.
|
|
|
|
func (f *fmt) fmt_q(s string) {
|
2011-04-07 19:09:10 +02:00
|
|
|
s = f.truncate(s)
|
2010-12-03 05:34:57 +01:00
|
|
|
var quoted string
|
|
|
|
if f.sharp && strconv.CanBackquote(s) {
|
|
|
|
quoted = "`" + s + "`"
|
|
|
|
} else {
|
2011-09-16 17:47:21 +02:00
|
|
|
if f.plus {
|
|
|
|
quoted = strconv.QuoteToASCII(s)
|
|
|
|
} else {
|
|
|
|
quoted = strconv.Quote(s)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
f.padString(quoted)
|
|
|
|
}
|
|
|
|
|
|
|
|
// fmt_qc formats the integer as a single-quoted, escaped Go character constant.
|
|
|
|
// If the character is not valid Unicode, it will print '\ufffd'.
|
|
|
|
func (f *fmt) fmt_qc(c int64) {
|
2012-01-13 06:11:45 +01:00
|
|
|
var quoted []byte
|
2011-09-16 17:47:21 +02:00
|
|
|
if f.plus {
|
2012-01-13 06:11:45 +01:00
|
|
|
quoted = strconv.AppendQuoteRuneToASCII(f.intbuf[0:0], rune(c))
|
2011-09-16 17:47:21 +02:00
|
|
|
} else {
|
2012-01-13 06:11:45 +01:00
|
|
|
quoted = strconv.AppendQuoteRune(f.intbuf[0:0], rune(c))
|
2010-12-03 05:34:57 +01:00
|
|
|
}
|
2012-01-13 06:11:45 +01:00
|
|
|
f.pad(quoted)
|
2010-12-03 05:34:57 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
// floating-point
|
|
|
|
|
|
|
|
func doPrec(f *fmt, def int) int {
|
|
|
|
if f.precPresent {
|
|
|
|
return f.prec
|
|
|
|
}
|
|
|
|
return def
|
|
|
|
}
|
|
|
|
|
2012-01-13 06:11:45 +01:00
|
|
|
// formatFloat formats a float64; it is an efficient equivalent to f.pad(strconv.FormatFloat()...).
|
|
|
|
func (f *fmt) formatFloat(v float64, verb byte, prec, n int) {
|
2014-07-19 10:53:52 +02:00
|
|
|
// Format number, reserving space for leading + sign if needed.
|
|
|
|
num := strconv.AppendFloat(f.intbuf[0:1], v, verb, prec, n)
|
|
|
|
if num[1] == '-' || num[1] == '+' {
|
|
|
|
num = num[1:]
|
|
|
|
} else {
|
|
|
|
num[0] = '+'
|
|
|
|
}
|
|
|
|
// Special handling for infinity, which doesn't look like a number so shouldn't be padded with zeros.
|
|
|
|
if math.IsInf(v, 0) {
|
|
|
|
if f.zero {
|
|
|
|
defer func() { f.zero = true }()
|
|
|
|
f.zero = false
|
2013-11-06 20:49:01 +01:00
|
|
|
}
|
2014-07-19 10:53:52 +02:00
|
|
|
}
|
|
|
|
// num is now a signed version of the number.
|
|
|
|
// If we're zero padding, want the sign before the leading zeros.
|
|
|
|
// Achieve this by writing the sign out and then padding the unsigned number.
|
|
|
|
if f.zero && f.widPresent && f.wid > len(num) {
|
|
|
|
if f.space && v >= 0 {
|
|
|
|
f.buf.WriteByte(' ') // This is what C does: even with zero, f.space means space.
|
|
|
|
f.wid--
|
|
|
|
} else if f.plus || v < 0 {
|
|
|
|
f.buf.WriteByte(num[0])
|
2014-06-05 01:15:33 +02:00
|
|
|
f.wid--
|
2010-12-03 05:34:57 +01:00
|
|
|
}
|
2014-07-19 10:53:52 +02:00
|
|
|
f.pad(num[1:])
|
|
|
|
return
|
2010-12-03 05:34:57 +01:00
|
|
|
}
|
2014-07-19 10:53:52 +02:00
|
|
|
// f.space says to replace a leading + with a space.
|
|
|
|
if f.space && num[0] == '+' {
|
|
|
|
num[0] = ' '
|
|
|
|
f.pad(num)
|
|
|
|
return
|
|
|
|
}
|
|
|
|
// Now we know the sign is attached directly to the number, if present at all.
|
|
|
|
// We want a sign if asked for, if it's negative, or if it's infinity (+Inf vs. -Inf).
|
|
|
|
if f.plus || num[0] == '-' || math.IsInf(v, 0) {
|
|
|
|
f.pad(num)
|
|
|
|
return
|
|
|
|
}
|
|
|
|
// No sign to show and the number is positive; just print the unsigned number.
|
|
|
|
f.pad(num[1:])
|
2010-12-03 05:34:57 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
// fmt_e64 formats a float64 in the form -1.23e+12.
|
2012-01-13 06:11:45 +01:00
|
|
|
func (f *fmt) fmt_e64(v float64) { f.formatFloat(v, 'e', doPrec(f, 6), 64) }
|
2010-12-03 05:34:57 +01:00
|
|
|
|
|
|
|
// fmt_E64 formats a float64 in the form -1.23E+12.
|
2012-01-13 06:11:45 +01:00
|
|
|
func (f *fmt) fmt_E64(v float64) { f.formatFloat(v, 'E', doPrec(f, 6), 64) }
|
2010-12-03 05:34:57 +01:00
|
|
|
|
|
|
|
// fmt_f64 formats a float64 in the form -1.23.
|
2012-01-13 06:11:45 +01:00
|
|
|
func (f *fmt) fmt_f64(v float64) { f.formatFloat(v, 'f', doPrec(f, 6), 64) }
|
2010-12-03 05:34:57 +01:00
|
|
|
|
|
|
|
// fmt_g64 formats a float64 in the 'f' or 'e' form according to size.
|
2012-01-13 06:11:45 +01:00
|
|
|
func (f *fmt) fmt_g64(v float64) { f.formatFloat(v, 'g', doPrec(f, -1), 64) }
|
2010-12-03 05:34:57 +01:00
|
|
|
|
2012-12-22 02:15:33 +01:00
|
|
|
// fmt_G64 formats a float64 in the 'f' or 'E' form according to size.
|
2012-01-13 06:11:45 +01:00
|
|
|
func (f *fmt) fmt_G64(v float64) { f.formatFloat(v, 'G', doPrec(f, -1), 64) }
|
2010-12-03 05:34:57 +01:00
|
|
|
|
|
|
|
// fmt_fb64 formats a float64 in the form -123p3 (exponent is power of 2).
|
2012-01-13 06:11:45 +01:00
|
|
|
func (f *fmt) fmt_fb64(v float64) { f.formatFloat(v, 'b', 0, 64) }
|
2010-12-03 05:34:57 +01:00
|
|
|
|
|
|
|
// float32
|
|
|
|
// cannot defer to float64 versions
|
|
|
|
// because it will get rounding wrong in corner cases.
|
|
|
|
|
|
|
|
// fmt_e32 formats a float32 in the form -1.23e+12.
|
2012-01-13 06:11:45 +01:00
|
|
|
func (f *fmt) fmt_e32(v float32) { f.formatFloat(float64(v), 'e', doPrec(f, 6), 32) }
|
2010-12-03 05:34:57 +01:00
|
|
|
|
|
|
|
// fmt_E32 formats a float32 in the form -1.23E+12.
|
2012-01-13 06:11:45 +01:00
|
|
|
func (f *fmt) fmt_E32(v float32) { f.formatFloat(float64(v), 'E', doPrec(f, 6), 32) }
|
2010-12-03 05:34:57 +01:00
|
|
|
|
|
|
|
// fmt_f32 formats a float32 in the form -1.23.
|
2012-01-13 06:11:45 +01:00
|
|
|
func (f *fmt) fmt_f32(v float32) { f.formatFloat(float64(v), 'f', doPrec(f, 6), 32) }
|
2010-12-03 05:34:57 +01:00
|
|
|
|
|
|
|
// fmt_g32 formats a float32 in the 'f' or 'e' form according to size.
|
2012-01-13 06:11:45 +01:00
|
|
|
func (f *fmt) fmt_g32(v float32) { f.formatFloat(float64(v), 'g', doPrec(f, -1), 32) }
|
2010-12-03 05:34:57 +01:00
|
|
|
|
|
|
|
// fmt_G32 formats a float32 in the 'f' or 'E' form according to size.
|
2012-01-13 06:11:45 +01:00
|
|
|
func (f *fmt) fmt_G32(v float32) { f.formatFloat(float64(v), 'G', doPrec(f, -1), 32) }
|
2010-12-03 05:34:57 +01:00
|
|
|
|
|
|
|
// fmt_fb32 formats a float32 in the form -123p3 (exponent is power of 2).
|
2012-01-13 06:11:45 +01:00
|
|
|
func (f *fmt) fmt_fb32(v float32) { f.formatFloat(float64(v), 'b', 0, 32) }
|
2010-12-03 05:34:57 +01:00
|
|
|
|
|
|
|
// fmt_c64 formats a complex64 according to the verb.
|
2011-12-02 20:34:41 +01:00
|
|
|
func (f *fmt) fmt_c64(v complex64, verb rune) {
|
2014-07-19 10:53:52 +02:00
|
|
|
f.fmt_complex(float64(real(v)), float64(imag(v)), 32, verb)
|
2010-12-03 05:34:57 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
// fmt_c128 formats a complex128 according to the verb.
|
2011-12-02 20:34:41 +01:00
|
|
|
func (f *fmt) fmt_c128(v complex128, verb rune) {
|
2014-07-19 10:53:52 +02:00
|
|
|
f.fmt_complex(real(v), imag(v), 64, verb)
|
|
|
|
}
|
|
|
|
|
|
|
|
// fmt_complex formats a complex number as (r+ji).
|
|
|
|
func (f *fmt) fmt_complex(r, j float64, size int, verb rune) {
|
2010-12-03 05:34:57 +01:00
|
|
|
f.buf.WriteByte('(')
|
2012-12-22 02:15:33 +01:00
|
|
|
oldPlus := f.plus
|
2014-07-19 10:53:52 +02:00
|
|
|
oldSpace := f.space
|
|
|
|
oldWid := f.wid
|
2010-12-03 05:34:57 +01:00
|
|
|
for i := 0; ; i++ {
|
|
|
|
switch verb {
|
2013-11-06 20:49:01 +01:00
|
|
|
case 'b':
|
2014-07-19 10:53:52 +02:00
|
|
|
f.formatFloat(r, 'b', 0, size)
|
2010-12-03 05:34:57 +01:00
|
|
|
case 'e':
|
2014-07-19 10:53:52 +02:00
|
|
|
f.formatFloat(r, 'e', doPrec(f, 6), size)
|
2010-12-03 05:34:57 +01:00
|
|
|
case 'E':
|
2014-07-19 10:53:52 +02:00
|
|
|
f.formatFloat(r, 'E', doPrec(f, 6), size)
|
|
|
|
case 'f', 'F':
|
|
|
|
f.formatFloat(r, 'f', doPrec(f, 6), size)
|
2010-12-03 05:34:57 +01:00
|
|
|
case 'g':
|
2014-07-19 10:53:52 +02:00
|
|
|
f.formatFloat(r, 'g', doPrec(f, -1), size)
|
2010-12-03 05:34:57 +01:00
|
|
|
case 'G':
|
2014-07-19 10:53:52 +02:00
|
|
|
f.formatFloat(r, 'G', doPrec(f, -1), size)
|
2010-12-03 05:34:57 +01:00
|
|
|
}
|
|
|
|
if i != 0 {
|
|
|
|
break
|
|
|
|
}
|
2014-07-19 10:53:52 +02:00
|
|
|
// Imaginary part always has a sign.
|
2010-12-03 05:34:57 +01:00
|
|
|
f.plus = true
|
2014-07-19 10:53:52 +02:00
|
|
|
f.space = false
|
|
|
|
f.wid = oldWid
|
|
|
|
r = j
|
2010-12-03 05:34:57 +01:00
|
|
|
}
|
2014-07-19 10:53:52 +02:00
|
|
|
f.space = oldSpace
|
2012-12-22 02:15:33 +01:00
|
|
|
f.plus = oldPlus
|
2014-07-19 10:53:52 +02:00
|
|
|
f.wid = oldWid
|
2010-12-03 05:34:57 +01:00
|
|
|
f.buf.Write(irparenBytes)
|
|
|
|
}
|