blob: d4b92f812148f392bd4ace92a3cb3ae8a7b0f976 [file] [log] [blame]
Rob Pike42d78502008-03-27 00:06:21 -07001// Copyright 2009 The Go Authors. All rights reserved.
2// Use of this source code is governed by a BSD-style
3// license that can be found in the LICENSE file.
4
5package fmt
6
Russ Cox387df5e2008-11-24 14:51:33 -08007import (
Robert Griesemer1c729592009-12-15 15:27:16 -08008 "strconv"
Rob Pike30aa7012011-11-08 15:40:58 -08009 "unicode/utf8"
Russ Cox387df5e2008-11-24 14:51:33 -080010)
Russ Cox079c00a2008-11-17 12:34:03 -080011
Rob Piked984f982009-12-03 00:04:40 -080012const (
Martin Möhrmann033e3e12016-02-27 19:47:43 +010013 ldigits = "0123456789abcdefx"
14 udigits = "0123456789ABCDEFX"
Rob Piked984f982009-12-03 00:04:40 -080015)
Rob Pike655c2812008-07-08 14:20:04 -070016
Rob Pikef91cd442009-12-06 15:01:07 -080017const (
Robert Griesemer1c729592009-12-15 15:27:16 -080018 signed = true
19 unsigned = false
Rob Pikef91cd442009-12-06 15:01:07 -080020)
21
Rob Pikea2242452014-10-03 13:23:35 -070022// flags placed in a separate struct for easy clearing.
23type fmtFlags struct {
24 widPresent bool
25 precPresent bool
26 minus bool
27 plus bool
28 sharp bool
29 space bool
Rob Pikea2242452014-10-03 13:23:35 -070030 zero bool
31
32 // For the formats %+v %#v, we set the plusV/sharpV flags
33 // and clear the plus/sharp flags since %+v and %#v are in effect
34 // different, flagless formats set at the top level.
35 plusV bool
36 sharpV bool
37}
38
Rob Pike353ef802009-12-06 12:58:16 -080039// A fmt is the raw formatter used by Printf etc.
Russ Cox0bc18812012-03-06 23:27:11 -050040// It prints into a buffer that must be set up separately.
Rob Pike353ef802009-12-06 12:58:16 -080041type fmt struct {
Martin Möhrmann7345fa52016-03-21 14:36:21 +010042 buf *buffer
43
Rob Pikea2242452014-10-03 13:23:35 -070044 fmtFlags
Martin Möhrmann7345fa52016-03-21 14:36:21 +010045
46 wid int // width
47 prec int // precision
48
Brad Fitzpatrick7534a722016-11-17 14:39:11 +000049 // intbuf is large enough to store %b of an int64 with a sign and
Martin Möhrmann7345fa52016-03-21 14:36:21 +010050 // avoids padding at the end of the struct on 32 bit architectures.
51 intbuf [68]byte
Rob Pike42d78502008-03-27 00:06:21 -070052}
53
Rob Pike353ef802009-12-06 12:58:16 -080054func (f *fmt) clearflags() {
Rob Pikea2242452014-10-03 13:23:35 -070055 f.fmtFlags = fmtFlags{}
Rob Pike42d78502008-03-27 00:06:21 -070056}
57
Russ Cox0bc18812012-03-06 23:27:11 -050058func (f *fmt) init(buf *buffer) {
Robert Griesemer1c729592009-12-15 15:27:16 -080059 f.buf = buf
60 f.clearflags()
Rob Pike4c0e51c2009-12-06 12:03:52 -080061}
62
Robin Eklind3692dfd2013-01-22 17:12:45 -050063// writePadding generates n bytes of padding.
Martin Möhrmannabcad1e2016-02-20 01:51:32 +010064func (f *fmt) writePadding(n int) {
65 if n <= 0 { // No padding bytes needed.
66 return
Rob Pike4c0e51c2009-12-06 12:03:52 -080067 }
Martin Möhrmannabcad1e2016-02-20 01:51:32 +010068 buf := *f.buf
69 oldLen := len(buf)
70 newLen := oldLen + n
71 // Make enough room for padding.
72 if newLen > cap(buf) {
73 buf = make(buffer, cap(buf)*2+n)
74 copy(buf, *f.buf)
75 }
76 // Decide which byte the padding should be filled with.
77 padByte := byte(' ')
78 if f.zero {
79 padByte = byte('0')
80 }
81 // Fill padding with padByte.
82 padding := buf[oldLen:newLen]
83 for i := range padding {
84 padding[i] = padByte
85 }
86 *f.buf = buf[:newLen]
Rob Pike4c0e51c2009-12-06 12:03:52 -080087}
88
Martin Möhrmannabcad1e2016-02-20 01:51:32 +010089// pad appends b to f.buf, padded on left (!f.minus) or right (f.minus).
Rob Pikef91cd442009-12-06 15:01:07 -080090func (f *fmt) pad(b []byte) {
Rob Pike53bc1942012-05-29 15:08:08 -070091 if !f.widPresent || f.wid == 0 {
92 f.buf.Write(b)
93 return
Rob Pike4c0e51c2009-12-06 12:03:52 -080094 }
Martin Möhrmannabcad1e2016-02-20 01:51:32 +010095 width := f.wid - utf8.RuneCount(b)
96 if !f.minus {
97 // left padding
98 f.writePadding(width)
99 f.buf.Write(b)
100 } else {
101 // right padding
102 f.buf.Write(b)
103 f.writePadding(width)
Rob Pike4c0e51c2009-12-06 12:03:52 -0800104 }
Rob Pike42d78502008-03-27 00:06:21 -0700105}
106
Martin Möhrmannabcad1e2016-02-20 01:51:32 +0100107// padString appends s to f.buf, padded on left (!f.minus) or right (f.minus).
Rob Pikef91cd442009-12-06 15:01:07 -0800108func (f *fmt) padString(s string) {
Rob Pike53bc1942012-05-29 15:08:08 -0700109 if !f.widPresent || f.wid == 0 {
110 f.buf.WriteString(s)
111 return
Rob Pike42d78502008-03-27 00:06:21 -0700112 }
Martin Möhrmannabcad1e2016-02-20 01:51:32 +0100113 width := f.wid - utf8.RuneCountInString(s)
114 if !f.minus {
115 // left padding
116 f.writePadding(width)
117 f.buf.WriteString(s)
118 } else {
119 // right padding
120 f.buf.WriteString(s)
121 f.writePadding(width)
Rob Pike4c0e51c2009-12-06 12:03:52 -0800122 }
Rob Pike42d78502008-03-27 00:06:21 -0700123}
124
Rob Pike353ef802009-12-06 12:58:16 -0800125// fmt_boolean formats a boolean.
126func (f *fmt) fmt_boolean(v bool) {
Rob Pike85647c92009-03-06 03:35:38 -0800127 if v {
Martin Möhrmann7da4ced2016-02-21 16:05:44 +0100128 f.padString("true")
Rob Pike362ea7c2008-06-24 12:54:26 -0700129 } else {
Martin Möhrmann7da4ced2016-02-21 16:05:44 +0100130 f.padString("false")
Rob Pike362ea7c2008-06-24 12:54:26 -0700131 }
Rob Pike362ea7c2008-06-24 12:54:26 -0700132}
133
Martin Möhrmannd38275c2016-03-11 13:29:23 +0100134// fmt_unicode formats a uint64 as "U+0078" or with f.sharp set as "U+0078 'x'".
135func (f *fmt) fmt_unicode(u uint64) {
136 buf := f.intbuf[0:]
137
138 // With default precision set the maximum needed buf length is 18
Martin Möhrmann7345fa52016-03-21 14:36:21 +0100139 // for formatting -1 with %#U ("U+FFFFFFFFFFFFFFFF") which fits
140 // into the already allocated intbuf with a capacity of 68 bytes.
Martin Möhrmannd38275c2016-03-11 13:29:23 +0100141 prec := 4
142 if f.precPresent && f.prec > 4 {
143 prec = f.prec
144 // Compute space needed for "U+" , number, " '", character, "'".
145 width := 2 + prec + 2 + utf8.UTFMax + 1
Martin Möhrmann7345fa52016-03-21 14:36:21 +0100146 if width > len(buf) {
Martin Möhrmannd38275c2016-03-11 13:29:23 +0100147 buf = make([]byte, width)
148 }
149 }
150
151 // Format into buf, ending at buf[i]. Formatting numbers is easier right-to-left.
152 i := len(buf)
153
154 // For %#U we want to add a space and a quoted character at the end of the buffer.
155 if f.sharp && u <= utf8.MaxRune && strconv.IsPrint(rune(u)) {
156 i--
157 buf[i] = '\''
158 i -= utf8.RuneLen(rune(u))
159 utf8.EncodeRune(buf[i:], rune(u))
160 i--
161 buf[i] = '\''
162 i--
163 buf[i] = ' '
164 }
165 // Format the Unicode code point u as a hexadecimal number.
166 for u >= 16 {
167 i--
168 buf[i] = udigits[u&0xF]
169 prec--
170 u >>= 4
171 }
172 i--
173 buf[i] = udigits[u]
174 prec--
175 // Add zeros in front of the number until requested precision is reached.
176 for prec > 0 {
177 i--
178 buf[i] = '0'
179 prec--
180 }
181 // Add a leading "U+".
182 i--
183 buf[i] = '+'
184 i--
185 buf[i] = 'U'
186
187 oldZero := f.zero
188 f.zero = false
189 f.pad(buf[i:])
190 f.zero = oldZero
191}
192
Martin Möhrmann8d9ece92016-03-18 08:24:40 +0100193// fmt_integer formats signed and unsigned integers.
194func (f *fmt) fmt_integer(u uint64, base int, isSigned bool, digits string) {
Martin Möhrmann8d9ece92016-03-18 08:24:40 +0100195 negative := isSigned && int64(u) < 0
Håvard Haugen70cf7352015-05-25 23:14:35 +0200196 if negative {
Martin Möhrmann8d9ece92016-03-18 08:24:40 +0100197 u = -u
Håvard Haugen70cf7352015-05-25 23:14:35 +0200198 }
199
Martin Möhrmann7345fa52016-03-21 14:36:21 +0100200 buf := f.intbuf[0:]
201 // The already allocated f.intbuf with a capacity of 68 bytes
202 // is large enough for integer formatting when no precision or width is set.
203 if f.widPresent || f.precPresent {
204 // Account 3 extra bytes for possible addition of a sign and "0x".
205 width := 3 + f.wid + f.prec // wid and prec are always positive.
206 if width > len(buf) {
Rob Pikefc908a02014-01-16 09:48:23 -0800207 // We're going to need a bigger boat.
208 buf = make([]byte, width)
209 }
Rob Pikef59064d2013-08-07 08:38:46 +1000210 }
211
Martin Möhrmanna85a2242016-04-16 09:24:43 +0200212 // Two ways to ask for extra leading zero digits: %.3d or %03d.
213 // If both are specified the f.zero flag is ignored and
214 // padding with spaces is used instead.
Robert Griesemer1c729592009-12-15 15:27:16 -0800215 prec := 0
Rob Pike4c0e51c2009-12-06 12:03:52 -0800216 if f.precPresent {
Robert Griesemer1c729592009-12-15 15:27:16 -0800217 prec = f.prec
Martin Möhrmanna85a2242016-04-16 09:24:43 +0200218 // Precision of 0 and value of 0 means "print nothing" but padding.
219 if prec == 0 && u == 0 {
220 oldZero := f.zero
221 f.zero = false
222 f.writePadding(f.wid)
223 f.zero = oldZero
224 return
225 }
Martin Möhrmannd175a852016-03-27 11:50:25 +0200226 } else if f.zero && f.widPresent {
Robert Griesemer1c729592009-12-15 15:27:16 -0800227 prec = f.wid
Russ Cox387df5e2008-11-24 14:51:33 -0800228 if negative || f.plus || f.space {
Robert Griesemer1c729592009-12-15 15:27:16 -0800229 prec-- // leave room for sign
Rob Pike42d78502008-03-27 00:06:21 -0700230 }
231 }
Russ Cox387df5e2008-11-24 14:51:33 -0800232
Martin Möhrmann8d9ece92016-03-18 08:24:40 +0100233 // Because printing is easier right-to-left: format u into buf, ending at buf[i].
234 // We could make things marginally faster by splitting the 32-bit case out
235 // into a separate block but it's not worth the duplication, so u has 64 bits.
Rob Pikef59064d2013-08-07 08:38:46 +1000236 i := len(buf)
Martin Möhrmann8d9ece92016-03-18 08:24:40 +0100237 // Use constants for the division and modulo for more efficient code.
238 // Switch cases ordered by popularity.
Rob Pike78b53212014-09-22 11:58:15 -0700239 switch base {
240 case 10:
Martin Möhrmann8d9ece92016-03-18 08:24:40 +0100241 for u >= 10 {
Rob Pike78b53212014-09-22 11:58:15 -0700242 i--
Martin Möhrmann8d9ece92016-03-18 08:24:40 +0100243 next := u / 10
244 buf[i] = byte('0' + u - next*10)
245 u = next
Rob Pike78b53212014-09-22 11:58:15 -0700246 }
247 case 16:
Martin Möhrmann8d9ece92016-03-18 08:24:40 +0100248 for u >= 16 {
Rob Pike78b53212014-09-22 11:58:15 -0700249 i--
Martin Möhrmann8d9ece92016-03-18 08:24:40 +0100250 buf[i] = digits[u&0xF]
251 u >>= 4
Rob Pike78b53212014-09-22 11:58:15 -0700252 }
253 case 8:
Martin Möhrmann8d9ece92016-03-18 08:24:40 +0100254 for u >= 8 {
Rob Pike78b53212014-09-22 11:58:15 -0700255 i--
Martin Möhrmann8d9ece92016-03-18 08:24:40 +0100256 buf[i] = byte('0' + u&7)
257 u >>= 3
Rob Pike78b53212014-09-22 11:58:15 -0700258 }
259 case 2:
Martin Möhrmann8d9ece92016-03-18 08:24:40 +0100260 for u >= 2 {
Rob Pike78b53212014-09-22 11:58:15 -0700261 i--
Martin Möhrmann8d9ece92016-03-18 08:24:40 +0100262 buf[i] = byte('0' + u&1)
263 u >>= 1
Rob Pike78b53212014-09-22 11:58:15 -0700264 }
265 default:
266 panic("fmt: unknown base; can't happen")
Rob Pikef91cd442009-12-06 15:01:07 -0800267 }
Robert Griesemer1c729592009-12-15 15:27:16 -0800268 i--
Martin Möhrmann8d9ece92016-03-18 08:24:40 +0100269 buf[i] = digits[u]
Rob Pikef59064d2013-08-07 08:38:46 +1000270 for i > 0 && prec > len(buf)-i {
Robert Griesemer1c729592009-12-15 15:27:16 -0800271 i--
272 buf[i] = '0'
Russ Cox387df5e2008-11-24 14:51:33 -0800273 }
274
Rob Pikef91cd442009-12-06 15:01:07 -0800275 // Various prefixes: 0x, -, etc.
Rob Pikec81d09d2009-05-15 15:18:09 -0700276 if f.sharp {
277 switch base {
278 case 8:
Rob Pikef91cd442009-12-06 15:01:07 -0800279 if buf[i] != '0' {
Robert Griesemer1c729592009-12-15 15:27:16 -0800280 i--
281 buf[i] = '0'
Rob Pikec81d09d2009-05-15 15:18:09 -0700282 }
283 case 16:
Martin Möhrmann033e3e12016-02-27 19:47:43 +0100284 // Add a leading 0x or 0X.
Robert Griesemer1c729592009-12-15 15:27:16 -0800285 i--
Martin Möhrmann033e3e12016-02-27 19:47:43 +0100286 buf[i] = digits[16]
Robert Griesemer1c729592009-12-15 15:27:16 -0800287 i--
288 buf[i] = '0'
Rob Pikec81d09d2009-05-15 15:18:09 -0700289 }
290 }
291
Rob Pike42d78502008-03-27 00:06:21 -0700292 if negative {
Robert Griesemer1c729592009-12-15 15:27:16 -0800293 i--
294 buf[i] = '-'
Russ Cox387df5e2008-11-24 14:51:33 -0800295 } else if f.plus {
Robert Griesemer1c729592009-12-15 15:27:16 -0800296 i--
297 buf[i] = '+'
Russ Cox387df5e2008-11-24 14:51:33 -0800298 } else if f.space {
Robert Griesemer1c729592009-12-15 15:27:16 -0800299 i--
300 buf[i] = ' '
Rob Pike42d78502008-03-27 00:06:21 -0700301 }
Rob Piked152fe72011-06-11 00:03:02 +0000302
Martin Möhrmannd175a852016-03-27 11:50:25 +0200303 // Left padding with zeros has already been handled like precision earlier
Martin Möhrmanna85a2242016-04-16 09:24:43 +0200304 // or the f.zero flag is ignored due to an explicitly set precision.
305 oldZero := f.zero
306 f.zero = false
Robert Griesemer1c729592009-12-15 15:27:16 -0800307 f.pad(buf[i:])
Martin Möhrmanna85a2242016-04-16 09:24:43 +0200308 f.zero = oldZero
Rob Pike42d78502008-03-27 00:06:21 -0700309}
310
Rob Pike39070312011-03-31 14:56:01 -0700311// truncate truncates the string to the specified precision, if present.
312func (f *fmt) truncate(s string) string {
Martin Möhrmann83765d12016-03-02 20:31:20 +0100313 if f.precPresent {
Rob Pike39070312011-03-31 14:56:01 -0700314 n := f.prec
315 for i := range s {
Rob Pike39070312011-03-31 14:56:01 -0700316 n--
Martin Möhrmann83765d12016-03-02 20:31:20 +0100317 if n < 0 {
318 return s[:i]
319 }
Rob Pike42d78502008-03-27 00:06:21 -0700320 }
321 }
Rob Pike39070312011-03-31 14:56:01 -0700322 return s
323}
324
325// fmt_s formats a string.
326func (f *fmt) fmt_s(s string) {
327 s = f.truncate(s)
Robert Griesemer1c729592009-12-15 15:27:16 -0800328 f.padString(s)
Rob Pike42d78502008-03-27 00:06:21 -0700329}
330
Rob Pikeffea8352012-09-27 06:21:38 +1000331// fmt_sbx formats a string or byte slice as a hexadecimal encoding of its bytes.
332func (f *fmt) fmt_sbx(s string, b []byte, digits string) {
Martin Möhrmann033e3e12016-02-27 19:47:43 +0100333 length := len(b)
Rob Pikeffea8352012-09-27 06:21:38 +1000334 if b == nil {
Martin Möhrmann033e3e12016-02-27 19:47:43 +0100335 // No byte slice present. Assume string s should be encoded.
336 length = len(s)
Rob Pikeffea8352012-09-27 06:21:38 +1000337 }
Martin Möhrmann033e3e12016-02-27 19:47:43 +0100338 // Set length to not process more bytes than the precision demands.
339 if f.precPresent && f.prec < length {
340 length = f.prec
341 }
342 // Compute width of the encoding taking into account the f.sharp and f.space flag.
343 width := 2 * length
344 if width > 0 {
345 if f.space {
346 // Each element encoded by two hexadecimals will get a leading 0x or 0X.
347 if f.sharp {
348 width *= 2
349 }
350 // Elements will be separated by a space.
351 width += length - 1
352 } else if f.sharp {
353 // Only a leading 0x or 0X will be added for the whole string.
354 width += 2
355 }
356 } else { // The byte slice or string that should be encoded is empty.
357 if f.widPresent {
358 f.writePadding(f.wid)
359 }
360 return
361 }
362 // Handle padding to the left.
363 if f.widPresent && f.wid > width && !f.minus {
364 f.writePadding(f.wid - width)
365 }
366 // Write the encoding directly into the output buffer.
367 buf := *f.buf
368 if f.sharp {
369 // Add leading 0x or 0X.
370 buf = append(buf, '0', digits[16])
371 }
372 var c byte
373 for i := 0; i < length; i++ {
374 if f.space && i > 0 {
375 // Separate elements with a space.
Rob Pikeffea8352012-09-27 06:21:38 +1000376 buf = append(buf, ' ')
Martin Möhrmann033e3e12016-02-27 19:47:43 +0100377 if f.sharp {
378 // Add leading 0x or 0X for each element.
379 buf = append(buf, '0', digits[16])
380 }
Rob Pikeffea8352012-09-27 06:21:38 +1000381 }
Martin Möhrmann033e3e12016-02-27 19:47:43 +0100382 if b != nil {
383 c = b[i] // Take a byte from the input byte slice.
Rob Pikeffea8352012-09-27 06:21:38 +1000384 } else {
Martin Möhrmann033e3e12016-02-27 19:47:43 +0100385 c = s[i] // Take a byte from the input string.
Rob Pikeffea8352012-09-27 06:21:38 +1000386 }
Martin Möhrmann033e3e12016-02-27 19:47:43 +0100387 // Encode each byte as two hexadecimal digits.
Rob Pikeffea8352012-09-27 06:21:38 +1000388 buf = append(buf, digits[c>>4], digits[c&0xF])
389 }
Martin Möhrmann033e3e12016-02-27 19:47:43 +0100390 *f.buf = buf
391 // Handle padding to the right.
392 if f.widPresent && f.wid > width && f.minus {
393 f.writePadding(f.wid - width)
394 }
Rob Pikeffea8352012-09-27 06:21:38 +1000395}
396
Rob Pike353ef802009-12-06 12:58:16 -0800397// fmt_sx formats a string as a hexadecimal encoding of its bytes.
Rob Pike04faa082011-12-15 12:52:29 -0800398func (f *fmt) fmt_sx(s, digits string) {
Rob Pikeffea8352012-09-27 06:21:38 +1000399 f.fmt_sbx(s, nil, digits)
400}
401
402// fmt_bx formats a byte slice as a hexadecimal encoding of its bytes.
403func (f *fmt) fmt_bx(b []byte, digits string) {
404 f.fmt_sbx("", b, digits)
Russ Cox387df5e2008-11-24 14:51:33 -0800405}
406
Rob Pike353ef802009-12-06 12:58:16 -0800407// fmt_q formats a string as a double-quoted, escaped Go string constant.
Martin Möhrmann57634762016-03-05 11:33:13 +0100408// If f.sharp is set a raw (backquoted) string may be returned instead
409// if the string does not contain any control characters other than tab.
Rob Pike353ef802009-12-06 12:58:16 -0800410func (f *fmt) fmt_q(s string) {
Rob Pike39070312011-03-31 14:56:01 -0700411 s = f.truncate(s)
Russ Cox387df5e2008-11-24 14:51:33 -0800412 if f.sharp && strconv.CanBackquote(s) {
Martin Möhrmann57634762016-03-05 11:33:13 +0100413 f.padString("`" + s + "`")
414 return
Russ Cox387df5e2008-11-24 14:51:33 -0800415 }
Martin Möhrmann57634762016-03-05 11:33:13 +0100416 buf := f.intbuf[:0]
417 if f.plus {
418 f.pad(strconv.AppendQuoteToASCII(buf, s))
419 } else {
420 f.pad(strconv.AppendQuote(buf, s))
421 }
Russ Cox387df5e2008-11-24 14:51:33 -0800422}
423
Martin Möhrmann42cd69f2016-03-08 20:13:58 +0100424// fmt_c formats an integer as a Unicode character.
Rob Pike7b03f2a2011-05-25 21:25:15 +1000425// If the character is not valid Unicode, it will print '\ufffd'.
Martin Möhrmann42cd69f2016-03-08 20:13:58 +0100426func (f *fmt) fmt_c(c uint64) {
427 r := rune(c)
428 if c > utf8.MaxRune {
429 r = utf8.RuneError
430 }
431 buf := f.intbuf[:0]
432 w := utf8.EncodeRune(buf[:utf8.UTFMax], r)
433 f.pad(buf[:w])
434}
435
436// fmt_qc formats an integer as a single-quoted, escaped Go character constant.
437// If the character is not valid Unicode, it will print '\ufffd'.
438func (f *fmt) fmt_qc(c uint64) {
439 r := rune(c)
440 if c > utf8.MaxRune {
441 r = utf8.RuneError
442 }
Martin Möhrmann57634762016-03-05 11:33:13 +0100443 buf := f.intbuf[:0]
Rob Piked152fe72011-06-11 00:03:02 +0000444 if f.plus {
Martin Möhrmann42cd69f2016-03-08 20:13:58 +0100445 f.pad(strconv.AppendQuoteRuneToASCII(buf, r))
Rob Piked152fe72011-06-11 00:03:02 +0000446 } else {
Martin Möhrmann42cd69f2016-03-08 20:13:58 +0100447 f.pad(strconv.AppendQuoteRune(buf, r))
Rob Piked152fe72011-06-11 00:03:02 +0000448 }
Rob Pike7b03f2a2011-05-25 21:25:15 +1000449}
450
Martin Möhrmannbd6d8422016-02-27 14:37:10 +0100451// fmt_float formats a float64. It assumes that verb is a valid format specifier
452// for strconv.AppendFloat and therefore fits into a byte.
453func (f *fmt) fmt_float(v float64, size int, verb rune, prec int) {
454 // Explicit precision in format specifier overrules default precision.
Rob Pike4c0e51c2009-12-06 12:03:52 -0800455 if f.precPresent {
Martin Möhrmannbd6d8422016-02-27 14:37:10 +0100456 prec = f.prec
Rob Pike42d78502008-03-27 00:06:21 -0700457 }
Rob Pike4464ae22014-05-21 12:30:43 -0700458 // Format number, reserving space for leading + sign if needed.
Martin Möhrmannbd6d8422016-02-27 14:37:10 +0100459 num := strconv.AppendFloat(f.intbuf[:1], v, byte(verb), prec, size)
Rob Pike4464ae22014-05-21 12:30:43 -0700460 if num[1] == '-' || num[1] == '+' {
461 num = num[1:]
462 } else {
463 num[0] = '+'
Rob Pike542e5b82009-11-20 11:04:51 -0800464 }
Martin Möhrmann75cc05f2016-02-27 12:19:49 +0100465 // f.space means to add a leading space instead of a "+" sign unless
466 // the sign is explicitly asked for by f.plus.
467 if f.space && num[0] == '+' && !f.plus {
Rob Pike4464ae22014-05-21 12:30:43 -0700468 num[0] = ' '
Martin Möhrmann5a9c1282016-02-19 22:45:38 +0100469 }
Martin Möhrmann5dc053b2016-02-21 10:46:59 +0100470 // Special handling for infinities and NaN,
Martin Möhrmann5a9c1282016-02-19 22:45:38 +0100471 // which don't look like a number so shouldn't be padded with zeros.
Martin Möhrmann5dc053b2016-02-21 10:46:59 +0100472 if num[1] == 'I' || num[1] == 'N' {
Martin Möhrmann5a9c1282016-02-19 22:45:38 +0100473 oldZero := f.zero
474 f.zero = false
Martin Möhrmann5dc053b2016-02-21 10:46:59 +0100475 // Remove sign before NaN if not asked for.
476 if num[1] == 'N' && !f.space && !f.plus {
477 num = num[1:]
478 }
Rob Pike4464ae22014-05-21 12:30:43 -0700479 f.pad(num)
Martin Möhrmann5a9c1282016-02-19 22:45:38 +0100480 f.zero = oldZero
Rob Pike4464ae22014-05-21 12:30:43 -0700481 return
482 }
Martin Möhrmanne97f4072017-02-15 12:41:02 +0100483 // The sharp flag forces printing a decimal point for non-binary formats
484 // and retains trailing zeros, which we may need to restore.
485 if f.sharp && verb != 'b' {
486 digits := 0
487 switch verb {
488 case 'v', 'g', 'G':
489 digits = prec
490 // If no precision is set explicitly use a precision of 6.
491 if digits == -1 {
492 digits = 6
493 }
494 }
495
496 // Buffer pre-allocated with enough room for
497 // exponent notations of the form "e+123".
498 var tailBuf [5]byte
499 tail := tailBuf[:0]
500
501 hasDecimalPoint := false
502 // Starting from i = 1 to skip sign at num[0].
503 for i := 1; i < len(num); i++ {
504 switch num[i] {
505 case '.':
506 hasDecimalPoint = true
507 case 'e', 'E':
508 tail = append(tail, num[i:]...)
509 num = num[:i]
510 default:
511 digits--
512 }
513 }
514 if !hasDecimalPoint {
515 num = append(num, '.')
516 }
517 for digits > 0 {
518 num = append(num, '0')
519 digits--
520 }
521 num = append(num, tail...)
522 }
Martin Möhrmann5a9c1282016-02-19 22:45:38 +0100523 // We want a sign if asked for and if the sign is not positive.
524 if f.plus || num[0] != '+' {
Martin Möhrmannd175a852016-03-27 11:50:25 +0200525 // If we're zero padding to the left we want the sign before the leading zeros.
Martin Möhrmann5a9c1282016-02-19 22:45:38 +0100526 // Achieve this by writing the sign out and then padding the unsigned number.
527 if f.zero && f.widPresent && f.wid > len(num) {
528 f.buf.WriteByte(num[0])
Martin Möhrmannd175a852016-03-27 11:50:25 +0200529 f.writePadding(f.wid - len(num))
530 f.buf.Write(num[1:])
Martin Möhrmann78374182016-03-04 15:52:35 +0100531 return
Martin Möhrmann5a9c1282016-02-19 22:45:38 +0100532 }
Rob Pike4464ae22014-05-21 12:30:43 -0700533 f.pad(num)
534 return
535 }
536 // No sign to show and the number is positive; just print the unsigned number.
537 f.pad(num[1:])
Rob Pike542e5b82009-11-20 11:04:51 -0800538}