Debian Codegraph @

src/fmt/format.go

raw
1 // 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
5 package fmt
6
7 import (
8 "strconv"
9 "unicode/utf8"
10 )
11
12 const (
13 ldigits = "0123456789abcdefx"
14 udigits = "0123456789ABCDEFX"
15 )
16
17 const (
18 signed = true
19 unsigned = false
20 )
21
22 // flags placed in a separate struct for easy clearing.
23 type fmtFlags struct {
24 widPresent bool
25 precPresent bool
26 minus bool
27 plus bool
28 sharp bool
29 space bool
30 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
39 // A fmt is the raw formatter used by Printf etc.
40 // It prints into a buffer that must be set up separately.
41 type fmt struct {
42 buf *buffer
43
44 fmtFlags
45
46 wid int // width
47 prec int // precision
48
49 // intbuf is large enough to store %b of an int64 with a sign and
50 // avoids padding at the end of the struct on 32 bit architectures.
51 intbuf [68]byte
52 }
53
54 func (f *fmt) clearflags() {
55 f.fmtFlags = fmtFlags{}
56 f.wid = 0
57 f.prec = 0
58 }
59
60 func (f *fmt) init(buf *buffer) {
61 f.buf = buf
62 f.clearflags()
63 }
64
65 // writePadding generates n bytes of padding.
66 func (f *fmt) writePadding(n int) {
67 if n <= 0 { // No padding bytes needed.
68 return
69 }
70 buf := *f.buf
71 oldLen := len(buf)
72 newLen := oldLen + n
73 // Make enough room for padding.
74 if newLen > cap(buf) {
75 buf = make(buffer, cap(buf)*2+n)
76 copy(buf, *f.buf)
77 }
78 // Decide which byte the padding should be filled with.
79 padByte := byte(' ')
80 // Zero padding is allowed only to the left.
81 if f.zero && !f.minus {
82 padByte = byte('0')
83 }
84 // Fill padding with padByte.
85 padding := buf[oldLen:newLen]
86 for i := range padding {
87 padding[i] = padByte
88 }
89 *f.buf = buf[:newLen]
90 }
91
92 // pad appends b to f.buf, padded on left (!f.minus) or right (f.minus).
93 func (f *fmt) pad(b []byte) {
94 if !f.widPresent || f.wid == 0 {
95 f.buf.write(b)
96 return
97 }
98 width := f.wid - utf8.RuneCount(b)
99 if !f.minus {
100 // left padding
101 f.writePadding(width)
102 f.buf.write(b)
103 } else {
104 // right padding
105 f.buf.write(b)
106 f.writePadding(width)
107 }
108 }
109
110 // padString appends s to f.buf, padded on left (!f.minus) or right (f.minus).
111 func (f *fmt) padString(s string) {
112 if !f.widPresent || f.wid == 0 {
113 f.buf.writeString(s)
114 return
115 }
116 width := f.wid - utf8.RuneCountInString(s)
117 if !f.minus {
118 // left padding
119 f.writePadding(width)
120 f.buf.writeString(s)
121 } else {
122 // right padding
123 f.buf.writeString(s)
124 f.writePadding(width)
125 }
126 }
127
128 // fmtBoolean formats a boolean.
129 func (f *fmt) fmtBoolean(v bool) {
130 if v {
131 f.padString("true")
132 } else {
133 f.padString("false")
134 }
135 }
136
137 // fmtUnicode formats a uint64 as "U+0078" or with f.sharp set as "U+0078 'x'".
138 func (f *fmt) fmtUnicode(u uint64) {
139 buf := f.intbuf[0:]
140
141 // With default precision set the maximum needed buf length is 18
142 // for formatting -1 with %#U ("U+FFFFFFFFFFFFFFFF") which fits
143 // into the already allocated intbuf with a capacity of 68 bytes.
144 prec := 4
145 if f.precPresent && f.prec > 4 {
146 prec = f.prec
147 // Compute space needed for "U+" , number, " '", character, "'".
148 width := 2 + prec + 2 + utf8.UTFMax + 1
149 if width > len(buf) {
150 buf = make([]byte, width)
151 }
152 }
153
154 // Format into buf, ending at buf[i]. Formatting numbers is easier right-to-left.
155 i := len(buf)
156
157 // For %#U we want to add a space and a quoted character at the end of the buffer.
158 if f.sharp && u <= utf8.MaxRune && strconv.IsPrint(rune(u)) {
159 i--
160 buf[i] = '\''
161 i -= utf8.RuneLen(rune(u))
162 utf8.EncodeRune(buf[i:], rune(u))
163 i--
164 buf[i] = '\''
165 i--
166 buf[i] = ' '
167 }
168 // Format the Unicode code point u as a hexadecimal number.
169 for u >= 16 {
170 i--
171 buf[i] = udigits[u&0xF]
172 prec--
173 u >>= 4
174 }
175 i--
176 buf[i] = udigits[u]
177 prec--
178 // Add zeros in front of the number until requested precision is reached.
179 for prec > 0 {
180 i--
181 buf[i] = '0'
182 prec--
183 }
184 // Add a leading "U+".
185 i--
186 buf[i] = '+'
187 i--
188 buf[i] = 'U'
189
190 oldZero := f.zero
191 f.zero = false
192 f.pad(buf[i:])
193 f.zero = oldZero
194 }
195
196 // fmtInteger formats signed and unsigned integers.
197 func (f *fmt) fmtInteger(u uint64, base int, isSigned bool, verb rune, digits string) {
198 negative := isSigned && int64(u) < 0
199 if negative {
200 u = -u
201 }
202
203 buf := f.intbuf[0:]
204 // The already allocated f.intbuf with a capacity of 68 bytes
205 // is large enough for integer formatting when no precision or width is set.
206 if f.widPresent || f.precPresent {
207 // Account 3 extra bytes for possible addition of a sign and "0x".
208 width := 3 + f.wid + f.prec // wid and prec are always positive.
209 if width > len(buf) {
210 // We're going to need a bigger boat.
211 buf = make([]byte, width)
212 }
213 }
214
215 // Two ways to ask for extra leading zero digits: %.3d or %03d.
216 // If both are specified the f.zero flag is ignored and
217 // padding with spaces is used instead.
218 prec := 0
219 if f.precPresent {
220 prec = f.prec
221 // Precision of 0 and value of 0 means "print nothing" but padding.
222 if prec == 0 && u == 0 {
223 oldZero := f.zero
224 f.zero = false
225 f.writePadding(f.wid)
226 f.zero = oldZero
227 return
228 }
229 } else if f.zero && !f.minus && f.widPresent { // Zero padding is allowed only to the left.
230 prec = f.wid
231 if negative || f.plus || f.space {
232 prec-- // leave room for sign
233 }
234 }
235
236 // Because printing is easier right-to-left: format u into buf, ending at buf[i].
237 // We could make things marginally faster by splitting the 32-bit case out
238 // into a separate block but it's not worth the duplication, so u has 64 bits.
239 i := len(buf)
240 // Use constants for the division and modulo for more efficient code.
241 // Switch cases ordered by popularity.
242 switch base {
243 case 10:
244 for u >= 10 {
245 i--
246 next := u / 10
247 buf[i] = byte('0' + u - next*10)
248 u = next
249 }
250 case 16:
251 for u >= 16 {
252 i--
253 buf[i] = digits[u&0xF]
254 u >>= 4
255 }
256 case 8:
257 for u >= 8 {
258 i--
259 buf[i] = byte('0' + u&7)
260 u >>= 3
261 }
262 case 2:
263 for u >= 2 {
264 i--
265 buf[i] = byte('0' + u&1)
266 u >>= 1
267 }
268 default:
269 panic("fmt: unknown base; can't happen")
270 }
271 i--
272 buf[i] = digits[u]
273 for i > 0 && prec > len(buf)-i {
274 i--
275 buf[i] = '0'
276 }
277
278 // Various prefixes: 0x, -, etc.
279 if f.sharp {
280 switch base {
281 case 2:
282 // Add a leading 0b.
283 i--
284 buf[i] = 'b'
285 i--
286 buf[i] = '0'
287 case 8:
288 if buf[i] != '0' {
289 i--
290 buf[i] = '0'
291 }
292 case 16:
293 // Add a leading 0x or 0X.
294 i--
295 buf[i] = digits[16]
296 i--
297 buf[i] = '0'
298 }
299 }
300 if verb == 'O' {
301 i--
302 buf[i] = 'o'
303 i--
304 buf[i] = '0'
305 }
306
307 if negative {
308 i--
309 buf[i] = '-'
310 } else if f.plus {
311 i--
312 buf[i] = '+'
313 } else if f.space {
314 i--
315 buf[i] = ' '
316 }
317
318 // Left padding with zeros has already been handled like precision earlier
319 // or the f.zero flag is ignored due to an explicitly set precision.
320 oldZero := f.zero
321 f.zero = false
322 f.pad(buf[i:])
323 f.zero = oldZero
324 }
325
326 // truncateString truncates the string s to the specified precision, if present.
327 func (f *fmt) truncateString(s string) string {
328 if f.precPresent {
329 n := f.prec
330 for i := range s {
331 n--
332 if n < 0 {
333 return s[:i]
334 }
335 }
336 }
337 return s
338 }
339
340 // truncate truncates the byte slice b as a string of the specified precision, if present.
341 func (f *fmt) truncate(b []byte) []byte {
342 if f.precPresent {
343 n := f.prec
344 for i := 0; i < len(b); {
345 n--
346 if n < 0 {
347 return b[:i]
348 }
349 _, wid := utf8.DecodeRune(b[i:])
350 i += wid
351 }
352 }
353 return b
354 }
355
356 // fmtS formats a string.
357 func (f *fmt) fmtS(s string) {
358 s = f.truncateString(s)
359 f.padString(s)
360 }
361
362 // fmtBs formats the byte slice b as if it was formatted as string with fmtS.
363 func (f *fmt) fmtBs(b []byte) {
364 b = f.truncate(b)
365 f.pad(b)
366 }
367
368 // fmtSbx formats a string or byte slice as a hexadecimal encoding of its bytes.
369 func (f *fmt) fmtSbx(s string, b []byte, digits string) {
370 length := len(b)
371 if b == nil {
372 // No byte slice present. Assume string s should be encoded.
373 length = len(s)
374 }
375 // 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.
413 buf = append(buf, ' ')
414 if f.sharp {
415 // Add leading 0x or 0X for each element.
416 buf = append(buf, '0', digits[16])
417 }
418 }
419 if b != nil {
420 c = b[i] // Take a byte from the input byte slice.
421 } else {
422 c = s[i] // Take a byte from the input string.
423 }
424 // Encode each byte as two hexadecimal digits.
425 buf = append(buf, digits[c>>4], digits[c&0xF])
426 }
427 *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 }
432 }
433
434 // fmtSx formats a string as a hexadecimal encoding of its bytes.
435 func (f *fmt) fmtSx(s, digits string) {
436 f.fmtSbx(s, nil, digits)
437 }
438
439 // fmtBx formats a byte slice as a hexadecimal encoding of its bytes.
440 func (f *fmt) fmtBx(b []byte, digits string) {
441 f.fmtSbx("", b, digits)
442 }
443
444 // fmtQ formats a string as a double-quoted, escaped Go string constant.
445 // 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.
447 func (f *fmt) fmtQ(s string) {
448 s = f.truncateString(s)
449 if f.sharp && strconv.CanBackquote(s) {
450 f.padString("`" + s + "`")
451 return
452 }
453 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 }
459 }
460
461 // fmtC formats an integer as a Unicode character.
462 // If the character is not valid Unicode, it will print '\ufffd'.
463 func (f *fmt) fmtC(c uint64) {
464 // Explicitly check whether c exceeds utf8.MaxRune since the conversion
465 // of a uint64 to a rune may lose precision that indicates an overflow.
466 r := rune(c)
467 if c > utf8.MaxRune {
468 r = utf8.RuneError
469 }
470 buf := f.intbuf[:0]
471 f.pad(utf8.AppendRune(buf, r))
472 }
473
474 // fmtQc formats an integer as a single-quoted, escaped Go character constant.
475 // If the character is not valid Unicode, it will print '\ufffd'.
476 func (f *fmt) fmtQc(c uint64) {
477 r := rune(c)
478 if c > utf8.MaxRune {
479 r = utf8.RuneError
480 }
481 buf := f.intbuf[:0]
482 if f.plus {
483 f.pad(strconv.AppendQuoteRuneToASCII(buf, r))
484 } else {
485 f.pad(strconv.AppendQuoteRune(buf, r))
486 }
487 }
488
489 // fmtFloat formats a float64. It assumes that verb is a valid format specifier
490 // for strconv.AppendFloat and therefore fits into a byte.
491 func (f *fmt) fmtFloat(v float64, size int, verb rune, prec int) {
492 // Explicit precision in format specifier overrules default precision.
493 if f.precPresent {
494 prec = f.prec
495 }
496 // Format number, reserving space for leading + sign if needed.
497 num := strconv.AppendFloat(f.intbuf[:1], v, byte(verb), prec, size)
498 if num[1] == '-' || num[1] == '+' {
499 num = num[1:]
500 } else {
501 num[0] = '+'
502 }
503 // f.space means to add a leading space instead of a "+" sign unless
504 // the sign is explicitly asked for by f.plus.
505 if f.space && num[0] == '+' && !f.plus {
506 num[0] = ' '
507 }
508 // Special handling for infinities and NaN,
509 // which don't look like a number so shouldn't be padded with zeros.
510 if num[1] == 'I' || num[1] == 'N' {
511 oldZero := f.zero
512 f.zero = false
513 // Remove sign before NaN if not asked for.
514 if num[1] == 'N' && !f.space && !f.plus {
515 num = num[1:]
516 }
517 f.pad(num)
518 f.zero = oldZero
519 return
520 }
521 // The sharp flag forces printing a decimal point for non-binary formats
522 // and retains trailing zeros, which we may need to restore.
523 if f.sharp && verb != 'b' {
524 digits := 0
525 switch verb {
526 case 'v', 'g', 'G', 'x':
527 digits = prec
528 // If no precision is set explicitly use a precision of 6.
529 if digits == -1 {
530 digits = 6
531 }
532 }
533
534 // Buffer pre-allocated with enough room for
535 // exponent notations of the form "e+123" or "p-1023".
536 var tailBuf [6]byte
537 tail := tailBuf[:0]
538
539 hasDecimalPoint := false
540 sawNonzeroDigit := false
541 // Starting from i = 1 to skip sign at num[0].
542 for i := 1; i < len(num); i++ {
543 switch num[i] {
544 case '.':
545 hasDecimalPoint = true
546 case 'p', 'P':
547 tail = append(tail, num[i:]...)
548 num = num[:i]
549 case 'e', 'E':
550 if verb != 'x' && verb != 'X' {
551 tail = append(tail, num[i:]...)
552 num = num[:i]
553 break
554 }
555 fallthrough
556 default:
557 if num[i] != '0' {
558 sawNonzeroDigit = true
559 }
560 // Count significant digits after the first non-zero digit.
561 if sawNonzeroDigit {
562 digits--
563 }
564 }
565 }
566 if !hasDecimalPoint {
567 // Leading digit 0 should contribute once to digits.
568 if len(num) == 2 && num[1] == '0' {
569 digits--
570 }
571 num = append(num, '.')
572 }
573 for digits > 0 {
574 num = append(num, '0')
575 digits--
576 }
577 num = append(num, tail...)
578 }
579 // We want a sign if asked for and if the sign is not positive.
580 if f.plus || num[0] != '+' {
581 // If we're zero padding to the left we want the sign before the leading zeros.
582 // Achieve this by writing the sign out and then padding the unsigned number.
583 // Zero padding is allowed only to the left.
584 if f.zero && !f.minus && f.widPresent && f.wid > len(num) {
585 f.buf.writeByte(num[0])
586 f.writePadding(f.wid - len(num))
587 f.buf.write(num[1:])
588 return
589 }
590 f.pad(num)
591 return
592 }
593 // No sign to show and the number is positive; just print the unsigned number.
594 f.pad(num[1:])
595 }