// Copyright 2009 The Go Authors. All rights reserved.
// Use of this source code is governed by a BSD-style
// license that can be found in the LICENSE file.

package fmt

import (
	
	
	
	
	
	
	
)

// Strings for use with buffer.WriteString.
// This is less overhead than using buffer.Write with byte arrays.
const (
	commaSpaceString  = ", "
	nilAngleString    = "<nil>"
	nilParenString    = "(nil)"
	nilString         = "nil"
	mapString         = "map["
	percentBangString = "%!"
	missingString     = "(MISSING)"
	badIndexString    = "(BADINDEX)"
	panicString       = "(PANIC="
	extraString       = "%!(EXTRA "
	badWidthString    = "%!(BADWIDTH)"
	badPrecString     = "%!(BADPREC)"
	noVerbString      = "%!(NOVERB)"
	invReflectString  = "<invalid reflect.Value>"
)

// State represents the printer state passed to custom formatters.
// It provides access to the [io.Writer] interface plus information about
// the flags and options for the operand's format specifier.
type State interface {
	// Write is the function to call to emit formatted output to be printed.
	Write(b []byte) (n int, err error)
	// Width returns the value of the width option and whether it has been set.
	Width() (wid int, ok bool)
	// Precision returns the value of the precision option and whether it has been set.
	Precision() (prec int, ok bool)

	// Flag reports whether the flag c, a character, has been set.
	Flag(c int) bool
}

// Formatter is implemented by any value that has a Format method.
// The implementation controls how [State] and rune are interpreted,
// and may call [Sprint] or [Fprint](f) etc. to generate its output.
type Formatter interface {
	Format(f State, verb rune)
}

// Stringer is implemented by any value that has a String method,
// which defines the “native” format for that value.
// The String method is used to print values passed as an operand
// to any format that accepts a string or to an unformatted printer
// such as [Print].
type Stringer interface {
	String() string
}

// GoStringer is implemented by any value that has a GoString method,
// which defines the Go syntax for that value.
// The GoString method is used to print values passed as an operand
// to a %#v format.
type GoStringer interface {
	GoString() string
}

// FormatString returns a string representing the fully qualified formatting
// directive captured by the [State], followed by the argument verb. ([State] does not
// itself contain the verb.) The result has a leading percent sign followed by any
// flags, the width, and the precision. Missing flags, width, and precision are
// omitted. This function allows a [Formatter] to reconstruct the original
// directive triggering the call to Format.
func ( State,  rune) string {
	var  [16]byte // Use a local buffer.
	 := append([:0], '%')
	for ,  := range " +-#0" { // All known flags
		if .Flag(int()) { // The argument is an int for historical reasons.
			 = append(, byte())
		}
	}
	if ,  := .Width();  {
		 = strconv.AppendInt(, int64(), 10)
	}
	if ,  := .Precision();  {
		 = append(, '.')
		 = strconv.AppendInt(, int64(), 10)
	}
	 = utf8.AppendRune(, )
	return string()
}

// Use simple []byte instead of bytes.Buffer to avoid large dependency.
type buffer []byte

func ( *buffer) ( []byte) {
	* = append(*, ...)
}

func ( *buffer) ( string) {
	* = append(*, ...)
}

func ( *buffer) ( byte) {
	* = append(*, )
}

func ( *buffer) ( rune) {
	* = utf8.AppendRune(*, )
}

// pp is used to store a printer's state and is reused with sync.Pool to avoid allocations.
type pp struct {
	buf buffer

	// arg holds the current item, as an interface{}.
	arg any

	// value is used instead of arg for reflect values.
	value reflect.Value

	// fmt is used to format basic items such as integers or strings.
	fmt fmt

	// reordered records whether the format string used argument reordering.
	reordered bool
	// goodArgNum records whether the most recent reordering directive was valid.
	goodArgNum bool
	// panicking is set by catchPanic to avoid infinite panic, recover, panic, ... recursion.
	panicking bool
	// erroring is set when printing an error string to guard against calling handleMethods.
	erroring bool
	// wrapErrs is set when the format string may contain a %w verb.
	wrapErrs bool
	// wrappedErrs records the targets of the %w verb.
	wrappedErrs []int
}

var ppFree = sync.Pool{
	New: func() any { return new(pp) },
}

