Skip to content

Commit

Permalink
restore standard library features
Browse files Browse the repository at this point in the history
  • Loading branch information
joel-ling committed Dec 18, 2021
1 parent b10e210 commit 1ae6f55
Show file tree
Hide file tree
Showing 2 changed files with 64 additions and 0 deletions.
61 changes: 61 additions & 0 deletions binary.go
Original file line number Diff line number Diff line change
@@ -1,7 +1,9 @@
package binary

import (
stdlib "encoding/binary"
"fmt"
"io"

"github.com/encodingx/binary/internal/validation"
)
Expand Down Expand Up @@ -85,3 +87,62 @@ func Unmarshal(bytes []byte, iface interface{}) (e error) {

return
}

// Standard library features

const (
MaxVarintLen16 = stdlib.MaxVarintLen16
MaxVarintLen32 = stdlib.MaxVarintLen32
MaxVarintLen64 = stdlib.MaxVarintLen64
)

var (
BigEndian = stdlib.BigEndian
LittleEndian = stdlib.LittleEndian
)

type ByteOrder interface {
Uint16([]byte) uint16
Uint32([]byte) uint32
Uint64([]byte) uint64
PutUint16([]byte, uint16)
PutUint32([]byte, uint32)
PutUint64([]byte, uint64)
String() string
}

func PutUvarint(buf []byte, x uint64) int {
return stdlib.PutUvarint(buf, x)
}

func PutVarint(buf []byte, x int64) int {
return stdlib.PutVarint(buf, x)
}

func Read(r io.Reader, order ByteOrder, data interface{}) error {
return stdlib.Read(r, order, data)
}

func ReadUvarint(r io.ByteReader) (uint64, error) {
return stdlib.ReadUvarint(r)
}

func ReadVarint(r io.ByteReader) (int64, error) {
return stdlib.ReadVarint(r)
}

func Size(v interface{}) int {
return stdlib.Size(v)
}

func Uvarint(buf []byte) (uint64, int) {
return stdlib.Uvarint(buf)
}

func Varint(buf []byte) (int64, int) {
return stdlib.Varint(buf)
}

func Write(w io.Writer, order ByteOrder, data interface{}) error {
return stdlib.Write(w, order, data)
}
3 changes: 3 additions & 0 deletions metadata-format.go
Original file line number Diff line number Diff line change
Expand Up @@ -53,6 +53,9 @@ func newFormatMetadataFromTypeReflection(reflection reflect.Type) (
}

func (m formatMetadata) marshal(reflection reflect.Value) (bytes []byte) {
// Merge byte slices marshalled from words,
// in the order they appear in the format.

var (
copyIndex int
i int
Expand Down

0 comments on commit 1ae6f55

Please sign in to comment.