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.
13 ldigits
= "0123456789abcdefx"
14 udigits
= "0123456789ABCDEFX"
22 // flags placed in a separate struct for easy clearing.
23 type fmtFlags
struct {
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.
39 // A fmt is the raw formatter used by Printf etc.
40 // It prints into a buffer that must be set up separately.
49 // intbuf is large enought to store %b of an int64 with a sign and
50 // avoids padding at the end of the struct on 32 bit architectures.
54 func (f
*fmt
) clearflags() {
55 f
.fmtFlags
= fmtFlags
{}
58 func (f
*fmt
) init(buf
*buffer
) {
63 // writePadding generates n bytes of padding.
64 func (f
*fmt
) writePadding(n
int) {
65 if n
<= 0 { // No padding bytes needed.
71 // Make enough room for padding.
72 if newLen
> cap(buf
) {
73 buf
= make(buffer
, cap(buf
)*2+n
)
76 // Decide which byte the padding should be filled with.
81 // Fill padding with padByte.
82 padding
:= buf
[oldLen
:newLen
]
83 for i
:= range padding
{
89 // pad appends b to f.buf, padded on left (!f.minus) or right (f.minus).
90 func (f
*fmt
) pad(b
[]byte) {
91 if !f
.widPresent || f
.wid
== 0 {
95 width
:= f
.wid
- utf8
.RuneCount(b
)
103 f
.writePadding(width
)
107 // padString appends s to f.buf, padded on left (!f.minus) or right (f.minus).
108 func (f
*fmt
) padString(s
string) {
109 if !f
.widPresent || f
.wid
== 0 {
113 width
:= f
.wid
- utf8
.RuneCountInString(s
)
116 f
.writePadding(width
)
121 f
.writePadding(width
)
125 // fmt_boolean formats a boolean.
126 func (f
*fmt
) fmt_boolean(v
bool) {
134 // fmt_unicode formats a uint64 as "U+0078" or with f.sharp set as "U+0078 'x'".
135 func (f
*fmt
) fmt_unicode(u
uint64) {
138 // With default precision set the maximum needed buf length is 18
139 // for formatting -1 with %#U ("U+FFFFFFFFFFFFFFFF") which fits
140 // into the already allocated intbuf with a capacity of 68 bytes.
142 if f
.precPresent
&& f
.prec
> 4 {
144 // Compute space needed for "U+" , number, " '", character, "'".
145 width
:= 2 + prec
+ 2 + utf8
.UTFMax
+ 1
146 if width
> len(buf
) {
147 buf
= make([]byte, width
)
151 // Format into buf, ending at buf[i]. Formatting numbers is easier right-to-left.
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
)) {
158 i
-= utf8
.RuneLen(rune(u
))
159 utf8
.EncodeRune(buf
[i
:], rune(u
))
165 // Format the Unicode code point u as a hexadecimal number.
168 buf
[i
] = udigits
[u
&0xF]
175 // Add zeros in front of the number until requested precision is reached.
181 // Add a leading "U+".
193 // fmt_integer formats signed and unsigned integers.
194 func (f
*fmt
) fmt_integer(u
uint64, base
int, isSigned
bool, digits
string) {
195 negative
:= isSigned
&& int64(u
) < 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
) {
207 // We're going to need a bigger boat.
208 buf
= make([]byte, width
)
212 // 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.
218 // Precision of 0 and value of 0 means "print nothing" but padding.
219 if prec
== 0 && u
== 0 {
222 f
.writePadding(f
.wid
)
226 } else if f
.zero
&& f
.widPresent
{
228 if negative || f
.plus || f
.space
{
229 prec
-- // leave room for sign
233 // 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.
237 // Use constants for the division and modulo for more efficient code.
238 // Switch cases ordered by popularity.
244 buf
[i
] = byte('0' + u
- next
*10)
250 buf
[i
] = digits
[u
&0xF]
256 buf
[i
] = byte('0' + u
&7)
262 buf
[i
] = byte('0' + u
&1)
266 panic("fmt: unknown base; can't happen")
270 for i
> 0 && prec
> len(buf
)-i
{
275 // Various prefixes: 0x, -, etc.
284 // Add a leading 0x or 0X.
303 // Left padding with zeros has already been handled like precision earlier
304 // or the f.zero flag is ignored due to an explicitly set precision.
311 // truncate truncates the string to the specified precision, if present.
312 func (f
*fmt
) truncate(s
string) string {
325 // fmt_s formats a string.
326 func (f
*fmt
) fmt_s(s
string) {
331 // fmt_sbx formats a string or byte slice as a hexadecimal encoding of its bytes.
332 func (f
*fmt
) fmt_sbx(s
string, b
[]byte, digits
string) {
335 // No byte slice present. Assume string s should be encoded.
338 // Set length to not process more bytes than the precision demands.
339 if f
.precPresent
&& f
.prec
< length
{
342 // Compute width of the encoding taking into account the f.sharp and f.space flag.
346 // Each element encoded by two hexadecimals will get a leading 0x or 0X.
350 // Elements will be separated by a space.
353 // Only a leading 0x or 0X will be added for the whole string.
356 } else { // The byte slice or string that should be encoded is empty.
358 f
.writePadding(f
.wid
)
362 // Handle padding to the left.
363 if f
.widPresent
&& f
.wid
> width
&& !f
.minus
{
364 f
.writePadding(f
.wid
- width
)
366 // Write the encoding directly into the output buffer.
369 // Add leading 0x or 0X.
370 buf
= append(buf
, '0', digits
[16])
373 for i
:= 0; i
< length
; i
++ {
374 if f
.space
&& i
> 0 {
375 // Separate elements with a space.
376 buf
= append(buf
, ' ')
378 // Add leading 0x or 0X for each element.
379 buf
= append(buf
, '0', digits
[16])
383 c
= b
[i
] // Take a byte from the input byte slice.
385 c
= s
[i
] // Take a byte from the input string.
387 // Encode each byte as two hexadecimal digits.
388 buf
= append(buf
, digits
[c
>>4], digits
[c
&0xF])
391 // Handle padding to the right.
392 if f
.widPresent
&& f
.wid
> width
&& f
.minus
{
393 f
.writePadding(f
.wid
- width
)
397 // fmt_sx formats a string as a hexadecimal encoding of its bytes.
398 func (f
*fmt
) fmt_sx(s
, digits
string) {
399 f
.fmt_sbx(s
, nil, digits
)
402 // fmt_bx formats a byte slice as a hexadecimal encoding of its bytes.
403 func (f
*fmt
) fmt_bx(b
[]byte, digits
string) {
404 f
.fmt_sbx("", b
, digits
)
407 // fmt_q formats a string as a double-quoted, escaped Go string constant.
408 // 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.
410 func (f
*fmt
) fmt_q(s
string) {
412 if f
.sharp
&& strconv
.CanBackquote(s
) {
413 f
.padString("`" + s
+ "`")
418 f
.pad(strconv
.AppendQuoteToASCII(buf
, s
))
420 f
.pad(strconv
.AppendQuote(buf
, s
))
424 // fmt_c formats an integer as a Unicode character.
425 // If the character is not valid Unicode, it will print '\ufffd'.
426 func (f
*fmt
) fmt_c(c
uint64) {
428 if c
> utf8
.MaxRune
{
432 w
:= utf8
.EncodeRune(buf
[:utf8
.UTFMax
], r
)
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'.
438 func (f
*fmt
) fmt_qc(c
uint64) {
440 if c
> utf8
.MaxRune
{
445 f
.pad(strconv
.AppendQuoteRuneToASCII(buf
, r
))
447 f
.pad(strconv
.AppendQuoteRune(buf
, r
))
451 // fmt_float formats a float64. It assumes that verb is a valid format specifier
452 // for strconv.AppendFloat and therefore fits into a byte.
453 func (f
*fmt
) fmt_float(v
float64, size
int, verb rune
, prec
int) {
454 // Explicit precision in format specifier overrules default precision.
458 // Format number, reserving space for leading + sign if needed.
459 num
:= strconv
.AppendFloat(f
.intbuf
[:1], v
, byte(verb
), prec
, size
)
460 if num
[1] == '-' || num
[1] == '+' {
465 // 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
{
470 // Special handling for infinities and NaN,
471 // which don't look like a number so shouldn't be padded with zeros.
472 if num
[1] == 'I' || num
[1] == 'N' {
475 // Remove sign before NaN if not asked for.
476 if num
[1] == 'N' && !f
.space
&& !f
.plus
{
483 // We want a sign if asked for and if the sign is not positive.
484 if f
.plus || num
[0] != '+' {
485 // If we're zero padding to the left we want the sign before the leading zeros.
486 // Achieve this by writing the sign out and then padding the unsigned number.
487 if f
.zero
&& f
.widPresent
&& f
.wid
> len(num
) {
488 f
.buf
.WriteByte(num
[0])
489 f
.writePadding(f
.wid
- len(num
))
496 // No sign to show and the number is positive; just print the unsigned number.