// newPrinter allocates a new pp struct or grabs a cached one.
func newPrinter() *pp {
	 := ppFree.Get().(*pp)
	.panicking = false
	.erroring = false
	.wrapErrs = false
	.fmt.init(&.buf)
	return 
}

// free saves used pp structs in ppFree; avoids an allocation per invocation.
func ( *pp) () {
	// Proper usage of a sync.Pool requires each entry to have approximately
	// the same memory cost. To obtain this property when the stored type
	// contains a variably-sized buffer, we add a hard limit on the maximum
	// buffer to place back in the pool. If the buffer is larger than the
	// limit, we drop the buffer and recycle just the printer.
	//
	// See https://golang.org/issue/23199.
	if cap(.buf) > 64*1024 {
		.buf = nil
	} else {
		.buf = .buf[:0]
	}
	if cap(.wrappedErrs) > 8 {
		.wrappedErrs = nil
	}

	.arg = nil
	.value = reflect.Value{}
	.wrappedErrs = .wrappedErrs[:0]
	ppFree.Put()
}

func ( *pp) () ( int,  bool) { return .fmt.wid, .fmt.widPresent }

func ( *pp) () ( int,  bool) { return .fmt.prec, .fmt.precPresent }

func ( *pp) ( int) bool {
	switch  {
	case '-':
		return .fmt.minus
	case '+':
		return .fmt.plus || .fmt.plusV
	case '#':
		return .fmt.sharp || .fmt.sharpV
	case ' ':
		return .fmt.space
	case '0':
		return .fmt.zero
	}
	return false
}

// Implement Write so we can call [Fprintf] on a pp (through [State]), for
// recursive use in custom verbs.
func ( *pp) ( []byte) ( int,  error) {
	.buf.write()
	return len(), nil
}

// Implement WriteString so that we can call [io.WriteString]
// on a pp (through state), for efficiency.
func ( *pp) ( string) ( int,  error) {
	.buf.writeString()
	return len(), nil
}

// These routines end in 'f' and take a format string.

// Fprintf formats according to a format specifier and writes to w.
// It returns the number of bytes written and any write error encountered.
func ( io.Writer,  string,  ...any) ( int,  error) {
	 := newPrinter()
	.doPrintf(, )
	,  = .Write(.buf)
	.free()
	return
}

// Printf formats according to a format specifier and writes to standard output.
// It returns the number of bytes written and any write error encountered.
func ( string,  ...any) ( int,  error) {
	return Fprintf(os.Stdout, , ...)
}

// Sprintf formats according to a format specifier and returns the resulting string.
func ( string,  ...any) string {
	 := newPrinter()
	.doPrintf(, )
	 := string(.buf)
	.free()
	return 
}

// Appendf formats according to a format specifier, appends the result to the byte
// slice, and returns the updated slice.
func ( []byte,  string,  ...any) []byte {
	 := newPrinter()
	.doPrintf(, )
	 = append(, .buf...)
	.free()
	return 
}

// These routines do not take a format string

// Fprint formats using the default formats for its operands and writes to w.
// Spaces are added between operands when neither is a string.
// It returns the number of bytes written and any write error encountered.
func ( io.Writer,  ...any) ( int,  error) {
	 := newPrinter()
	.doPrint()
	,  = .Write(.buf)
	.free()
	return
}

// Print formats using the default formats for its operands and writes to standard output.
// Spaces are added between operands when neither is a string.
// It returns the number of bytes written and any write error encountered.
func ( ...any) ( int,  error) {
	return Fprint(os.Stdout, ...)
}

// Sprint formats using the default formats for its operands and returns the resulting string.
// Spaces are added between operands when neither is a string.
func ( ...any) string {
	 := newPrinter()
	.doPrint()
	 := string(.buf)
	.free()
	return 
}

// Append formats using the default formats for its operands, appends the result to
// the byte slice, and returns the updated slice.
func ( []byte,  ...any) []byte {
	 := newPrinter()
	.doPrint()
	 = append(, .buf...)
	.free()
	return 
}

// These routines end in 'ln', do not take a format string,
// always add spaces between operands, and add a newline
// after the last operand.

