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.
16 // Strings for use with buffer.WriteString.
17 // This is less overhead than using buffer.Write with byte arrays.
19 commaSpaceString
= ", "
20 nilAngleString
= "<nil>"
21 nilParenString
= "(nil)"
24 percentBangString
= "%!"
25 missingString
= "(MISSING)"
26 badIndexString
= "(BADINDEX)"
27 panicString
= "(PANIC="
28 extraString
= "%!(EXTRA "
29 badWidthString
= "%!(BADWIDTH)"
30 badPrecString
= "%!(BADPREC)"
31 noVerbString
= "%!(NOVERB)"
32 invReflectString
= "<invalid reflect.Value>"
35 // State represents the printer state passed to custom formatters.
36 // It provides access to the io.Writer interface plus information about
37 // the flags and options for the operand's format specifier.
38 type State
interface {
39 // Write is the function to call to emit formatted output to be printed.
40 Write(b
[]byte) (n
int, err error
)
41 // Width returns the value of the width option and whether it has been set.
42 Width() (wid
int, ok
bool)
43 // Precision returns the value of the precision option and whether it has been set.
44 Precision() (prec
int, ok
bool)
46 // Flag reports whether the flag c, a character, has been set.
50 // Formatter is the interface implemented by values with a custom formatter.
51 // The implementation of Format may call Sprint(f) or Fprint(f) etc.
52 // to generate its output.
53 type Formatter
interface {
54 Format(f State
, c rune
)
57 // Stringer is implemented by any value that has a String method,
58 // which defines the ``native'' format for that value.
59 // The String method is used to print values passed as an operand
60 // to any format that accepts a string or to an unformatted printer
62 type Stringer
interface {
66 // GoStringer is implemented by any value that has a GoString method,
67 // which defines the Go syntax for that value.
68 // The GoString method is used to print values passed as an operand
70 type GoStringer
interface {
74 // Use simple []byte instead of bytes.Buffer to avoid large dependency.
77 func (b
*buffer
) Write(p
[]byte) {
81 func (b
*buffer
) WriteString(s
string) {
85 func (b
*buffer
) WriteByte(c
byte) {
89 func (bp
*buffer
) WriteRune(r rune
) {
90 if r
< utf8
.RuneSelf
{
91 *bp
= append(*bp
, byte(r
))
97 for n
+utf8
.UTFMax
> cap(b
) {
100 w
:= utf8
.EncodeRune(b
[n
:n
+utf8
.UTFMax
], r
)
104 // pp is used to store a printer's state and is reused with sync.Pool to avoid allocations.
108 // arg holds the current item, as an interface{}.
111 // value is used instead of arg for reflect values.
114 // fmt is used to format basic items such as integers or strings.
117 // reordered records whether the format string used argument reordering.
119 // goodArgNum records whether the most recent reordering directive was valid.
121 // panicking is set by catchPanic to avoid infinite panic, recover, panic, ... recursion.
123 // erroring is set when printing an error string to guard against calling handleMethods.
127 var ppFree
= sync
.Pool
{
128 New
: func() interface{} { return new(pp
) },
131 // newPrinter allocates a new pp struct or grabs a cached one.
132 func newPrinter() *pp
{
133 p
:= ppFree
.Get().(*pp
)
140 // free saves used pp structs in ppFree; avoids an allocation per invocation.
141 func (p
*pp
) free() {
144 p
.value
= reflect
.Value
{}
148 func (p
*pp
) Width() (wid
int, ok
bool) { return p
.fmt
.wid
, p
.fmt
.widPresent
}
150 func (p
*pp
) Precision() (prec
int, ok
bool) { return p
.fmt
.prec
, p
.fmt
.precPresent
}
152 func (p
*pp
) Flag(b
int) bool {
157 return p
.fmt
.plus || p
.fmt
.plusV
159 return p
.fmt
.sharp || p
.fmt
.sharpV
168 // Implement Write so we can call Fprintf on a pp (through State), for
169 // recursive use in custom verbs.
170 func (p
*pp
) Write(b
[]byte) (ret
int, err error
) {
175 // These routines end in 'f' and take a format string.
177 // Fprintf formats according to a format specifier and writes to w.
178 // It returns the number of bytes written and any write error encountered.
179 func Fprintf(w io
.Writer
, format
string, a
...interface{}) (n
int, err error
) {
181 p
.doPrintf(format
, a
)
182 n
, err
= w
.Write(p
.buf
)
187 // Printf formats according to a format specifier and writes to standard output.
188 // It returns the number of bytes written and any write error encountered.
189 func Printf(format
string, a
...interface{}) (n
int, err error
) {
190 return Fprintf(os
.Stdout
, format
, a
...)
193 // Sprintf formats according to a format specifier and returns the resulting string.
194 func Sprintf(format
string, a
...interface{}) string {
196 p
.doPrintf(format
, a
)
202 // Errorf formats according to a format specifier and returns the string
203 // as a value that satisfies error.
204 func Errorf(format
string, a
...interface{}) error
{
205 return errors
.New(Sprintf(format
, a
...))
208 // These routines do not take a format string
210 // Fprint formats using the default formats for its operands and writes to w.
211 // Spaces are added between operands when neither is a string.
212 // It returns the number of bytes written and any write error encountered.
213 func Fprint(w io
.Writer
, a
...interface{}) (n
int, err error
) {
216 n
, err
= w
.Write(p
.buf
)
221 // Print formats using the default formats for its operands and writes to standard output.
222 // Spaces are added between operands when neither is a string.
223 // It returns the number of bytes written and any write error encountered.
224 func Print(a
...interface{}) (n
int, err error
) {
225 return Fprint(os
.Stdout
, a
...)
228 // Sprint formats using the default formats for its operands and returns the resulting string.
229 // Spaces are added between operands when neither is a string.
230 func Sprint(a
...interface{}) string {
238 // These routines end in 'ln', do not take a format string,
239 // always add spaces between operands, and add a newline
240 // after the last operand.
242 // Fprintln formats using the default formats for its operands and writes to w.
243 // Spaces are always added between operands and a newline is appended.
244 // It returns the number of bytes written and any write error encountered.
245 func Fprintln(w io
.Writer
, a
...interface{}) (n
int, err error
) {
248 n
, err
= w
.Write(p
.buf
)
253 // Println formats using the default formats for its operands and writes to standard output.
254 // Spaces are always added between operands and a newline is appended.
255 // It returns the number of bytes written and any write error encountered.
256 func Println(a
...interface{}) (n
int, err error
) {
257 return Fprintln(os
.Stdout
, a
...)
260 // Sprintln formats using the default formats for its operands and returns the resulting string.
261 // Spaces are always added between operands and a newline is appended.
262 func Sprintln(a
...interface{}) string {
270 // getField gets the i'th field of the struct value.
271 // If the field is itself is an interface, return a value for
272 // the thing inside the interface, not the interface itself.
273 func getField(v reflect
.Value
, i
int) reflect
.Value
{
275 if val
.Kind() == reflect
.Interface
&& !val
.IsNil() {
281 // tooLarge reports whether the magnitude of the integer is
282 // too large to be used as a formatting width or precision.
283 func tooLarge(x
int) bool {
285 return x
> max || x
< -max
288 // parsenum converts ASCII to integer. num is 0 (and isnum is false) if no number present.
289 func parsenum(s
string, start
, end
int) (num
int, isnum
bool, newi
int) {
293 for newi
= start
; newi
< end
&& '0' <= s
[newi
] && s
[newi
] <= '9'; newi
++ {
295 return 0, false, end
// Overflow; crazy long number most likely.
297 num
= num
*10 + int(s
[newi
]-'0')
303 func (p
*pp
) unknownType(v reflect
.Value
) {
305 p
.buf
.WriteString(nilAngleString
)
309 p
.buf
.WriteString(v
.Type().String())
313 func (p
*pp
) badVerb(verb rune
) {
315 p
.buf
.WriteString(percentBangString
)
316 p
.buf
.WriteRune(verb
)
320 p
.buf
.WriteString(reflect
.TypeOf(p
.arg
).String())
322 p
.printArg(p
.arg
, 'v')
323 case p
.value
.IsValid():
324 p
.buf
.WriteString(p
.value
.Type().String())
326 p
.printValue(p
.value
, 'v', 0)
328 p
.buf
.WriteString(nilAngleString
)
334 func (p
*pp
) fmtBool(v
bool, verb rune
) {
343 // fmt0x64 formats a uint64 in hexadecimal and prefixes it with 0x or
344 // not, as requested, by temporarily setting the sharp flag.
345 func (p
*pp
) fmt0x64(v
uint64, leading0x
bool) {
347 p
.fmt
.sharp
= leading0x
348 p
.fmt
.fmt_integer(v
, 16, unsigned
, ldigits
)
352 // fmtInteger formats a signed or unsigned integer.
353 func (p
*pp
) fmtInteger(v
uint64, isSigned
bool, verb rune
) {
356 if p
.fmt
.sharpV
&& !isSigned
{
359 p
.fmt
.fmt_integer(v
, 10, isSigned
, ldigits
)
362 p
.fmt
.fmt_integer(v
, 10, isSigned
, ldigits
)
364 p
.fmt
.fmt_integer(v
, 2, isSigned
, ldigits
)
366 p
.fmt
.fmt_integer(v
, 8, isSigned
, ldigits
)
368 p
.fmt
.fmt_integer(v
, 16, isSigned
, ldigits
)
370 p
.fmt
.fmt_integer(v
, 16, isSigned
, udigits
)
374 if v
<= utf8
.MaxRune
{
386 // fmtFloat formats a float. The default precision for each verb
387 // is specified as last argument in the call to fmt_float.
388 func (p
*pp
) fmtFloat(v
float64, size
int, verb rune
) {
391 p
.fmt
.fmt_float(v
, size
, 'g', -1)
393 p
.fmt
.fmt_float(v
, size
, verb
, -1)
395 p
.fmt
.fmt_float(v
, size
, verb
, 6)
397 p
.fmt
.fmt_float(v
, size
, 'f', 6)
403 // fmtComplex formats a complex number v with
404 // r = real(v) and j = imag(v) as (r+ji) using
405 // fmtFloat for r and j formatting.
406 func (p
*pp
) fmtComplex(v complex128
, size
int, verb rune
) {
407 // Make sure any unsupported verbs are found before the
408 // calls to fmtFloat to not generate an incorrect error string.
410 case 'v', 'b', 'g', 'G', 'f', 'F', 'e', 'E':
411 oldPlus
:= p
.fmt
.plus
413 p
.fmtFloat(real(v
), size
/2, verb
)
414 // Imaginary part always has a sign.
416 p
.fmtFloat(imag(v
), size
/2, verb
)
417 p
.buf
.WriteString("i)")
424 func (p
*pp
) fmtString(v
string, verb rune
) {
435 p
.fmt
.fmt_sx(v
, ldigits
)
437 p
.fmt
.fmt_sx(v
, udigits
)
445 func (p
*pp
) fmtBytes(v
[]byte, verb rune
, typeString
string) {
449 p
.buf
.WriteString(typeString
)
451 p
.buf
.WriteString(nilParenString
)
455 for i
, c
:= range v
{
457 p
.buf
.WriteString(commaSpaceString
)
459 p
.fmt0x64(uint64(c
), true)
464 for i
, c
:= range v
{
468 p
.fmt
.fmt_integer(uint64(c
), 10, unsigned
, ldigits
)
473 p
.fmt
.fmt_s(string(v
))
475 p
.fmt
.fmt_bx(v
, ldigits
)
477 p
.fmt
.fmt_bx(v
, udigits
)
479 p
.fmt
.fmt_q(string(v
))
481 p
.printValue(reflect
.ValueOf(v
), verb
, 0)
485 func (p
*pp
) fmtPointer(value reflect
.Value
, verb rune
) {
487 switch value
.Kind() {
488 case reflect
.Chan
, reflect
.Func
, reflect
.Map
, reflect
.Ptr
, reflect
.Slice
, reflect
.UnsafePointer
:
499 p
.buf
.WriteString(value
.Type().String())
500 p
.buf
.WriteString(")(")
502 p
.buf
.WriteString(nilString
)
504 p
.fmt0x64(uint64(u
), true)
509 p
.fmt
.padString(nilAngleString
)
511 p
.fmt0x64(uint64(u
), !p
.fmt
.sharp
)
515 p
.fmt0x64(uint64(u
), !p
.fmt
.sharp
)
516 case 'b', 'o', 'd', 'x', 'X':
517 p
.fmtInteger(uint64(u
), unsigned
, verb
)
523 func (p
*pp
) catchPanic(arg
interface{}, verb rune
) {
524 if err
:= recover(); err
!= nil {
525 // If it's a nil pointer, just say "<nil>". The likeliest causes are a
526 // Stringer that fails to guard against nil or a nil pointer for a
527 // value receiver, and in either case, "<nil>" is a nice result.
528 if v
:= reflect
.ValueOf(arg
); v
.Kind() == reflect
.Ptr
&& v
.IsNil() {
529 p
.buf
.WriteString(nilAngleString
)
532 // Otherwise print a concise panic message. Most of the time the panic
533 // value will print itself nicely.
535 // Nested panics; the recursion in printArg cannot succeed.
539 oldFlags
:= p
.fmt
.fmtFlags
540 // For this output we want default behavior.
543 p
.buf
.WriteString(percentBangString
)
544 p
.buf
.WriteRune(verb
)
545 p
.buf
.WriteString(panicString
)
551 p
.fmt
.fmtFlags
= oldFlags
555 func (p
*pp
) handleMethods(verb rune
) (handled
bool) {
559 // Is it a Formatter?
560 if formatter
, ok
:= p
.arg
.(Formatter
); ok
{
562 defer p
.catchPanic(p
.arg
, verb
)
563 formatter
.Format(p
, verb
)
567 // If we're doing Go syntax and the argument knows how to supply it, take care of it now.
569 if stringer
, ok
:= p
.arg
.(GoStringer
); ok
{
571 defer p
.catchPanic(p
.arg
, verb
)
572 // Print the result of GoString unadorned.
573 p
.fmt
.fmt_s(stringer
.GoString())
577 // If a string is acceptable according to the format, see if
578 // the value satisfies one of the string-valued interfaces.
579 // Println etc. set verb to %v, which is "stringable".
581 case 'v', 's', 'x', 'X', 'q':
582 // Is it an error or Stringer?
583 // The duplication in the bodies is necessary:
584 // setting handled and deferring catchPanic
585 // must happen before calling the method.
586 switch v
:= p
.arg
.(type) {
589 defer p
.catchPanic(p
.arg
, verb
)
590 p
.fmtString(v
.Error(), verb
)
595 defer p
.catchPanic(p
.arg
, verb
)
596 p
.fmtString(v
.String(), verb
)
604 func (p
*pp
) printArg(arg
interface{}, verb rune
) {
606 p
.value
= reflect
.Value
{}
611 p
.fmt
.padString(nilAngleString
)
618 // Special processing considerations.
619 // %T (the value's type) and %p (its address) are special; we always do them first.
622 p
.fmt
.fmt_s(reflect
.TypeOf(arg
).String())
625 p
.fmtPointer(reflect
.ValueOf(arg
), 'p')
629 // Some types can be done without reflection.
630 switch f
:= arg
.(type) {
634 p
.fmtFloat(float64(f
), 32, verb
)
636 p
.fmtFloat(f
, 64, verb
)
638 p
.fmtComplex(complex128(f
), 64, verb
)
640 p
.fmtComplex(f
, 128, verb
)
642 p
.fmtInteger(uint64(f
), signed
, verb
)
644 p
.fmtInteger(uint64(f
), signed
, verb
)
646 p
.fmtInteger(uint64(f
), signed
, verb
)
648 p
.fmtInteger(uint64(f
), signed
, verb
)
650 p
.fmtInteger(uint64(f
), signed
, verb
)
652 p
.fmtInteger(uint64(f
), unsigned
, verb
)
654 p
.fmtInteger(uint64(f
), unsigned
, verb
)
656 p
.fmtInteger(uint64(f
), unsigned
, verb
)
658 p
.fmtInteger(uint64(f
), unsigned
, verb
)
660 p
.fmtInteger(f
, unsigned
, verb
)
662 p
.fmtInteger(uint64(f
), unsigned
, verb
)
666 p
.fmtBytes(f
, verb
, "[]byte")
668 // Handle extractable values with special methods
669 // since printValue does not handle them at depth 0.
670 if f
.IsValid() && f
.CanInterface() {
671 p
.arg
= f
.Interface()
672 if p
.handleMethods(verb
) {
676 p
.printValue(f
, verb
, 0)
678 // If the type is not simple, it might have methods.
679 if !p
.handleMethods(verb
) {
680 // Need to use reflection, since the type had no
681 // interface methods that could be used for formatting.
682 p
.printValue(reflect
.ValueOf(f
), verb
, 0)
687 var byteType
= reflect
.TypeOf(byte(0))
689 // printValue is similar to printArg but starts with a reflect value, not an interface{} value.
690 // It does not handle 'p' and 'T' verbs because these should have been already handled by printArg.
691 func (p
*pp
) printValue(value reflect
.Value
, verb rune
, depth
int) {
692 // Handle values with special methods if not already handled by printArg (depth == 0).
693 if depth
> 0 && value
.IsValid() && value
.CanInterface() {
694 p
.arg
= value
.Interface()
695 if p
.handleMethods(verb
) {
702 switch f
:= value
; value
.Kind() {
703 case reflect
.Invalid
:
705 p
.buf
.WriteString(invReflectString
)
709 p
.buf
.WriteString(nilAngleString
)
715 p
.fmtBool(f
.Bool(), verb
)
716 case reflect
.Int
, reflect
.Int8
, reflect
.Int16
, reflect
.Int32
, reflect
.Int64
:
717 p
.fmtInteger(uint64(f
.Int()), signed
, verb
)
718 case reflect
.Uint
, reflect
.Uint8
, reflect
.Uint16
, reflect
.Uint32
, reflect
.Uint64
, reflect
.Uintptr
:
719 p
.fmtInteger(f
.Uint(), unsigned
, verb
)
720 case reflect
.Float32
:
721 p
.fmtFloat(f
.Float(), 32, verb
)
722 case reflect
.Float64
:
723 p
.fmtFloat(f
.Float(), 64, verb
)
724 case reflect
.Complex64
:
725 p
.fmtComplex(f
.Complex(), 64, verb
)
726 case reflect
.Complex128
:
727 p
.fmtComplex(f
.Complex(), 128, verb
)
729 p
.fmtString(f
.String(), verb
)
732 p
.buf
.WriteString(f
.Type().String())
734 p
.buf
.WriteString(nilParenString
)
739 p
.buf
.WriteString(mapString
)
742 for i
, key
:= range keys
{
745 p
.buf
.WriteString(commaSpaceString
)
750 p
.printValue(key
, verb
, depth
+1)
752 p
.printValue(f
.MapIndex(key
), verb
, depth
+1)
761 p
.buf
.WriteString(f
.Type().String())
764 for i
:= 0; i
< f
.NumField(); i
++ {
767 p
.buf
.WriteString(commaSpaceString
)
772 if p
.fmt
.plusV || p
.fmt
.sharpV
{
773 if name
:= f
.Type().Field(i
).Name
; name
!= "" {
774 p
.buf
.WriteString(name
)
778 p
.printValue(getField(f
, i
), verb
, depth
+1)
781 case reflect
.Interface
:
783 if !value
.IsValid() {
785 p
.buf
.WriteString(f
.Type().String())
786 p
.buf
.WriteString(nilParenString
)
788 p
.buf
.WriteString(nilAngleString
)
791 p
.printValue(value
, verb
, depth
+1)
793 case reflect
.Array
, reflect
.Slice
:
795 case 's', 'q', 'x', 'X':
796 // Handle byte and uint8 slices and arrays special for the above verbs.
798 if t
.Elem().Kind() == reflect
.Uint8
{
800 if f
.Kind() == reflect
.Slice
{
802 } else if f
.CanAddr() {
803 bytes
= f
.Slice(0, f
.Len()).Bytes()
805 // We have an array, but we cannot Slice() a non-addressable array,
806 // so we build a slice by hand. This is a rare case but it would be nice
807 // if reflection could help a little more.
808 bytes
= make([]byte, f
.Len())
809 for i
:= range bytes
{
810 bytes
[i
] = byte(f
.Index(i
).Uint())
813 p
.fmtBytes(bytes
, verb
, t
.String())
818 p
.buf
.WriteString(f
.Type().String())
819 if f
.Kind() == reflect
.Slice
&& f
.IsNil() {
820 p
.buf
.WriteString(nilParenString
)
824 for i
:= 0; i
< f
.Len(); i
++ {
826 p
.buf
.WriteString(commaSpaceString
)
828 p
.printValue(f
.Index(i
), verb
, depth
+1)
833 for i
:= 0; i
< f
.Len(); i
++ {
837 p
.printValue(f
.Index(i
), verb
, depth
+1)
842 // pointer to array or slice or struct? ok at top level
843 // but not embedded (avoid loops)
844 if depth
== 0 && f
.Pointer() != 0 {
845 switch a
:= f
.Elem(); a
.Kind() {
846 case reflect
.Array
, reflect
.Slice
, reflect
.Struct
, reflect
.Map
:
848 p
.printValue(a
, verb
, depth
+1)
853 case reflect
.Chan
, reflect
.Func
, reflect
.UnsafePointer
:
854 p
.fmtPointer(f
, verb
)
860 // intFromArg gets the argNumth element of a. On return, isInt reports whether the argument has integer type.
861 func intFromArg(a
[]interface{}, argNum
int) (num
int, isInt
bool, newArgNum
int) {
864 num
, isInt
= a
[argNum
].(int) // Almost always OK.
867 switch v
:= reflect
.ValueOf(a
[argNum
]); v
.Kind() {
868 case reflect
.Int
, reflect
.Int8
, reflect
.Int16
, reflect
.Int32
, reflect
.Int64
:
870 if int64(int(n
)) == n
{
874 case reflect
.Uint
, reflect
.Uint8
, reflect
.Uint16
, reflect
.Uint32
, reflect
.Uint64
, reflect
.Uintptr
:
876 if int64(n
) >= 0 && uint64(int(n
)) == n
{
884 newArgNum
= argNum
+ 1
893 // parseArgNumber returns the value of the bracketed number, minus 1
894 // (explicit argument numbers are one-indexed but we want zero-indexed).
895 // The opening bracket is known to be present at format[0].
896 // The returned values are the index, the number of bytes to consume
897 // up to the closing paren, if present, and whether the number parsed
898 // ok. The bytes to consume will be 1 if no closing paren is present.
899 func parseArgNumber(format
string) (index
int, wid
int, ok
bool) {
900 // There must be at least 3 bytes: [n].
905 // Find closing bracket.
906 for i
:= 1; i
< len(format
); i
++ {
907 if format
[i
] == ']' {
908 width
, ok
, newi
:= parsenum(format
, 1, i
)
909 if !ok || newi
!= i
{
910 return 0, i
+ 1, false
912 return width
- 1, i
+ 1, true // arg numbers are one-indexed and skip paren.
918 // argNumber returns the next argument to evaluate, which is either the value of the passed-in
919 // argNum or the value of the bracketed integer that begins format[i:]. It also returns
920 // the new value of i, that is, the index of the next byte of the format to process.
921 func (p
*pp
) argNumber(argNum
int, format
string, i
int, numArgs
int) (newArgNum
, newi
int, found
bool) {
922 if len(format
) <= i || format
[i
] != '[' {
923 return argNum
, i
, false
926 index
, wid
, ok
:= parseArgNumber(format
[i
:])
927 if ok
&& 0 <= index
&& index
< numArgs
{
928 return index
, i
+ wid
, true
931 return argNum
, i
+ wid
, ok
934 func (p
*pp
) badArgNum(verb rune
) {
935 p
.buf
.WriteString(percentBangString
)
936 p
.buf
.WriteRune(verb
)
937 p
.buf
.WriteString(badIndexString
)
940 func (p
*pp
) missingArg(verb rune
) {
941 p
.buf
.WriteString(percentBangString
)
942 p
.buf
.WriteRune(verb
)
943 p
.buf
.WriteString(missingString
)
946 func (p
*pp
) doPrintf(format
string, a
[]interface{}) {
948 argNum
:= 0 // we process one argument per non-trivial format
949 afterIndex
:= false // previous item in format was an index like [3].
952 for i
:= 0; i
< end
; {
955 for i
< end
&& format
[i
] != '%' {
959 p
.buf
.WriteString(format
[lasti
:i
])
962 // done processing format string
978 p
.fmt
.zero
= !p
.fmt
.minus
// Only allow zero padding to the left.
983 p
.fmt
.zero
= false // Do not pad with zeros to the right.
987 // Fast path for common case of ascii lower case simple verbs
988 // without precision or width or argument indices.
989 if 'a' <= c
&& c
<= 'z' && argNum
< len(a
) {
992 p
.fmt
.sharpV
= p
.fmt
.sharp
994 // Struct-field syntax
995 p
.fmt
.plusV
= p
.fmt
.plus
998 p
.printArg(a
[argNum
], rune(c
))
1003 // Format is more complex than simple flags and a verb or is malformed.
1008 // Do we have an explicit argument index?
1009 argNum
, i
, afterIndex
= p
.argNumber(argNum
, format
, i
, len(a
))
1011 // Do we have width?
1012 if i
< end
&& format
[i
] == '*' {
1014 p
.fmt
.wid
, p
.fmt
.widPresent
, argNum
= intFromArg(a
, argNum
)
1016 if !p
.fmt
.widPresent
{
1017 p
.buf
.WriteString(badWidthString
)
1020 // We have a negative width, so take its value and ensure
1021 // that the minus flag is set
1023 p
.fmt
.wid
= -p
.fmt
.wid
1025 p
.fmt
.zero
= false // Do not pad with zeros to the right.
1029 p
.fmt
.wid
, p
.fmt
.widPresent
, i
= parsenum(format
, i
, end
)
1030 if afterIndex
&& p
.fmt
.widPresent
{ // "%[3]2d"
1031 p
.goodArgNum
= false
1035 // Do we have precision?
1036 if i
+1 < end
&& format
[i
] == '.' {
1038 if afterIndex
{ // "%[3].2d"
1039 p
.goodArgNum
= false
1041 argNum
, i
, afterIndex
= p
.argNumber(argNum
, format
, i
, len(a
))
1042 if i
< end
&& format
[i
] == '*' {
1044 p
.fmt
.prec
, p
.fmt
.precPresent
, argNum
= intFromArg(a
, argNum
)
1045 // Negative precision arguments don't make sense
1048 p
.fmt
.precPresent
= false
1050 if !p
.fmt
.precPresent
{
1051 p
.buf
.WriteString(badPrecString
)
1055 p
.fmt
.prec
, p
.fmt
.precPresent
, i
= parsenum(format
, i
, end
)
1056 if !p
.fmt
.precPresent
{
1058 p
.fmt
.precPresent
= true
1064 argNum
, i
, afterIndex
= p
.argNumber(argNum
, format
, i
, len(a
))
1068 p
.buf
.WriteString(noVerbString
)
1072 verb
, w
:= utf8
.DecodeRuneInString(format
[i
:])
1076 case verb
== '%': // Percent does not absorb operands and ignores f.wid and f.prec.
1077 p
.buf
.WriteByte('%')
1080 case argNum
>= len(a
): // No argument left over to print for the current verb.
1084 p
.fmt
.sharpV
= p
.fmt
.sharp
1086 // Struct-field syntax
1087 p
.fmt
.plusV
= p
.fmt
.plus
1091 p
.printArg(a
[argNum
], verb
)
1096 // Check for extra arguments unless the call accessed the arguments
1097 // out of order, in which case it's too expensive to detect if they've all
1098 // been used and arguably OK if they're not.
1099 if !p
.reordered
&& argNum
< len(a
) {
1101 p
.buf
.WriteString(extraString
)
1102 for i
, arg
:= range a
[argNum
:] {
1104 p
.buf
.WriteString(commaSpaceString
)
1107 p
.buf
.WriteString(nilAngleString
)
1109 p
.buf
.WriteString(reflect
.TypeOf(arg
).String())
1110 p
.buf
.WriteByte('=')
1111 p
.printArg(arg
, 'v')
1114 p
.buf
.WriteByte(')')
1118 func (p
*pp
) doPrint(a
[]interface{}) {
1120 for argNum
, arg
:= range a
{
1121 isString
:= arg
!= nil && reflect
.TypeOf(arg
).Kind() == reflect
.String
1122 // Add a space between two non-string arguments.
1123 if argNum
> 0 && !isString
&& !prevString
{
1124 p
.buf
.WriteByte(' ')
1126 p
.printArg(arg
, 'v')
1127 prevString
= isString
1131 // doPrintln is like doPrint but always adds a space between arguments
1132 // and a newline after the last argument.
1133 func (p
*pp
) doPrintln(a
[]interface{}) {
1134 for argNum
, arg
:= range a
{
1136 p
.buf
.WriteByte(' ')
1138 p
.printArg(arg
, 'v')
1140 p
.buf
.WriteByte('\n')