blob: 4d12f82f7dea84a14d0edd574150115f2c80c64e [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 {
Russ Cox50a1d892019-05-08 18:47:32 -040092 f.buf.write(b)
Rob Pike53bc1942012-05-29 15:08:08 -070093 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)
Russ Cox50a1d892019-05-08 18:47:32 -040099 f.buf.write(b)
Martin Möhrmannabcad1e2016-02-20 01:51:32 +0100100 } else {
101 // right padding
Russ Cox50a1d892019-05-08 18:47:32 -0400102 f.buf.write(b)
Martin Möhrmannabcad1e2016-02-20 01:51:32 +0100103 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 {
Russ Cox50a1d892019-05-08 18:47:32 -0400110 f.buf.writeString(s)
Rob Pike53bc1942012-05-29 15:08:08 -0700111 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)
Russ Cox50a1d892019-05-08 18:47:32 -0400117 f.buf.writeString(s)
Martin Möhrmannabcad1e2016-02-20 01:51:32 +0100118 } else {
119 // right padding
Russ Cox50a1d892019-05-08 18:47:32 -0400120 f.buf.writeString(s)
Martin Möhrmannabcad1e2016-02-20 01:51:32 +0100121 f.writePadding(width)
Rob Pike4c0e51c2009-12-06 12:03:52 -0800122 }
Rob Pike42d78502008-03-27 00:06:21 -0700123}
124
Darshan Parajuliccaa2bc2018-02-27 03:01:01 -0800125// fmtBoolean formats a boolean.
126func (f *fmt) fmtBoolean(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
Darshan Parajuliccaa2bc2018-02-27 03:01:01 -0800134// fmtUnicode formats a uint64 as "U+0078" or with f.sharp set as "U+0078 'x'".
135func (f *fmt) fmtUnicode(u uint64) {
Martin Möhrmannd38275c2016-03-11 13:29:23 +0100136 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
Darshan Parajuliccaa2bc2018-02-27 03:01:01 -0800193// fmtInteger formats signed and unsigned integers.
Russ Coxac512372019-01-29 22:24:36 -0500194func (f *fmt) fmtInteger(u uint64, base int, isSigned bool, verb rune, 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 {
Russ Coxac512372019-01-29 22:24:36 -0500278 case 2:
279 // Add a leading 0b.
280 i--
281 buf[i] = 'b'
282 i--
283 buf[i] = '0'
Rob Pikec81d09d2009-05-15 15:18:09 -0700284 case 8:
Rob Pikef91cd442009-12-06 15:01:07 -0800285 if buf[i] != '0' {
Robert Griesemer1c729592009-12-15 15:27:16 -0800286 i--
287 buf[i] = '0'
Rob Pikec81d09d2009-05-15 15:18:09 -0700288 }
289 case 16:
Martin Möhrmann033e3e12016-02-27 19:47:43 +0100290 // Add a leading 0x or 0X.
Robert Griesemer1c729592009-12-15 15:27:16 -0800291 i--
Martin Möhrmann033e3e12016-02-27 19:47:43 +0100292 buf[i] = digits[16]
Robert Griesemer1c729592009-12-15 15:27:16 -0800293 i--
294 buf[i] = '0'
Rob Pikec81d09d2009-05-15 15:18:09 -0700295 }
296 }
Russ Coxac512372019-01-29 22:24:36 -0500297 if verb == 'O' {
298 i--
299 buf[i] = 'o'
300 i--
301 buf[i] = '0'
302 }
Rob Pikec81d09d2009-05-15 15:18:09 -0700303
Rob Pike42d78502008-03-27 00:06:21 -0700304 if negative {
Robert Griesemer1c729592009-12-15 15:27:16 -0800305 i--
306 buf[i] = '-'
Russ Cox387df5e2008-11-24 14:51:33 -0800307 } else if f.plus {
Robert Griesemer1c729592009-12-15 15:27:16 -0800308 i--
309 buf[i] = '+'
Russ Cox387df5e2008-11-24 14:51:33 -0800310 } else if f.space {
Robert Griesemer1c729592009-12-15 15:27:16 -0800311 i--
312 buf[i] = ' '
Rob Pike42d78502008-03-27 00:06:21 -0700313 }
Rob Piked152fe72011-06-11 00:03:02 +0000314
Martin Möhrmannd175a852016-03-27 11:50:25 +0200315 // Left padding with zeros has already been handled like precision earlier
Martin Möhrmanna85a2242016-04-16 09:24:43 +0200316 // or the f.zero flag is ignored due to an explicitly set precision.
317 oldZero := f.zero
318 f.zero = false
Robert Griesemer1c729592009-12-15 15:27:16 -0800319 f.pad(buf[i:])
Martin Möhrmanna85a2242016-04-16 09:24:43 +0200320 f.zero = oldZero
Rob Pike42d78502008-03-27 00:06:21 -0700321}
322
Martin Möhrmannf36e92d2018-10-28 17:28:04 +0100323// truncate truncates the string s to the specified precision, if present.
324func (f *fmt) truncateString(s string) string {
Martin Möhrmann83765d12016-03-02 20:31:20 +0100325 if f.precPresent {
Rob Pike39070312011-03-31 14:56:01 -0700326 n := f.prec
327 for i := range s {
Rob Pike39070312011-03-31 14:56:01 -0700328 n--
Martin Möhrmann83765d12016-03-02 20:31:20 +0100329 if n < 0 {
330 return s[:i]
331 }
Rob Pike42d78502008-03-27 00:06:21 -0700332 }
333 }
Rob Pike39070312011-03-31 14:56:01 -0700334 return s
335}
336
Martin Möhrmannf36e92d2018-10-28 17:28:04 +0100337// truncate truncates the byte slice b as a string of the specified precision, if present.
338func (f *fmt) truncate(b []byte) []byte {
339 if f.precPresent {
340 n := f.prec
341 for i := 0; i < len(b); {
342 n--
343 if n < 0 {
344 return b[:i]
345 }
346 wid := 1
347 if b[i] >= utf8.RuneSelf {
348 _, wid = utf8.DecodeRune(b[i:])
349 }
350 i += wid
351 }
352 }
353 return b
354}
355
Darshan Parajuliccaa2bc2018-02-27 03:01:01 -0800356// fmtS formats a string.
357func (f *fmt) fmtS(s string) {
Martin Möhrmannf36e92d2018-10-28 17:28:04 +0100358 s = f.truncateString(s)
Robert Griesemer1c729592009-12-15 15:27:16 -0800359 f.padString(s)
Rob Pike42d78502008-03-27 00:06:21 -0700360}
361
Martin Möhrmannf36e92d2018-10-28 17:28:04 +0100362// fmtBs formats the byte slice b as if it was formatted as string with fmtS.
363func (f *fmt) fmtBs(b []byte) {
364 b = f.truncate(b)
365 f.pad(b)
366}
367
Darshan Parajuliccaa2bc2018-02-27 03:01:01 -0800368// fmtSbx formats a string or byte slice as a hexadecimal encoding of its bytes.
369func (f *fmt) fmtSbx(s string, b []byte, digits string) {
Martin Möhrmann033e3e12016-02-27 19:47:43 +0100370 length := len(b)
Rob Pikeffea8352012-09-27 06:21:38 +1000371 if b == nil {
Martin Möhrmann033e3e12016-02-27 19:47:43 +0100372 // No byte slice present. Assume string s should be encoded.
373 length = len(s)
Rob Pikeffea8352012-09-27 06:21:38 +1000374 }
Martin Möhrmann033e3e12016-02-27 19:47:43 +0100375 // Set length to not process more bytes than the precision demands.
376 if f.precPresent && f.prec < length {
377 length = f.prec
378 }
379 // Compute width of the encoding taking into account the f.sharp and f.space flag.
380 width := 2 * length
381 if width > 0 {
382 if f.space {
383 // Each element encoded by two hexadecimals will get a leading 0x or 0X.
384 if f.sharp {
385 width *= 2
386 }
387 // Elements will be separated by a space.
388 width += length - 1
389 } else if f.sharp {
390 // Only a leading 0x or 0X will be added for the whole string.
391 width += 2
392 }
393 } else { // The byte slice or string that should be encoded is empty.
394 if f.widPresent {
395 f.writePadding(f.wid)
396 }
397 return
398 }
399 // Handle padding to the left.
400 if f.widPresent && f.wid > width && !f.minus {
401 f.writePadding(f.wid - width)
402 }
403 // Write the encoding directly into the output buffer.
404 buf := *f.buf
405 if f.sharp {
406 // Add leading 0x or 0X.
407 buf = append(buf, '0', digits[16])
408 }
409 var c byte
410 for i := 0; i < length; i++ {
411 if f.space && i > 0 {
412 // Separate elements with a space.
Rob Pikeffea8352012-09-27 06:21:38 +1000413 buf = append(buf, ' ')
Martin Möhrmann033e3e12016-02-27 19:47:43 +0100414 if f.sharp {
415 // Add leading 0x or 0X for each element.
416 buf = append(buf, '0', digits[16])
417 }
Rob Pikeffea8352012-09-27 06:21:38 +1000418 }
Martin Möhrmann033e3e12016-02-27 19:47:43 +0100419 if b != nil {
420 c = b[i] // Take a byte from the input byte slice.
Rob Pikeffea8352012-09-27 06:21:38 +1000421 } else {
Martin Möhrmann033e3e12016-02-27 19:47:43 +0100422 c = s[i] // Take a byte from the input string.
Rob Pikeffea8352012-09-27 06:21:38 +1000423 }
Martin Möhrmann033e3e12016-02-27 19:47:43 +0100424 // Encode each byte as two hexadecimal digits.
Rob Pikeffea8352012-09-27 06:21:38 +1000425 buf = append(buf, digits[c>>4], digits[c&0xF])
426 }
Martin Möhrmann033e3e12016-02-27 19:47:43 +0100427 *f.buf = buf
428 // Handle padding to the right.
429 if f.widPresent && f.wid > width && f.minus {
430 f.writePadding(f.wid - width)
431 }
Rob Pikeffea8352012-09-27 06:21:38 +1000432}
433
Darshan Parajuliccaa2bc2018-02-27 03:01:01 -0800434// fmtSx formats a string as a hexadecimal encoding of its bytes.
435func (f *fmt) fmtSx(s, digits string) {
436 f.fmtSbx(s, nil, digits)
Rob Pikeffea8352012-09-27 06:21:38 +1000437}
438
Darshan Parajuliccaa2bc2018-02-27 03:01:01 -0800439// fmtBx formats a byte slice as a hexadecimal encoding of its bytes.
440func (f *fmt) fmtBx(b []byte, digits string) {
441 f.fmtSbx("", b, digits)
Russ Cox387df5e2008-11-24 14:51:33 -0800442}
443
Darshan Parajuliccaa2bc2018-02-27 03:01:01 -0800444// fmtQ formats a string as a double-quoted, escaped Go string constant.
Martin Möhrmann57634762016-03-05 11:33:13 +0100445// If f.sharp is set a raw (backquoted) string may be returned instead
446// if the string does not contain any control characters other than tab.
Darshan Parajuliccaa2bc2018-02-27 03:01:01 -0800447func (f *fmt) fmtQ(s string) {
Martin Möhrmannf36e92d2018-10-28 17:28:04 +0100448 s = f.truncateString(s)
Russ Cox387df5e2008-11-24 14:51:33 -0800449 if f.sharp && strconv.CanBackquote(s) {
Martin Möhrmann57634762016-03-05 11:33:13 +0100450 f.padString("`" + s + "`")
451 return
Russ Cox387df5e2008-11-24 14:51:33 -0800452 }
Martin Möhrmann57634762016-03-05 11:33:13 +0100453 buf := f.intbuf[:0]
454 if f.plus {
455 f.pad(strconv.AppendQuoteToASCII(buf, s))
456 } else {
457 f.pad(strconv.AppendQuote(buf, s))
458 }
Russ Cox387df5e2008-11-24 14:51:33 -0800459}
460
Darshan Parajuliccaa2bc2018-02-27 03:01:01 -0800461// fmtC formats an integer as a Unicode character.
Rob Pike7b03f2a2011-05-25 21:25:15 +1000462// If the character is not valid Unicode, it will print '\ufffd'.
Darshan Parajuliccaa2bc2018-02-27 03:01:01 -0800463func (f *fmt) fmtC(c uint64) {
Martin Möhrmann42cd69f2016-03-08 20:13:58 +0100464 r := rune(c)
465 if c > utf8.MaxRune {
466 r = utf8.RuneError
467 }
468 buf := f.intbuf[:0]
469 w := utf8.EncodeRune(buf[:utf8.UTFMax], r)
470 f.pad(buf[:w])
471}
472
Darshan Parajuliccaa2bc2018-02-27 03:01:01 -0800473// fmtQc formats an integer as a single-quoted, escaped Go character constant.
Martin Möhrmann42cd69f2016-03-08 20:13:58 +0100474// If the character is not valid Unicode, it will print '\ufffd'.
Darshan Parajuliccaa2bc2018-02-27 03:01:01 -0800475func (f *fmt) fmtQc(c uint64) {
Martin Möhrmann42cd69f2016-03-08 20:13:58 +0100476 r := rune(c)
477 if c > utf8.MaxRune {
478 r = utf8.RuneError
479 }
Martin Möhrmann57634762016-03-05 11:33:13 +0100480 buf := f.intbuf[:0]
Rob Piked152fe72011-06-11 00:03:02 +0000481 if f.plus {
Martin Möhrmann42cd69f2016-03-08 20:13:58 +0100482 f.pad(strconv.AppendQuoteRuneToASCII(buf, r))
Rob Piked152fe72011-06-11 00:03:02 +0000483 } else {
Martin Möhrmann42cd69f2016-03-08 20:13:58 +0100484 f.pad(strconv.AppendQuoteRune(buf, r))
Rob Piked152fe72011-06-11 00:03:02 +0000485 }
Rob Pike7b03f2a2011-05-25 21:25:15 +1000486}
487
Darshan Parajuliccaa2bc2018-02-27 03:01:01 -0800488// fmtFloat formats a float64. It assumes that verb is a valid format specifier
Martin Möhrmannbd6d8422016-02-27 14:37:10 +0100489// for strconv.AppendFloat and therefore fits into a byte.
Darshan Parajuliccaa2bc2018-02-27 03:01:01 -0800490func (f *fmt) fmtFloat(v float64, size int, verb rune, prec int) {
Martin Möhrmannbd6d8422016-02-27 14:37:10 +0100491 // Explicit precision in format specifier overrules default precision.
Rob Pike4c0e51c2009-12-06 12:03:52 -0800492 if f.precPresent {
Martin Möhrmannbd6d8422016-02-27 14:37:10 +0100493 prec = f.prec
Rob Pike42d78502008-03-27 00:06:21 -0700494 }
Rob Pike4464ae22014-05-21 12:30:43 -0700495 // Format number, reserving space for leading + sign if needed.
Martin Möhrmannbd6d8422016-02-27 14:37:10 +0100496 num := strconv.AppendFloat(f.intbuf[:1], v, byte(verb), prec, size)
Rob Pike4464ae22014-05-21 12:30:43 -0700497 if num[1] == '-' || num[1] == '+' {
498 num = num[1:]
499 } else {
500 num[0] = '+'
Rob Pike542e5b82009-11-20 11:04:51 -0800501 }
Martin Möhrmann75cc05f2016-02-27 12:19:49 +0100502 // f.space means to add a leading space instead of a "+" sign unless
503 // the sign is explicitly asked for by f.plus.
504 if f.space && num[0] == '+' && !f.plus {
Rob Pike4464ae22014-05-21 12:30:43 -0700505 num[0] = ' '
Martin Möhrmann5a9c1282016-02-19 22:45:38 +0100506 }
Martin Möhrmann5dc053b2016-02-21 10:46:59 +0100507 // Special handling for infinities and NaN,
Martin Möhrmann5a9c1282016-02-19 22:45:38 +0100508 // which don't look like a number so shouldn't be padded with zeros.
Martin Möhrmann5dc053b2016-02-21 10:46:59 +0100509 if num[1] == 'I' || num[1] == 'N' {
Martin Möhrmann5a9c1282016-02-19 22:45:38 +0100510 oldZero := f.zero
511 f.zero = false
Martin Möhrmann5dc053b2016-02-21 10:46:59 +0100512 // Remove sign before NaN if not asked for.
513 if num[1] == 'N' && !f.space && !f.plus {
514 num = num[1:]
515 }
Rob Pike4464ae22014-05-21 12:30:43 -0700516 f.pad(num)
Martin Möhrmann5a9c1282016-02-19 22:45:38 +0100517 f.zero = oldZero
Rob Pike4464ae22014-05-21 12:30:43 -0700518 return
519 }
Martin Möhrmanne97f4072017-02-15 12:41:02 +0100520 // The sharp flag forces printing a decimal point for non-binary formats
Filippo Valsordaa52289e2018-10-16 13:01:07 -0400521 // and retains trailing zeros, which we may need to restore.
522 if f.sharp && verb != 'b' {
Martin Möhrmanne97f4072017-02-15 12:41:02 +0100523 digits := 0
Filippo Valsordaa52289e2018-10-16 13:01:07 -0400524 switch verb {
Russ Coxe1a6d1f2019-01-29 22:13:54 -0500525 case 'v', 'g', 'G', 'x':
Filippo Valsordaa52289e2018-10-16 13:01:07 -0400526 digits = prec
527 // If no precision is set explicitly use a precision of 6.
528 if digits == -1 {
529 digits = 6
Martin Möhrmanne97f4072017-02-15 12:41:02 +0100530 }
531 }
532
533 // Buffer pre-allocated with enough room for
Russ Coxe1a6d1f2019-01-29 22:13:54 -0500534 // exponent notations of the form "e+123" or "p-1023".
535 var tailBuf [6]byte
Martin Möhrmanne97f4072017-02-15 12:41:02 +0100536 tail := tailBuf[:0]
537
Filippo Valsordaa52289e2018-10-16 13:01:07 -0400538 hasDecimalPoint := false
yah0142b93b72020-02-26 08:17:14 +0000539 sawNonzeroDigit := false
Martin Möhrmanne97f4072017-02-15 12:41:02 +0100540 // Starting from i = 1 to skip sign at num[0].
541 for i := 1; i < len(num); i++ {
542 switch num[i] {
543 case '.':
544 hasDecimalPoint = true
Russ Coxe1a6d1f2019-01-29 22:13:54 -0500545 case 'p', 'P':
Martin Möhrmanne97f4072017-02-15 12:41:02 +0100546 tail = append(tail, num[i:]...)
547 num = num[:i]
Russ Coxe1a6d1f2019-01-29 22:13:54 -0500548 case 'e', 'E':
549 if verb != 'x' && verb != 'X' {
550 tail = append(tail, num[i:]...)
551 num = num[:i]
552 break
553 }
554 fallthrough
Martin Möhrmanne97f4072017-02-15 12:41:02 +0100555 default:
yah0142b93b72020-02-26 08:17:14 +0000556 if num[i] != '0' {
557 sawNonzeroDigit = true
558 }
559 // Count significant digits after the first non-zero digit.
560 if sawNonzeroDigit {
561 digits--
562 }
Martin Möhrmanne97f4072017-02-15 12:41:02 +0100563 }
564 }
Filippo Valsordaa52289e2018-10-16 13:01:07 -0400565 if !hasDecimalPoint {
yah0142b93b72020-02-26 08:17:14 +0000566 // Leading digit 0 should contribute once to digits.
567 if len(num) == 2 && num[1] == '0' {
568 digits--
569 }
Filippo Valsordaa52289e2018-10-16 13:01:07 -0400570 num = append(num, '.')
571 }
572 for digits > 0 {
573 num = append(num, '0')
574 digits--
Martin Möhrmanne97f4072017-02-15 12:41:02 +0100575 }
576 num = append(num, tail...)
577 }
Martin Möhrmann5a9c1282016-02-19 22:45:38 +0100578 // We want a sign if asked for and if the sign is not positive.
579 if f.plus || num[0] != '+' {
Martin Möhrmannd175a852016-03-27 11:50:25 +0200580 // If we're zero padding to the left we want the sign before the leading zeros.
Martin Möhrmann5a9c1282016-02-19 22:45:38 +0100581 // Achieve this by writing the sign out and then padding the unsigned number.
582 if f.zero && f.widPresent && f.wid > len(num) {
Russ Cox50a1d892019-05-08 18:47:32 -0400583 f.buf.writeByte(num[0])
Martin Möhrmannd175a852016-03-27 11:50:25 +0200584 f.writePadding(f.wid - len(num))
Russ Cox50a1d892019-05-08 18:47:32 -0400585 f.buf.write(num[1:])
Martin Möhrmann78374182016-03-04 15:52:35 +0100586 return
Martin Möhrmann5a9c1282016-02-19 22:45:38 +0100587 }
Rob Pike4464ae22014-05-21 12:30:43 -0700588 f.pad(num)
589 return
590 }
591 // No sign to show and the number is positive; just print the unsigned number.
592 f.pad(num[1:])
Rob Pike542e5b82009-11-20 11:04:51 -0800593}