// Fprintln formats using the default formats for its operands and writes to w.
// Spaces are always added between operands and a newline is appended.
// It returns the number of bytes written and any write error encountered.
func ( io.Writer,  ...any) ( int,  error) {
	 := newPrinter()
	.doPrintln()
	,  = .Write(.buf)
	.free()
	return
}

// Println formats using the default formats for its operands and writes to standard output.
// Spaces are always added between operands and a newline is appended.
// It returns the number of bytes written and any write error encountered.
func ( ...any) ( int,  error) {
	return Fprintln(os.Stdout, ...)
}

// Sprintln formats using the default formats for its operands and returns the resulting string.
// Spaces are always added between operands and a newline is appended.
func ( ...any) string {
	 := newPrinter()
	.doPrintln()
	 := string(.buf)
	.free()
	return 
}

// Appendln formats using the default formats for its operands, appends the result
// to the byte slice, and returns the updated slice. Spaces are always added
// between operands and a newline is appended.
func ( []byte,  ...any) []byte {
	 := newPrinter()
	.doPrintln()
	 = append(, .buf...)
	.free()
	return 
}

// getField gets the i'th field of the struct value.
// If the field itself is a non-nil interface, return a value for
// the thing inside the interface, not the interface itself.
func getField( reflect.Value,  int) reflect.Value {
	 := .Field()
	if .Kind() == reflect.Interface && !.IsNil() {
		 = .Elem()
	}
	return 
}

// tooLarge reports whether the magnitude of the integer is
// too large to be used as a formatting width or precision.
func tooLarge( int) bool {
	const  int = 1e6
	return  >  ||  < -
}

// parsenum converts ASCII to integer.  num is 0 (and isnum is false) if no number present.
func parsenum( string, ,  int) ( int,  bool,  int) {
	if  >=  {
		return 0, false, 
	}
	for  = ;  <  && '0' <= [] && [] <= '9'; ++ {
		if tooLarge() {
			return 0, false,  // Overflow; crazy long number most likely.
		}
		 = *10 + int([]-'0')
		 = true
	}
	return
}

func ( *pp) ( reflect.Value) {
	if !.IsValid() {
		.buf.writeString(nilAngleString)
		return
	}
	.buf.writeByte('?')
	.buf.writeString(.Type().String())
	.buf.writeByte('?')
}

func ( *pp) ( rune) {
	.erroring = true
	.buf.writeString(percentBangString)
	.buf.writeRune()
	.buf.writeByte('(')
	switch {
	case .arg != nil:
		.buf.writeString(reflect.TypeOf(.arg).String())
		.buf.writeByte('=')
		.printArg(.arg, 'v')
	case .value.IsValid():
		.buf.writeString(.value.Type().String())
		.buf.writeByte('=')
		.printValue(.value, 'v', 0)
	default:
		.buf.writeString(nilAngleString)
	}
	.buf.writeByte(')')
	.erroring = false
}

func ( *pp) ( bool,  rune) {
	switch  {
	case 't', 'v':
		.fmt.fmtBoolean()
	default:
		.badVerb()
	}
}

// fmt0x64 formats a uint64 in hexadecimal and prefixes it with 0x or
// not, as requested, by temporarily setting the sharp flag.
func ( *pp) ( uint64,  bool) {
	 := .fmt.sharp
	.fmt.sharp = 
	.fmt.fmtInteger(, 16, unsigned, 'v', ldigits)
	.fmt.sharp = 
}

// fmtInteger formats a signed or unsigned integer.
func ( *pp) ( uint64,  bool,  rune) {
	switch  {
	case 'v':
		if .fmt.sharpV && ! {
			.fmt0x64(, true)
		} else {
			.fmt.fmtInteger(, 10, , , ldigits)
		}
	case 'd':
		.fmt.fmtInteger(, 10, , , ldigits)
	case 'b':
		.fmt.fmtInteger(, 2, , , ldigits)
	case 'o', 'O':
		.fmt.fmtInteger(, 8, , , ldigits)
	case 'x':
		.fmt.fmtInteger(, 16, , , ldigits)
	case 'X':
		.fmt.fmtInteger(, 16, , , udigits)
	case 'c':
		.fmt.fmtC()
	case 'q':
		.fmt.fmtQc()
	case 'U':
		.fmt.fmtUnicode()
	default:
		.badVerb()
	}
}

