Source File
codec.go
Belonging Package
golang.org/x/pkgsite/internal/godoc/codec
package codec
import (
)
return
, := .(codecError)
if ! {
panic()
* = .err
}
type codecError struct {
err error
}
func ( error) {
panic(codecError{})
}
func ( string, ...interface{}) {
fail(fmt.Errorf(, ...))
}
func ( *Decoder) () ( interface{}, error) {
defer handlePanic(&)
if .typeInfos == nil {
.decodeInitial()
}
return .DecodeAny(), nil
}
func ( byte) {
failf("bad code: %d", )
}
func ( *Decoder) ( int) []byte {
.i +=
return .buf[.i- : .i]
}
func ( *Encoder) ( string) {
.buf = append(.buf, ...)
}
func ( *Decoder) ( int) string {
return string(.readBytes())
}
func ( *Encoder) ( uint32) {
var [4]byte
binary.LittleEndian.PutUint32([:], )
.writeBytes([:])
}
func ( *Decoder) () uint32 {
return binary.LittleEndian.Uint32(.readBytes(4))
}
func ( *Encoder) ( uint64) {
var [8]byte
binary.LittleEndian.PutUint64([:], )
.writeBytes([:])
}
func ( *Decoder) () uint64 {
return binary.LittleEndian.Uint64(.readBytes(8))
}
const (
)
.writeByte(nBytesCode)
.writeByte(4)
.writeUint32(uint32())
.writeByte(nBytesCode)
.writeByte(8)
.writeUint64()
}
}
func ( *Decoder) () uint64 {
:= .readByte()
switch {
case < endCode:
return uint64()
case == nBytesCode:
switch := .readByte(); {
case 4:
return uint64(.readUint32())
case 8:
return .readUint64()
default:
failf("DecodeUint: bad length %d", )
}
default:
badcode()
}
return 0
}
var uint64
if < 0 {
= (^uint64() << 1) | 1 // complement i, bit 0 is 1
} else {
= (uint64() << 1) // do not complement i, bit 0 is 0
}
.EncodeUint()
}
func ( *Decoder) () int64 {
:= .DecodeUint()
if &1 == 1 {
return int64(^( >> 1))
}
return int64( >> 1)
}
func ( *Encoder) ( int) {
.writeByte(nBytesCode)
.EncodeUint(uint64())
}
func ( *Decoder) () int {
if := .readByte(); != nBytesCode {
badcode()
}
return int(.DecodeUint())
}
func ( *Encoder) ( []byte) {
.encodeLen(len())
.writeBytes()
}
func ( *Encoder) ( string) {
.encodeLen(len())
.writeString()
}
func ( *Decoder) () string {
return .readString(.decodeLen())
}
func ( *Encoder) ( float64) {
.EncodeUint(math.Float64bits())
}
func ( *Decoder) () float64 {
return math.Float64frombits(.DecodeUint())
}
func ( *Encoder) () {
.writeByte(nilCode)
}
func ( *Encoder) ( int) {
.writeByte(nValuesCode)
.EncodeUint(uint64())
}
func ( *Decoder) () int {
switch := .readByte(); {
case nilCode:
return -1
case nValuesCode:
return int(.DecodeUint())
default:
badcode()
return 0
}
}
.writeByte(refCode)
.EncodeUint()
return false // Caller should not encode the struct.
return
}
switch {
.readBytes(int(.DecodeUint()))
:= int(.DecodeUint())
for := 0; < ; ++ {
.()
}
.DecodeUint()
:= reflect.TypeOf()
:= typeInfosByType[]
if == nil {
failf("unregistered type %s", )
.StartList(2)
.EncodeUint(uint64())
.encode(, )
}
:= .StartList()
.typeInfos = make([]*typeInfo, )
for := 0; < ; ++ {
:= .DecodeString()
:= typeInfosByName[]
if == nil {
failf("unregistered type: %s", )
}
.typeInfos[] =
}
}
type typeInfo struct {
name string // e.g. "go/ast.File"
encode encodeFunc
decode decodeFunc
}
type (
encodeFunc func(*Encoder, interface{})
decodeFunc func(*Decoder) interface{}
)
var (
typeInfosByName = map[string]*typeInfo{}
typeInfosByType = map[reflect.Type]*typeInfo{}
)
func ( interface{}, encodeFunc, decodeFunc) {
:= reflect.TypeOf()
:= typeName()
if , := typeInfosByName[]; {
panic(fmt.Sprintf("codec.Register: duplicate type %s (typeName=%q)", , ))
}
:= &typeInfo{
name: ,
encode: ,
decode: ,
}
typeInfosByName[.name] =
typeInfosByType[] =
}
func ( reflect.Type) string {
if .PkgPath() == "" {
return .String()
}
return .PkgPath() + "." + .Name()
}
var builtinTypes []reflect.Type
func () {
Register(int64(0),
func( *Encoder, interface{}) { .EncodeInt(.(int64)) },
func( *Decoder) interface{} { return .DecodeInt() })
Register(uint64(0),
func( *Encoder, interface{}) { .EncodeUint(.(uint64)) },
func( *Decoder) interface{} { return .DecodeUint() })
Register(int(0),
func( *Encoder, interface{}) { .EncodeInt(int64(.(int))) },
func( *Decoder) interface{} { return int(.DecodeInt()) })
Register(float64(0),
func( *Encoder, interface{}) { .EncodeFloat(.(float64)) },
func( *Decoder) interface{} { return .DecodeFloat() })
Register(false,
func( *Encoder, interface{}) { .EncodeBool(.(bool)) },
func( *Decoder) interface{} { return .DecodeBool() })
Register("",
func( *Encoder, interface{}) { .EncodeString(.(string)) },
func( *Decoder) interface{} { return .DecodeString() })
Register([]byte(nil),
func( *Encoder, interface{}) { .EncodeBytes(.([]byte)) },
func( *Decoder) interface{} { return .DecodeBytes() })
for := range typeInfosByType {
builtinTypes = append(builtinTypes, )
}
![]() |
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. |