Source File
print.go
Belonging Package
fmt
package fmt
import (
)
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>"
)
type GoStringer interface {
GoString() string
}
type buffer []byte
func ( *buffer) ( []byte) {
* = append(*, ...)
}
func ( *buffer) ( string) {
* = append(*, ...)
}
func ( *buffer) ( byte) {
* = append(*, )
}
func ( *buffer) ( rune) {
if < utf8.RuneSelf {
* = append(*, byte())
return
}
:= *
:= len()
for +utf8.UTFMax > cap() {
= append(, 0)
}
:= utf8.EncodeRune([:+utf8.UTFMax], )
* = [:+]
}
arg interface{}
if cap(.buf) > 64<<10 {
return
}
.buf = .buf[:0]
.arg = nil
.value = reflect.Value{}
.wrappedErr = nil
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
}
func ( 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()
}
}
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()
}
}
.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.Ptr, reflect.Slice, reflect.UnsafePointer:
= .Pointer()
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) ( interface{}, rune, string) {
if := reflect.ValueOf(); .Kind() == reflect.Ptr && .IsNil() {
.buf.writeString(nilAngleString)
return
panic()
}
.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
}
= 'v'
}
if .fmt.sharpV {
if , := .arg.(GoStringer); {
= true
switch {
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) ( interface{}, rune) {
.arg =
.value = reflect.Value{}
if == nil {
switch {
case 'T', 'v':
.fmt.padString(nilAngleString)
default:
.badVerb()
}
return
}
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")
if .IsValid() && .CanInterface() {
.arg = .Interface()
if .handleMethods() {
return
}
}
.printValue(, , 0)
.printValue(reflect.ValueOf(), , 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 .Key {
if > 0 {
if .fmt.sharpV {
.buf.writeString(commaSpaceString)
} else {
.buf.writeByte(' ')
}
}
.(, , +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 {
= 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(']')
}
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
}
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, []interface{}) {
:= 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([:])
}
break
}
++
if 'a' <= && <= 'z' && < len() {
break
}
}
if < && [] == '*' {
++
.fmt.wid, .fmt.widPresent, = intFromArg(, )
if !.fmt.widPresent {
.buf.writeString(badWidthString)
}
if +1 < && [] == '.' {
++
if { // "%[3].2d"
.goodArgNum = false
}
, , = .argNumber(, , , len())
if < && [] == '*' {
++
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()
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) ( []interface{}) {
:= false
for , := range {
![]() |
The pages are generated with Golds v0.3.2-preview. (GOOS=darwin GOARCH=amd64) Golds is a Go 101 project developed by Tapir Liu. PR and bug reports are welcome and can be submitted to the issue list. Please follow @Go100and1 (reachable from the left QR code) to get the latest news of Golds. |