// fmtFloat formats a float. The default precision for each verb
// is specified as last argument in the call to fmt_float.
func ( *pp) ( float64,  int,  rune) {
	switch  {
	case 'v':
		.fmt.fmtFloat(, , 'g', -1)
	case 'b', 'g', 'G', 'x', 'X':
		.fmt.fmtFloat(, , , -1)
	case 'f', 'e', 'E':
		.fmt.fmtFloat(, , , 6)
	case 'F':
		.fmt.fmtFloat(, , 'f', 6)
	default:
		.badVerb()
	}
}

// fmtComplex formats a complex number v with
// r = real(v) and j = imag(v) as (r+ji) using
// fmtFloat for r and j formatting.
func ( *pp) ( complex128,  int,  rune) {
	// Make sure any unsupported verbs are found before the
	// calls to fmtFloat to not generate an incorrect error string.
	switch  {
	case 'v', 'b', 'g', 'G', 'x', 'X', 'f', 'F', 'e', 'E':
		 := .fmt.plus
		.buf.writeByte('(')
		.fmtFloat(real(), /2, )
		// Imaginary part always has a sign.
		.fmt.plus = true
		.fmtFloat(imag(), /2, )
		.buf.writeString("i)")
		.fmt.plus = 
	default:
		.badVerb()
	}
}

func ( *pp) ( string,  rune) {
	switch  {
	case 'v':
		if .fmt.sharpV {
			.fmt.fmtQ()
		} else {
			.fmt.fmtS()
		}
	case 's':
		.fmt.fmtS()
	case 'x':
		.fmt.fmtSx(, ldigits)
	case 'X':
		.fmt.fmtSx(, udigits)
	case 'q':
		.fmt.fmtQ()
	default:
		.badVerb()
	}
}

func ( *pp) ( []byte,  rune,  string) {
	switch  {
	case 'v', 'd':
		if .fmt.sharpV {
			.buf.writeString()
			if  == nil {
				.buf.writeString(nilParenString)
				return
			}
			.buf.writeByte('{')
			for ,  := range  {
				if  > 0 {
					.buf.writeString(commaSpaceString)
				}
				.fmt0x64(uint64(), true)
			}
			.buf.writeByte('}')
		} else {
			.buf.writeByte('[')
			for ,  := range  {
				if  > 0 {
					.buf.writeByte(' ')
				}
				.fmt.fmtInteger(uint64(), 10, unsigned, , ldigits)
			}
			.buf.writeByte(']')
		}
	case 's':
		.fmt.fmtBs()
	case 'x':
		.fmt.fmtBx(, ldigits)
	case 'X':
		.fmt.fmtBx(, udigits)
	case 'q':
		.fmt.fmtQ(string())
	default:
		.printValue(reflect.ValueOf(), , 0)
	}
}

func ( *pp) ( reflect.Value,  rune) {
	var  uintptr
	switch .Kind() {
	case reflect.Chan, reflect.Func, reflect.Map, reflect.Pointer, reflect.Slice, reflect.UnsafePointer:
		 = uintptr(.UnsafePointer())
	default:
		.badVerb()
		return
	}

	switch  {
	case 'v':
		if .fmt.sharpV {
			.buf.writeByte('(')
			.buf.writeString(.Type().String())
			.buf.writeString(")(")
			if  == 0 {
				.buf.writeString(nilString)
			} else {
				.fmt0x64(uint64(), true)
			}
			.buf.writeByte(')')
		} else {
			if  == 0 {
				.fmt.padString(nilAngleString)
			} else {
				.fmt0x64(uint64(), !.fmt.sharp)
			}
		}
	case 'p':
		.fmt0x64(uint64(), !.fmt.sharp)
	case 'b', 'o', 'd', 'x', 'X':
		.fmtInteger(uint64(), unsigned, )
	default:
		.badVerb()
	}
}

