Skip to content

Commit 1ae6f55

Browse files
committed
restore standard library features
1 parent b10e210 commit 1ae6f55

File tree

2 files changed

+64
-0
lines changed

2 files changed

+64
-0
lines changed

binary.go

Lines changed: 61 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,9 @@
11
package binary
22

33
import (
4+
stdlib "encoding/binary"
45
"fmt"
6+
"io"
57

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

8688
return
8789
}
90+
91+
// Standard library features
92+
93+
const (
94+
MaxVarintLen16 = stdlib.MaxVarintLen16
95+
MaxVarintLen32 = stdlib.MaxVarintLen32
96+
MaxVarintLen64 = stdlib.MaxVarintLen64
97+
)
98+
99+
var (
100+
BigEndian = stdlib.BigEndian
101+
LittleEndian = stdlib.LittleEndian
102+
)
103+
104+
type ByteOrder interface {
105+
Uint16([]byte) uint16
106+
Uint32([]byte) uint32
107+
Uint64([]byte) uint64
108+
PutUint16([]byte, uint16)
109+
PutUint32([]byte, uint32)
110+
PutUint64([]byte, uint64)
111+
String() string
112+
}
113+
114+
func PutUvarint(buf []byte, x uint64) int {
115+
return stdlib.PutUvarint(buf, x)
116+
}
117+
118+
func PutVarint(buf []byte, x int64) int {
119+
return stdlib.PutVarint(buf, x)
120+
}
121+
122+
func Read(r io.Reader, order ByteOrder, data interface{}) error {
123+
return stdlib.Read(r, order, data)
124+
}
125+
126+
func ReadUvarint(r io.ByteReader) (uint64, error) {
127+
return stdlib.ReadUvarint(r)
128+
}
129+
130+
func ReadVarint(r io.ByteReader) (int64, error) {
131+
return stdlib.ReadVarint(r)
132+
}
133+
134+
func Size(v interface{}) int {
135+
return stdlib.Size(v)
136+
}
137+
138+
func Uvarint(buf []byte) (uint64, int) {
139+
return stdlib.Uvarint(buf)
140+
}
141+
142+
func Varint(buf []byte) (int64, int) {
143+
return stdlib.Varint(buf)
144+
}
145+
146+
func Write(w io.Writer, order ByteOrder, data interface{}) error {
147+
return stdlib.Write(w, order, data)
148+
}

metadata-format.go

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -53,6 +53,9 @@ func newFormatMetadataFromTypeReflection(reflection reflect.Type) (
5353
}
5454

5555
func (m formatMetadata) marshal(reflection reflect.Value) (bytes []byte) {
56+
// Merge byte slices marshalled from words,
57+
// in the order they appear in the format.
58+
5659
var (
5760
copyIndex int
5861
i int

0 commit comments

Comments
 (0)