Imports #
"strconv"
"unicode/utf8"
"internal/fmtsort"
"io"
"os"
"reflect"
"strconv"
"sync"
"unicode/utf8"
"errors"
"io"
"math"
"os"
"reflect"
"strconv"
"sync"
"unicode/utf8"
"errors"
"slices"
"strconv"
"unicode/utf8"
"internal/fmtsort"
"io"
"os"
"reflect"
"strconv"
"sync"
"unicode/utf8"
"errors"
"io"
"math"
"os"
"reflect"
"strconv"
"sync"
"unicode/utf8"
"errors"
"slices"
Strings for use with buffer.WriteString. This is less overhead than using buffer.Write with byte arrays.
const badIndexString = "(BADINDEX)"
Strings for use with buffer.WriteString. This is less overhead than using buffer.Write with byte arrays.
const badPrecString = "%!(BADPREC)"
Strings for use with buffer.WriteString. This is less overhead than using buffer.Write with byte arrays.
const badWidthString = "%!(BADWIDTH)"
Numerical elements
const binaryDigits = "01"
Strings for use with buffer.WriteString. This is less overhead than using buffer.Write with byte arrays.
const commaSpaceString = ", "
Numerical elements
const decimalDigits = "0123456789"
const eof = *ast.UnaryExpr
var errBool = *ast.CallExpr
var errComplex = *ast.CallExpr
Numerical elements
const exponent = "eEpP"
Strings for use with buffer.WriteString. This is less overhead than using buffer.Write with byte arrays.
const extraString = "%!(EXTRA "
const floatVerbs = "beEfFgGv"
Numerical elements
const hexadecimalDigits = "0123456789aAbBcCdDeEfF"
const hugeWid = *ast.BinaryExpr
const intBits = *ast.BinaryExpr
Strings for use with buffer.WriteString. This is less overhead than using buffer.Write with byte arrays.
const invReflectString = ""
const ldigits = "0123456789abcdefx"
Strings for use with buffer.WriteString. This is less overhead than using buffer.Write with byte arrays.
const mapString = "map["
Strings for use with buffer.WriteString. This is less overhead than using buffer.Write with byte arrays.
const missingString = "(MISSING)"
Strings for use with buffer.WriteString. This is less overhead than using buffer.Write with byte arrays.
const nilAngleString = ""
Strings for use with buffer.WriteString. This is less overhead than using buffer.Write with byte arrays.
const nilParenString = "(nil)"
Strings for use with buffer.WriteString. This is less overhead than using buffer.Write with byte arrays.
const nilString = "nil"
Strings for use with buffer.WriteString. This is less overhead than using buffer.Write with byte arrays.
const noVerbString = "%!(NOVERB)"
Numerical elements
const octalDigits = "01234567"
Strings for use with buffer.WriteString. This is less overhead than using buffer.Write with byte arrays.
const panicString = "(PANIC="
Strings for use with buffer.WriteString. This is less overhead than using buffer.Write with byte arrays.
const percentBangString = "%!"
Numerical elements
const period = "."
var ppFree = sync.Pool{...}
Numerical elements
const sign = "+-"
const signed = true
space is a copy of the unicode.White_Space ranges, to avoid depending on package unicode.
var space = [][2]uint16{...}
var ssFree = sync.Pool{...}
const udigits = "0123456789ABCDEFX"
const uintptrBits = *ast.BinaryExpr
const unsigned = false
Use simple []byte instead of bytes.Buffer to avoid large dependency.
type buffer []byte
type stringReader string
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)
}
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
}
ScanState represents the scanner state passed to custom scanners. Scanners may do rune-at-a-time scanning or ask the ScanState to discover the next space-delimited token.
type ScanState interface {
ReadRune() (r rune, size int, err error)
UnreadRune() error
SkipSpace()
Token(skipSpace bool, f func(rune) bool) (token []byte, err error)
Width() (wid int, ok bool)
Read(buf []byte) (n int, err error)
}
Scanner is implemented by any value that has a Scan method, which scans the input for the representation of a value and stores the result in the receiver, which must be a pointer to be useful. The Scan method is called for any argument to [Scan], [Scanf], or [Scanln] that implements it.
type Scanner interface {
Scan(state ScanState, verb rune) error
}
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(b []byte) (n int, err error)
Width() (wid int, ok bool)
Precision() (prec int, ok bool)
Flag(c int) bool
}
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
}
A fmt is the raw formatter used by Printf etc. It prints into a buffer that must be set up separately.
type fmt struct {
buf *buffer
fmtFlags
wid int
prec int
intbuf [68]byte
}
flags placed in a separate struct for easy clearing.
type fmtFlags struct {
widPresent bool
precPresent bool
minus bool
plus bool
sharp bool
space bool
zero bool
plusV bool
sharpV bool
}
pp is used to store a printer's state and is reused with sync.Pool to avoid allocations.
type pp struct {
buf buffer
arg any
value reflect.Value
fmt fmt
reordered bool
goodArgNum bool
panicking bool
erroring bool
wrapErrs bool
wrappedErrs []int
}
readRune is a structure to enable reading UTF-8 encoded code points from an io.Reader. It is used if the Reader given to the scanner does not already implement io.RuneScanner.
type readRune struct {
reader io.Reader
buf [utf8.UTFMax]byte
pending int
pendBuf [utf8.UTFMax]byte
peekRune rune
}
scanError represents an error generated by the scanning software. It's used as a unique signature to identify such errors when recovering.
type scanError struct {
err error
}
ss is the internal implementation of ScanState.
type ss struct {
rs io.RuneScanner
buf buffer
count int
atEOF bool
ssave
}
ssave holds the parts of ss that need to be saved and restored on recursive scans.
type ssave struct {
validSave bool
nlIsEnd bool
nlIsSpace bool
argLimit int
limit int
maxWid int
}
type wrapError struct {
msg string
err error
}
type wrapErrors struct {
msg string
errs []error
}
Append formats using the default formats for its operands, appends the result to the byte slice, and returns the updated slice.
func Append(b []byte, a ...any) []byte
Appendf formats according to a format specifier, appends the result to the byte slice, and returns the updated slice.
func Appendf(b []byte, format string, a ...any) []byte
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 Appendln(b []byte, a ...any) []byte
func (e *wrapErrors) Error() string
func (e *wrapError) Error() string
Errorf formats according to a format specifier and returns the string as a value that satisfies error. If the format specifier includes a %w verb with an error operand, the returned error will implement an Unwrap method returning the operand. If there is more than one %w verb, the returned error will implement an Unwrap method returning a []error containing all the %w operands in the order they appear in the arguments. It is invalid to supply the %w verb with an operand that does not implement the error interface. The %w verb is otherwise a synonym for %v.
func Errorf(format string, a ...any) error
func (p *pp) Flag(b int) bool
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 FormatString(state State, verb rune) 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 Fprint(w io.Writer, a ...any) (n int, err error)
Fprintf formats according to a format specifier and writes to w. It returns the number of bytes written and any write error encountered.
func Fprintf(w io.Writer, format string, a ...any) (n int, err error)
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 Fprintln(w io.Writer, a ...any) (n int, err error)
Fscan scans text read from r, storing successive space-separated values into successive arguments. Newlines count as space. It returns the number of items successfully scanned. If that is less than the number of arguments, err will report why.
func Fscan(r io.Reader, a ...any) (n int, err error)
Fscanf scans text read from r, storing successive space-separated values into successive arguments as determined by the format. It returns the number of items successfully parsed. Newlines in the input must match newlines in the format.
func Fscanf(r io.Reader, format string, a ...any) (n int, err error)
Fscanln is similar to [Fscan], but stops scanning at a newline and after the final item there must be a newline or EOF.
func Fscanln(r io.Reader, a ...any) (n int, err error)
func (p *pp) Precision() (prec int, ok bool)
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 Print(a ...any) (n int, err error)
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 Printf(format string, a ...any) (n int, err error)
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 Println(a ...any) (n int, err error)
The Read method is only in ScanState so that ScanState satisfies io.Reader. It will never be called when used as intended, so there is no need to make it actually work.
func (s *ss) Read(buf []byte) (n int, err error)
func (r *stringReader) Read(b []byte) (n int, err error)
func (s *ss) ReadRune() (r rune, size int, err error)
ReadRune returns the next UTF-8 encoded code point from the io.Reader inside r.
func (r *readRune) ReadRune() (rr rune, size int, err error)
Scan scans text read from standard input, storing successive space-separated values into successive arguments. Newlines count as space. It returns the number of items successfully scanned. If that is less than the number of arguments, err will report why.
func Scan(a ...any) (n int, err error)
Scanf scans text read from standard input, storing successive space-separated values into successive arguments as determined by the format. It returns the number of items successfully scanned. If that is less than the number of arguments, err will report why. Newlines in the input must match newlines in the format. The one exception: the verb %c always scans the next rune in the input, even if it is a space (or tab etc.) or newline.
func Scanf(format string, a ...any) (n int, err error)
Scanln is similar to [Scan], but stops scanning at a newline and after the final item there must be a newline or EOF.
func Scanln(a ...any) (n int, err error)
SkipSpace provides Scan methods the ability to skip space and newline characters in keeping with the current scanning mode set by format strings and [Scan]/[Scanln].
func (s *ss) SkipSpace()
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 Sprint(a ...any) string
Sprintf formats according to a format specifier and returns the resulting string.
func Sprintf(format string, a ...any) string
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 Sprintln(a ...any) string
Sscan scans the argument string, storing successive space-separated values into successive arguments. Newlines count as space. It returns the number of items successfully scanned. If that is less than the number of arguments, err will report why.
func Sscan(str string, a ...any) (n int, err error)
Sscanf scans the argument string, storing successive space-separated values into successive arguments as determined by the format. It returns the number of items successfully parsed. Newlines in the input must match newlines in the format.
func Sscanf(str string, format string, a ...any) (n int, err error)
Sscanln is similar to [Sscan], but stops scanning at a newline and after the final item there must be a newline or EOF.
func Sscanln(str string, a ...any) (n int, err error)
func (s *ss) Token(skipSpace bool, f func(rune) bool) (tok []byte, err error)
func (s *ss) UnreadRune() error
func (r *readRune) UnreadRune() error
func (e *wrapErrors) Unwrap() []error
func (e *wrapError) Unwrap() error
func (s *ss) Width() (wid int, ok bool)
func (p *pp) Width() (wid int, ok bool)
Write implements [io.Writer] so we can call [Fprintf] on a pp (through [State]), for recursive use in custom verbs.
func (p *pp) Write(b []byte) (ret int, err error)
WriteString implements [io.StringWriter] so that we can call [io.WriteString] on a pp (through state), for efficiency.
func (p *pp) WriteString(s string) (ret int, err error)
accept checks the next rune in the input. If it's a byte (sic) in the string, it puts it in the buffer and returns true. Otherwise it return false.
func (s *ss) accept(ok string) bool
advance determines whether the next characters in the input match those of the format. It returns the number of bytes (sic) consumed in the format. All runs of space characters in either input or format behave as a single space. Newlines are special, though: newlines in the format must match those in the input and vice versa. This routine also handles the %% case. If the return value is zero, either format starts with a % (with no following %) or the input is empty. If it is negative, the input did not match the string.
func (s *ss) advance(format string) (i int)
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 (p *pp) argNumber(argNum int, format string, i int, numArgs int) (newArgNum int, newi int, found bool)
func (p *pp) badArgNum(verb rune)
func (p *pp) badVerb(verb rune)
func (p *pp) catchPanic(arg any, verb rune, method string)
func (f *fmt) clearflags()
complexTokens returns the real and imaginary parts of the complex number starting here. The number might be parenthesized and has the format (N+Ni) where N is a floating-point number and there are no spaces within.
func (s *ss) complexTokens() (real string, imag string)
consume reads the next rune in the input and reports whether it is in the ok string. If accept is true, it puts the character into the input token.
func (s *ss) consume(ok string, accept bool) bool
convertFloat converts the string to a float64value.
func (s *ss) convertFloat(str string, n int) float64
convertString returns the string represented by the next input characters. The format of the input is determined by the verb.
func (s *ss) convertString(verb rune) (str string)
func (p *pp) doPrint(a []any)
func (p *pp) doPrintf(format string, a []any)
doPrintln is like doPrint but always adds a space between arguments and a newline after the last argument.
func (p *pp) doPrintln(a []any)
doScan does the real work for scanning without a format string.
func (s *ss) doScan(a []any) (numProcessed int, err error)
doScanf does the real work when scanning with a format string. At the moment, it handles only pointers to basic types.
func (s *ss) doScanf(format string, a []any) (numProcessed int, err error)
func (s *ss) error(err error)
errorHandler turns local panics into error returns.
func errorHandler(errp *error)
func (s *ss) errorString(err string)
floatToken returns the floating-point number starting here, no longer than swid if the width is specified. It's not rigorous about syntax because it doesn't check that we have at least some digits, but Atof will do that.
func (s *ss) floatToken() string
fmt0x64 formats a uint64 in hexadecimal and prefixes it with 0x or not, as requested, by temporarily setting the sharp flag.
func (p *pp) fmt0x64(v uint64, leading0x bool)
func (p *pp) fmtBool(v bool, verb rune)
fmtBoolean formats a boolean.
func (f *fmt) fmtBoolean(v bool)
fmtBs formats the byte slice b as if it was formatted as string with fmtS.
func (f *fmt) fmtBs(b []byte)
fmtBx formats a byte slice as a hexadecimal encoding of its bytes.
func (f *fmt) fmtBx(b []byte, digits string)
func (p *pp) fmtBytes(v []byte, verb rune, typeString string)
fmtC formats an integer as a Unicode character. If the character is not valid Unicode, it will print '\ufffd'.
func (f *fmt) fmtC(c uint64)
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 (p *pp) fmtComplex(v complex128, size int, verb rune)
fmtFloat formats a float64. It assumes that verb is a valid format specifier for strconv.AppendFloat and therefore fits into a byte.
func (f *fmt) fmtFloat(v float64, size int, verb rune, prec int)
fmtFloat formats a float. The default precision for each verb is specified as last argument in the call to fmt_float.
func (p *pp) fmtFloat(v float64, size int, verb rune)
fmtInteger formats signed and unsigned integers.
func (f *fmt) fmtInteger(u uint64, base int, isSigned bool, verb rune, digits string)
fmtInteger formats a signed or unsigned integer.
func (p *pp) fmtInteger(v uint64, isSigned bool, verb rune)
func (p *pp) fmtPointer(value reflect.Value, verb rune)
fmtQ formats a string as a double-quoted, escaped Go string constant. If f.sharp is set a raw (backquoted) string may be returned instead if the string does not contain any control characters other than tab.
func (f *fmt) fmtQ(s string)
fmtQc formats an integer as a single-quoted, escaped Go character constant. If the character is not valid Unicode, it will print '\ufffd'.
func (f *fmt) fmtQc(c uint64)
fmtS formats a string.
func (f *fmt) fmtS(s string)
fmtSbx formats a string or byte slice as a hexadecimal encoding of its bytes.
func (f *fmt) fmtSbx(s string, b []byte, digits string)
func (p *pp) fmtString(v string, verb rune)
fmtSx formats a string as a hexadecimal encoding of its bytes.
func (f *fmt) fmtSx(s string, digits string)
fmtUnicode formats a uint64 as "U+0078" or with f.sharp set as "U+0078 'x'".
func (f *fmt) fmtUnicode(u uint64)
free saves used pp structs in ppFree; avoids an allocation per invocation.
func (p *pp) free()
free saves used ss structs in ssFree; avoid an allocation per invocation.
func (s *ss) free(old ssave)
getBase returns the numeric base represented by the verb and its digit string.
func (s *ss) getBase(verb rune) (base int, digits string)
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(v reflect.Value, i int) reflect.Value
The public method returns an error; this private one panics. If getRune reaches EOF, the return value is EOF (-1).
func (s *ss) getRune() (r rune)
func (p *pp) handleMethods(verb rune) (handled bool)
func hasX(s string) bool
hexByte returns the next hex-encoded (two-character) byte from the input. It returns ok==false if the next bytes in the input do not encode a hex byte. If the first byte is hex and the second is not, processing stops.
func (s *ss) hexByte() (b byte, ok bool)
hexDigit returns the value of the hexadecimal digit.
func hexDigit(d rune) (int, bool)
hexString returns the space-delimited hexpair-encoded string.
func (s *ss) hexString() string
func indexRune(s string, r rune) int
func (f *fmt) init(buf *buffer)
intFromArg gets the argNumth element of a. On return, isInt reports whether the argument has integer type.
func intFromArg(a []any, argNum int) (num int, isInt bool, newArgNum int)
func isSpace(r rune) bool
func (p *pp) missingArg(verb rune)
mustReadRune turns io.EOF into a panic(io.ErrUnexpectedEOF). It is called in cases such as string scanning where an EOF is a syntax error.
func (s *ss) mustReadRune() (r rune)
newPrinter allocates a new pp struct or grabs a cached one.
func newPrinter() *pp
newScanState allocates a new ss struct or grab a cached one.
func newScanState(r io.Reader, nlIsSpace bool, nlIsEnd bool) (s *ss, old ssave)
func (s *ss) notEOF()
notSpace is the default scanning function used in Token.
func notSpace(r rune) bool
okVerb verifies that the verb is present in the list, setting s.err appropriately if not.
func (s *ss) okVerb(verb rune, okVerbs string, typ string) bool
pad appends b to f.buf, padded on left (!f.minus) or right (f.minus).
func (f *fmt) pad(b []byte)
padString appends s to f.buf, padded on left (!f.minus) or right (f.minus).
func (f *fmt) padString(s string)
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(format string) (index int, wid int, ok bool)
parsenum converts ASCII to integer. num is 0 (and isnum is false) if no number present.
func parsenum(s string, start int, end int) (num int, isnum bool, newi int)
peek reports whether the next character is in the ok string, without consuming it.
func (s *ss) peek(ok string) bool
func (p *pp) printArg(arg any, verb rune)
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 (p *pp) printValue(value reflect.Value, verb rune, depth int)
quotedString returns the double- or back-quoted string represented by the next input characters.
func (s *ss) quotedString() string
readByte returns the next byte from the input, which may be left over from a previous read if the UTF-8 was ill-formed.
func (r *readRune) readByte() (b byte, err error)
scanBasePrefix reports whether the integer begins with a base prefix and returns the base, digit string, and whether a zero was found. It is called only if the verb is %v.
func (s *ss) scanBasePrefix() (base int, digits string, zeroFound bool)
scanBool returns the value of the boolean represented by the next token.
func (s *ss) scanBool(verb rune) bool
scanComplex converts the next token to a complex128 value. The atof argument is a type-specific reader for the underlying type. If we're reading complex64, atof will parse float32s and convert them to float64's to avoid reproducing this code for each complex type.
func (s *ss) scanComplex(verb rune, n int) complex128
scanInt returns the value of the integer represented by the next token, checking for overflow. Any error is stored in s.err.
func (s *ss) scanInt(verb rune, bitSize int) int64
scanNumber returns the numerical string with specified digits starting here.
func (s *ss) scanNumber(digits string, haveDigits bool) string
scanOne scans a single value, deriving the scanner from the type of the argument.
func (s *ss) scanOne(verb rune, arg any)
scanPercent scans a literal percent character.
func (s *ss) scanPercent()
scanRune returns the next rune value in the input.
func (s *ss) scanRune(bitSize int) int64
scanUint returns the value of the unsigned integer represented by the next token, checking for overflow. Any error is stored in s.err.
func (s *ss) scanUint(verb rune, bitSize int) uint64
token returns the next space-delimited string from the input. It skips white space. For Scanln, it stops at newlines. For Scan, newlines are treated as spaces.
func (s *ss) token(skipSpace bool, f func(rune) bool) []byte
tooLarge reports whether the magnitude of the integer is too large to be used as a formatting width or precision.
func tooLarge(x int) bool
truncate truncates the byte slice b as a string of the specified precision, if present.
func (f *fmt) truncate(b []byte) []byte
truncateString truncates the string s to the specified precision, if present.
func (f *fmt) truncateString(s string) string
func (p *pp) unknownType(v reflect.Value)
func (b *buffer) write(p []byte)
func (b *buffer) writeByte(c byte)
writePadding generates n bytes of padding.
func (f *fmt) writePadding(n int)
func (b *buffer) writeRune(r rune)
func (b *buffer) writeString(s string)
Generated with Arrow