func ( *pp) ( any,  rune,  string) {
	if  := recover();  != nil {
		// If it's a nil pointer, just say "<nil>". The likeliest causes are a
		// Stringer that fails to guard against nil or a nil pointer for a
		// value receiver, and in either case, "<nil>" is a nice result.
		if  := reflect.ValueOf(); .Kind() == reflect.Pointer && .IsNil() {
			.buf.writeString(nilAngleString)
			return
		}
		// Otherwise print a concise panic message. Most of the time the panic
		// value will print itself nicely.
		if .panicking {
			// Nested panics; the recursion in printArg cannot succeed.
			panic()
		}

		 := .fmt.fmtFlags
		// For this output we want default behavior.
		.fmt.clearflags()

		.buf.writeString(percentBangString)
		.buf.writeRune()
		.buf.writeString(panicString)
		.buf.writeString()
		.buf.writeString(" method: ")
		.panicking = true
		.printArg(, 'v')
		.panicking = false
		.buf.writeByte(')')

		.fmt.fmtFlags = 
	}
}

func ( *pp) ( rune) ( bool) {
	if .erroring {
		return
	}
	if  == 'w' {
		// It is invalid to use %w other than with Errorf or with a non-error arg.
		,  := .arg.(error)
		if ! || !.wrapErrs {
			.badVerb()
			return true
		}
		// If the arg is a Formatter, pass 'v' as the verb to it.
		 = 'v'
	}

	// Is it a Formatter?
	if ,  := .arg.(Formatter);  {
		 = true
		defer .catchPanic(.arg, , "Format")
		.Format(, )
		return
	}

	// If we're doing Go syntax and the argument knows how to supply it, take care of it now.
	if .fmt.sharpV {
		if ,  := .arg.(GoStringer);  {
			 = true
			defer .catchPanic(.arg, , "GoString")
			// Print the result of GoString unadorned.
			.fmt.fmtS(.GoString())
			return
		}
	} else {
		// If a string is acceptable according to the format, see if
		// the value satisfies one of the string-valued interfaces.
		// Println etc. set verb to %v, which is "stringable".
		switch  {
		case 'v', 's', 'x', 'X', 'q':
			// Is it an error or Stringer?
			// The duplication in the bodies is necessary:
			// setting handled and deferring catchPanic
			// must happen before calling the method.
			switch v := .arg.(type) {
			case error:
				 = true
				defer .catchPanic(.arg, , "Error")
				.fmtString(.Error(), )
				return

			case Stringer:
				 = true
				defer .catchPanic(.arg, , "String")
				.fmtString(.String(), )
				return
			}
		}
	}
	return false
}

func ( *pp) ( any,  rune) {
	.arg = 
	.value = reflect.Value{}

	if  == nil {
		switch  {
		case 'T', 'v':
			.fmt.padString(nilAngleString)
		default:
			.badVerb()
		}
		return
	}

	// Special processing considerations.
	// %T (the value's type) and %p (its address) are special; we always do them first.
	switch  {
	case 'T':
		.fmt.fmtS(reflect.TypeOf().String())
		return
	case 'p':
		.fmtPointer(reflect.ValueOf(), 'p')
		return
	}

	// Some types can be done without reflection.
	switch f := .(type) {
	case bool:
		.fmtBool(, )
	case float32:
		.fmtFloat(float64(), 32, )
	case float64:
		.fmtFloat(, 64, )
	case complex64:
		.fmtComplex(complex128(), 64, )
	case complex128:
		.fmtComplex(, 128, )
	case int:
		.fmtInteger(uint64(), signed, )
	case int8:
		.fmtInteger(uint64(), signed, )
	case int16:
		.fmtInteger(uint64(), signed, )
	case int32:
		.fmtInteger(uint64(), signed, )
	case int64:
		.fmtInteger(uint64(), signed, )
	case uint:
		.fmtInteger(uint64(), unsigned, )
	case uint8:
		.fmtInteger(uint64(), unsigned, )
	case uint16:
		.fmtInteger(uint64(), unsigned, )
	case uint32:
		.fmtInteger(uint64(), unsigned, )
	case uint64:
		.fmtInteger(, unsigned, )
	case uintptr:
		.fmtInteger(uint64(), unsigned, )
	case string:
		.fmtString(, )
	case []byte:
		.fmtBytes(, , "[]byte")
	case reflect.Value:
		// Handle extractable values with special methods
		// since printValue does not handle them at depth 0.
		if .IsValid() && .CanInterface() {
			.arg = .Interface()
			if .handleMethods() {
				return
			}
		}
		.printValue(, , 0)
	default:
		// If the type is not simple, it might have methods.
		if !.handleMethods() {
			// Need to use reflection, since the type had no
			// interface methods that could be used for formatting.
			.printValue(reflect.ValueOf(), , 0)
		}
	}
}

// printValue is similar to printArg but starts with a reflect value, not an interface{} value.
// It does not handle 'p' and 'T' verbs because these should have been already handled by printArg.
func ( *pp) ( reflect.Value,  rune,  int) {
	// Handle values with special methods if not already handled by printArg (depth == 0).
	if  > 0 && .IsValid() && .CanInterface() {
		.arg = .Interface()
		if .handleMethods() {
			return
		}
	}
	.arg = nil
	.value = 

	switch  := ; .Kind() {
	case reflect.Invalid:
		if  == 0 {
			.buf.writeString(invReflectString)
		} else {
			switch  {
			case 'v':
				.buf.writeString(nilAngleString)
			default:
				.badVerb()
			}
		}
	case reflect.Bool:
		.fmtBool(.Bool(), )
	case reflect.Int, reflect.Int8, reflect.Int16, reflect.Int32, reflect.Int64:
		.fmtInteger(uint64(.Int()), signed, )
	case reflect.Uint, reflect.Uint8, reflect.Uint16, reflect.Uint32, reflect.Uint64, reflect.Uintptr:
		.fmtInteger(.Uint(), unsigned, )
	case reflect.Float32:
		.fmtFloat(.Float(), 32, )
	case reflect.Float64:
		.fmtFloat(.Float(), 64, )
	case reflect.Complex64:
		.fmtComplex(.Complex(), 64, )
	case reflect.Complex128:
		.fmtComplex(.Complex(), 128, )
	case reflect.String:
		.fmtString(.String(), )
	case reflect.Map:
		if .fmt.sharpV {
			.buf.writeString(.Type().String())
			if .IsNil() {
				.buf.writeString(nilParenString)
				return
			}
			.buf.writeByte('{')
		} else {
			.buf.writeString(mapString)
		}
		 := fmtsort.Sort()
		for ,  := range  {
			if  > 0 {
				if .fmt.sharpV {
					.buf.writeString(commaSpaceString)
				} else {
					.buf.writeByte(' ')
				}
			}
			.(.Key, , +1)
			.buf.writeByte(':')
			.(.Value, , +1)
		}
		if .fmt.sharpV {
			.buf.writeByte('}')
		} else {
			.buf.writeByte(']')
		}
	case reflect.Struct:
		if .fmt.sharpV {
			.buf.writeString(.Type().String())
		}
		.buf.writeByte('{')
		for  := 0;  < .NumField(); ++ {
			if  > 0 {
				if .fmt.sharpV {
					.buf.writeString(commaSpaceString)
				} else {
					.buf.writeByte(' ')
				}
			}
			if .fmt.plusV || .fmt.sharpV {
				if  := .Type().Field().Name;  != "" {
					.buf.writeString()
					.buf.writeByte(':')
				}
			}
			.(getField(, ), , +1)
		}
		.buf.writeByte('}')
	case reflect.Interface:
		 := .Elem()
		if !.IsValid() {
			if .fmt.sharpV {
				.buf.writeString(.Type().String())
				.buf.writeString(nilParenString)
			} else {
				.buf.writeString(nilAngleString)
			}
		} else {
			.(, , +1)
		}
	case reflect.Array, reflect.Slice:
		switch  {
		case 's', 'q', 'x', 'X':
			// Handle byte and uint8 slices and arrays special for the above verbs.
			 := .Type()
			if .Elem().Kind() == reflect.Uint8 {
				var  []byte
				if .Kind() == reflect.Slice || .CanAddr() {
					 = .Bytes()
				} else {
					// We have an array, but we cannot Bytes() a non-addressable array,
					// so we build a slice by hand. This is a rare case but it would be nice
					// if reflection could help a little more.
					 = make([]byte, .Len())
					for  := range  {
						[] = byte(.Index().Uint())
					}
				}
				.fmtBytes(, , .String())
				return
			}
		}
		if .fmt.sharpV {
			.buf.writeString(.Type().String())
			if .Kind() == reflect.Slice && .IsNil() {
				.buf.writeString(nilParenString)
				return
			}
			.buf.writeByte('{')
			for  := 0;  < .Len(); ++ {
				if  > 0 {
					.buf.writeString(commaSpaceString)
				}
				.(.Index(), , +1)
			}
			.buf.writeByte('}')
		} else {
			.buf.writeByte('[')
			for  := 0;  < .Len(); ++ {
				if  > 0 {
					.buf.writeByte(' ')
				}
				.(.Index(), , +1)
			}
			.buf.writeByte(']')
		}
	case reflect.Pointer:
		// pointer to array or slice or struct? ok at top level
		// but not embedded (avoid loops)
		if  == 0 && .UnsafePointer() != nil {
			switch  := .Elem(); .Kind() {
			case reflect.Array, reflect.Slice, reflect.Struct, reflect.Map:
				.buf.writeByte('&')
				.(, , +1)
				return
			}
		}
		fallthrough
	case reflect.Chan, reflect.Func, reflect.UnsafePointer:
		.fmtPointer(, )
	default:
		.unknownType()
	}
}

// intFromArg gets the argNumth element of a. On return, isInt reports whether the argument has integer type.
func intFromArg( []any,  int) ( int,  bool,  int) {
	 = 
	if  < len() {
		,  = [].(int) // Almost always OK.
		if ! {
			// Work harder.
			switch  := reflect.ValueOf([]); .Kind() {
			case reflect.Int, reflect.Int8, reflect.Int16, reflect.Int32, reflect.Int64:
				 := .Int()
				if int64(int()) ==  {
					 = int()
					 = true
				}
			case reflect.Uint, reflect.Uint8, reflect.Uint16, reflect.Uint32, reflect.Uint64, reflect.Uintptr:
				 := .Uint()
				if int64() >= 0 && uint64(int()) ==  {
					 = int()
					 = true
				}
			default:
				// Already 0, false.
			}
		}
		 =  + 1
		if tooLarge() {
			 = 0
			 = false
		}
	}
	return
}

// parseArgNumber returns the value of the bracketed number, minus 1
// (explicit argument numbers are one-indexed but we want zero-indexed).
// The opening bracket is known to be present at format[0].
// The returned values are the index, the number of bytes to consume
// up to the closing paren, if present, and whether the number parsed
// ok. The bytes to consume will be 1 if no closing paren is present.
func parseArgNumber( string) ( int,  int,  bool) {
	// There must be at least 3 bytes: [n].
	if len() < 3 {
		return 0, 1, false
	}

	// Find closing bracket.
	for  := 1;  < len(); ++ {
		if [] == ']' {
			, ,  := parsenum(, 1, )
			if ! ||  !=  {
				return 0,  + 1, false
			}
			return  - 1,  + 1, true // arg numbers are one-indexed and skip paren.
		}
	}
	return 0, 1, false
}

// argNumber returns the next argument to evaluate, which is either the value of the passed-in
// argNum or the value of the bracketed integer that begins format[i:]. It also returns
// the new value of i, that is, the index of the next byte of the format to process.
func ( *pp) ( int,  string,  int,  int) (,  int,  bool) {
	if len() <=  || [] != '[' {
		return , , false
	}
	.reordered = true
	, ,  := parseArgNumber([:])
	if  && 0 <=  &&  <  {
		return ,  + , true
	}
	.goodArgNum = false
	return ,  + , 
}

func ( *pp) ( rune) {
	.buf.writeString(percentBangString)
	.buf.writeRune()
	.buf.writeString(badIndexString)
}

func ( *pp) ( rune) {
	.buf.writeString(percentBangString)
	.buf.writeRune()
	.buf.writeString(missingString)
}

func ( *pp) ( string,  []any) {
	 := len()
	 := 0         // we process one argument per non-trivial format
	 := false // previous item in format was an index like [3].
	.reordered = false
:
	for  := 0;  < ; {
		.goodArgNum = true
		 := 
		for  <  && [] != '%' {
			++
		}
		if  >  {
			.buf.writeString([:])
		}
		if  >=  {
			// done processing format string
			break
		}

		// Process one verb
		++

		// Do we have flags?
		.fmt.clearflags()
	:
		for ;  < ; ++ {
			 := []
			switch  {
			case '#':
				.fmt.sharp = true
			case '0':
				.fmt.zero = true
			case '+':
				.fmt.plus = true
			case '-':
				.fmt.minus = true
			case ' ':
				.fmt.space = true
			default:
				// Fast path for common case of ascii lower case simple verbs
				// without precision or width or argument indices.
				if 'a' <=  &&  <= 'z' &&  < len() {
					switch  {
					case 'w':
						.wrappedErrs = append(.wrappedErrs, )
						fallthrough
					case 'v':
						// Go syntax
						.fmt.sharpV = .fmt.sharp
						.fmt.sharp = false
						// Struct-field syntax
						.fmt.plusV = .fmt.plus
						.fmt.plus = false
					}
					.printArg([], rune())
					++
					++
					continue 
				}
				// Format is more complex than simple flags and a verb or is malformed.
				break 
			}
		}

		// Do we have an explicit argument index?
		, ,  = .argNumber(, , , len())

		// Do we have width?
		if  <  && [] == '*' {
			++
			.fmt.wid, .fmt.widPresent,  = intFromArg(, )

			if !.fmt.widPresent {
				.buf.writeString(badWidthString)
			}

			// We have a negative width, so take its value and ensure
			// that the minus flag is set
			if .fmt.wid < 0 {
				.fmt.wid = -.fmt.wid
				.fmt.minus = true
				.fmt.zero = false // Do not pad with zeros to the right.
			}
			 = false
		} else {
			.fmt.wid, .fmt.widPresent,  = parsenum(, , )
			if  && .fmt.widPresent { // "%[3]2d"
				.goodArgNum = false
			}
		}

		// Do we have precision?
		if +1 <  && [] == '.' {
			++
			if  { // "%[3].2d"
				.goodArgNum = false
			}
			, ,  = .argNumber(, , , len())
			if  <  && [] == '*' {
				++
				.fmt.prec, .fmt.precPresent,  = intFromArg(, )
				// Negative precision arguments don't make sense
				if .fmt.prec < 0 {
					.fmt.prec = 0
					.fmt.precPresent = false
				}
				if !.fmt.precPresent {
					.buf.writeString(badPrecString)
				}
				 = false
			} else {
				.fmt.prec, .fmt.precPresent,  = parsenum(, , )
				if !.fmt.precPresent {
					.fmt.prec = 0
					.fmt.precPresent = true
				}
			}
		}

		if ! {
			, ,  = .argNumber(, , , len())
		}

		if  >=  {
			.buf.writeString(noVerbString)
			break
		}

		,  := rune([]), 1
		if  >= utf8.RuneSelf {
			,  = utf8.DecodeRuneInString([:])
		}
		 += 

		switch {
		case  == '%': // Percent does not absorb operands and ignores f.wid and f.prec.
			.buf.writeByte('%')
		case !.goodArgNum:
			.badArgNum()
		case  >= len(): // No argument left over to print for the current verb.
			.missingArg()
		case  == 'w':
			.wrappedErrs = append(.wrappedErrs, )
			fallthrough
		case  == 'v':
			// Go syntax
			.fmt.sharpV = .fmt.sharp
			.fmt.sharp = false
			// Struct-field syntax
			.fmt.plusV = .fmt.plus
			.fmt.plus = false
			fallthrough
		default:
			.printArg([], )
			++
		}
	}

	// Check for extra arguments unless the call accessed the arguments
	// out of order, in which case it's too expensive to detect if they've all
	// been used and arguably OK if they're not.
	if !.reordered &&  < len() {
		.fmt.clearflags()
		.buf.writeString(extraString)
		for ,  := range [:] {
			if  > 0 {
				.buf.writeString(commaSpaceString)
			}
			if  == nil {
				.buf.writeString(nilAngleString)
			} else {
				.buf.writeString(reflect.TypeOf().String())
				.buf.writeByte('=')
				.printArg(, 'v')
			}
		}
		.buf.writeByte(')')
	}
}

func ( *pp) ( []any) {
	 := false
	for ,  := range  {
		 :=  != nil && reflect.TypeOf().Kind() == reflect.String
		// Add a space between two non-string arguments.
		if  > 0 && ! && ! {
			.buf.writeByte(' ')
		}
		.printArg(, 'v')
		 = 
	}
}

// doPrintln is like doPrint but always adds a space between arguments
// and a newline after the last argument.
func ( *pp) ( []any) {
	for ,  := range  {
		if  > 0 {
			.buf.writeByte(' ')
		}
		.printArg(, 'v')
	}
	.buf.writeByte('\n')
}