init
This commit is contained in:
parent
0cf71ee320
commit
7ef000f6ed
|
@ -75,10 +75,10 @@ func (codec *VarCodec) DecodeNVarUInt64(buffer []byte, value *uint64) error {
|
|||
|
||||
// SizeOfNVarInt return the buffer size after encoding value as NVarInt
|
||||
func sizeOfNVarInt(value int64, width int) int {
|
||||
const unit = 8 // bit width of encoding unit
|
||||
const unit = 8 // a bit width of encoding unit
|
||||
|
||||
var lead = value >> (width - 1)
|
||||
for size := width / unit - 1; size > 0; size-- {
|
||||
for size := width/unit - 1; size > 0; size-- {
|
||||
var lookAhead = value >> (size*unit - 1)
|
||||
if lookAhead != lead {
|
||||
return size + 1
|
||||
|
@ -92,7 +92,7 @@ func (codec *VarCodec) encodeNVarInt(buffer []byte, value int64) error {
|
|||
return errors.New("nothing to encode")
|
||||
}
|
||||
|
||||
const unit = 8 // bit width of encoding unit
|
||||
const unit = 8 // a bit width of encoding unit
|
||||
for codec.Size > 0 {
|
||||
if codec.Ptr >= len(buffer) {
|
||||
return ErrBufferInsufficient
|
||||
|
@ -110,7 +110,7 @@ func (codec *VarCodec) decodeNVarInt(buffer []byte, value *int64) error {
|
|||
return errors.New("nothing to decode")
|
||||
}
|
||||
|
||||
const unit = 8 // bit width of encoding unit
|
||||
const unit = 8 // a bit width of encoding unit
|
||||
if codec.Size > 0 { // initialize sign bit
|
||||
if codec.Ptr >= len(buffer) {
|
||||
return ErrBufferInsufficient
|
||||
|
|
|
@ -74,7 +74,7 @@ func (codec *VarCodec) DecodePVarUInt64(buffer []byte, value *uint64) error {
|
|||
}
|
||||
|
||||
func sizeOfPVarInt(value int64, width int) int {
|
||||
const unit = 7 // bit width of encoding unit
|
||||
const unit = 7 // a bit width of encoding unit
|
||||
|
||||
var lead = value >> (width - 1)
|
||||
for size := width / unit; size > 0; size-- {
|
||||
|
@ -94,7 +94,7 @@ func (codec *VarCodec) encodePVarInt(buffer []byte, value int64) error {
|
|||
return ErrBufferInsufficient
|
||||
}
|
||||
|
||||
const unit = 7 // bit width of encoding unit
|
||||
const unit = 7 // a bit width of encoding unit
|
||||
const more = -1 << 7 // continuation bits
|
||||
for codec.Size > 1 {
|
||||
codec.Size--
|
||||
|
@ -123,9 +123,9 @@ func (codec *VarCodec) decodePVarInt(buffer []byte, value *int64) error {
|
|||
return ErrBufferInsufficient
|
||||
}
|
||||
|
||||
const unit = 7 // bit width of encoding unit
|
||||
const unit = 7 // a bit width of encoding unit
|
||||
if codec.Size == 0 { // initialize sign bit
|
||||
const flag = 8 - unit // bit width for non-encoding bits
|
||||
const flag = 8 - unit // a bit of width for non-encoding bits
|
||||
*value = int64(int8(buffer[codec.Ptr]) << flag >> unit)
|
||||
}
|
||||
|
||||
|
@ -135,7 +135,7 @@ func (codec *VarCodec) decodePVarInt(buffer []byte, value *int64) error {
|
|||
codec.Ptr++
|
||||
|
||||
codec.Size++
|
||||
*value = (*value << unit) | int64(mask & part)
|
||||
*value = (*value << unit) | int64(mask&part)
|
||||
|
||||
if part >= 0 { // it's the last byte
|
||||
return nil
|
||||
|
|
|
@ -43,11 +43,11 @@ func (codec *VarCodec) DecodeVarFloat64(buffer []byte, value *float64) error {
|
|||
}
|
||||
|
||||
func sizeOfVarFloat(bits uint64, width int) int {
|
||||
const unit = 8 // bit width of encoding unit
|
||||
const unit = 8 // a bit width of encoding unit
|
||||
const mask = uint64(0xFF) // mask of encoding unit
|
||||
|
||||
for s := 0; width > 1; s += unit {
|
||||
if bits & (mask << s) != 0 {
|
||||
if bits&(mask<<s) != 0 {
|
||||
return width
|
||||
}
|
||||
width--
|
||||
|
@ -68,7 +68,7 @@ func (codec *VarCodec) encodeVarFloat(buffer []byte, bits uint64, width int) err
|
|||
return ErrBufferInsufficient
|
||||
}
|
||||
codec.Size--
|
||||
buffer[codec.Ptr] = byte(bits >> ((codec.Size & mask + gap) * unit))
|
||||
buffer[codec.Ptr] = byte(bits >> ((codec.Size&mask + gap) * unit))
|
||||
codec.Ptr++
|
||||
}
|
||||
|
||||
|
@ -99,8 +99,8 @@ func (codec *VarCodec) decodeVarFloat(buffer []byte, bits *uint64, width int) er
|
|||
}
|
||||
|
||||
func (codec *VarCodec) sizeOfGap(width int) (int, int) {
|
||||
var ms = mbit.OnesCount(^uint(0)) // machine bit width for an int
|
||||
var size = ms - 8 // bit width of effective size
|
||||
var ms = mbit.OnesCount(^uint(0)) // machine a bit of width for an int
|
||||
var size = ms - 8 // a bit width of effective size
|
||||
var mask = -1 ^ (-1 << size) // mask of effective size
|
||||
|
||||
var gap = 0 // gap between encoded size and decoded size
|
||||
|
@ -108,7 +108,7 @@ func (codec *VarCodec) sizeOfGap(width int) (int, int) {
|
|||
if width > codec.Size {
|
||||
gap = width - codec.Size
|
||||
}
|
||||
var sign = -1 << (ms - 1) // single sign bit for an int
|
||||
var sign = -1 << (ms - 1) // single sign a bit for an int
|
||||
codec.Size = sign | (gap << size) | (codec.Size & mask)
|
||||
} else {
|
||||
gap = (codec.Size >> size) & 0x7F
|
||||
|
|
44
packet.go
44
packet.go
|
@ -12,7 +12,7 @@ import (
|
|||
type StreamPacket struct {
|
||||
t spec.T
|
||||
l spec.L
|
||||
vbuf []byte
|
||||
vBuf []byte
|
||||
vr io.Reader
|
||||
chunkMode bool
|
||||
chunkSize int
|
||||
|
@ -38,8 +38,8 @@ func (p *StreamPacket) Bytes() []byte {
|
|||
buf := new(bytes.Buffer)
|
||||
// the raw bytes of T and L
|
||||
p.writeTL(buf)
|
||||
// p.valbuf stores the raw bytes of V
|
||||
buf.Write(p.vbuf)
|
||||
// p.valBuf stores the raw bytes of V
|
||||
buf.Write(p.vBuf)
|
||||
|
||||
return buf.Bytes()
|
||||
}
|
||||
|
@ -47,7 +47,7 @@ func (p *StreamPacket) Bytes() []byte {
|
|||
// VReader return an io.Reader which can be read as the content of V.
|
||||
func (p *StreamPacket) VReader() io.Reader {
|
||||
if !p.chunkMode {
|
||||
return bytes.NewReader(p.vbuf)
|
||||
return bytes.NewReader(p.vBuf)
|
||||
}
|
||||
return p.vr
|
||||
}
|
||||
|
@ -60,7 +60,7 @@ func (p *StreamPacket) Reader() io.Reader {
|
|||
buf := new(bytes.Buffer)
|
||||
buf.Write(p.t.Bytes())
|
||||
buf.Write(p.l.Bytes())
|
||||
buf.Write(p.vbuf)
|
||||
buf.Write(p.vBuf)
|
||||
return buf
|
||||
}
|
||||
|
||||
|
@ -68,7 +68,7 @@ func (p *StreamPacket) Reader() io.Reader {
|
|||
// T and L of this packet
|
||||
p.writeTL(buf)
|
||||
// V of this packet
|
||||
buf.Write(p.vbuf)
|
||||
buf.Write(p.vBuf)
|
||||
|
||||
return &chunkVReader{
|
||||
buf: buf,
|
||||
|
@ -98,59 +98,59 @@ func (p *StreamPacket) writeTL(buf *bytes.Buffer) {
|
|||
|
||||
// BytesV return V as bytes
|
||||
func (p *StreamPacket) BytesV() []byte {
|
||||
return p.vbuf
|
||||
return p.vBuf
|
||||
}
|
||||
|
||||
// UTF8StringV return V as utf-8 string
|
||||
func (p *StreamPacket) UTF8StringV() string {
|
||||
return string(p.vbuf)
|
||||
return string(p.vBuf)
|
||||
}
|
||||
|
||||
// Int32V return V as int32
|
||||
func (p *StreamPacket) Int32V() (val int32, err error) {
|
||||
codec := encoding.VarCodec{Size: len(p.vbuf)}
|
||||
err = codec.DecodeNVarInt32(p.vbuf, &val)
|
||||
codec := encoding.VarCodec{Size: len(p.vBuf)}
|
||||
err = codec.DecodeNVarInt32(p.vBuf, &val)
|
||||
return val, err
|
||||
}
|
||||
|
||||
// UInt32V return V as uint32
|
||||
func (p *StreamPacket) UInt32V() (val uint32, err error) {
|
||||
codec := encoding.VarCodec{Size: len(p.vbuf)}
|
||||
err = codec.DecodeNVarUInt32(p.vbuf, &val)
|
||||
codec := encoding.VarCodec{Size: len(p.vBuf)}
|
||||
err = codec.DecodeNVarUInt32(p.vBuf, &val)
|
||||
return val, err
|
||||
}
|
||||
|
||||
// Int64V return V as int64
|
||||
func (p *StreamPacket) Int64V() (val int64, err error) {
|
||||
codec := encoding.VarCodec{Size: len(p.vbuf)}
|
||||
err = codec.DecodeNVarInt64(p.vbuf, &val)
|
||||
codec := encoding.VarCodec{Size: len(p.vBuf)}
|
||||
err = codec.DecodeNVarInt64(p.vBuf, &val)
|
||||
return val, err
|
||||
}
|
||||
|
||||
// UInt64V return V as uint64
|
||||
func (p *StreamPacket) UInt64V() (val uint64, err error) {
|
||||
codec := encoding.VarCodec{Size: len(p.vbuf)}
|
||||
err = codec.DecodeNVarUInt64(p.vbuf, &val)
|
||||
codec := encoding.VarCodec{Size: len(p.vBuf)}
|
||||
err = codec.DecodeNVarUInt64(p.vBuf, &val)
|
||||
return val, err
|
||||
}
|
||||
|
||||
// Float32V return V as float32
|
||||
func (p *StreamPacket) Float32V() (val float32, err error) {
|
||||
codec := encoding.VarCodec{Size: len(p.vbuf)}
|
||||
err = codec.DecodeVarFloat32(p.vbuf, &val)
|
||||
codec := encoding.VarCodec{Size: len(p.vBuf)}
|
||||
err = codec.DecodeVarFloat32(p.vBuf, &val)
|
||||
return val, err
|
||||
}
|
||||
|
||||
// Float64V return V as float64
|
||||
func (p *StreamPacket) Float64V() (val float64, err error) {
|
||||
codec := encoding.VarCodec{Size: len(p.vbuf)}
|
||||
err = codec.DecodeVarFloat64(p.vbuf, &val)
|
||||
codec := encoding.VarCodec{Size: len(p.vBuf)}
|
||||
err = codec.DecodeVarFloat64(p.vBuf, &val)
|
||||
return val, err
|
||||
}
|
||||
|
||||
// BoolV return V as bool
|
||||
func (p *StreamPacket) BoolV() (val bool, err error) {
|
||||
codec := encoding.VarCodec{Size: len(p.vbuf)}
|
||||
err = codec.DecodePVarBool(p.vbuf, &val)
|
||||
codec := encoding.VarCodec{Size: len(p.vBuf)}
|
||||
err = codec.DecodePVarBool(p.vBuf, &val)
|
||||
return val, err
|
||||
}
|
||||
|
|
|
@ -13,7 +13,7 @@ const (
|
|||
)
|
||||
|
||||
var (
|
||||
errInvalidSeqID = errors.New("y3.Builder: SeqID should >= 0 and =< 0x3F")
|
||||
errInvalidSeqId = errors.New("coder.Builder: SeqId should >= 0 and =< 0x3F")
|
||||
)
|
||||
|
||||
func readByte(reader io.Reader) (byte, error) {
|
||||
|
|
|
@ -12,13 +12,13 @@ type T byte
|
|||
// will set MSB to T.
|
||||
func NewT(seqID int) (T, error) {
|
||||
if seqID < 0 || seqID > maxSeqID {
|
||||
return 0, errInvalidSeqID
|
||||
return 0, errInvalidSeqId
|
||||
}
|
||||
|
||||
return T(seqID), nil
|
||||
}
|
||||
|
||||
// Sid returns the sequenceID of this packet.
|
||||
// Sid returns the sequenceId of this packet.
|
||||
func (t T) Sid() int {
|
||||
return int(t & wipeFlagBits)
|
||||
}
|
||||
|
@ -29,7 +29,7 @@ func (t T) Bytes() []byte {
|
|||
}
|
||||
|
||||
// IsNodeMode will return true if this packet contains other packets.
|
||||
// Otherwise return flase.
|
||||
// Otherwise, return false.
|
||||
func (t T) IsNodeMode() bool {
|
||||
return t&flagBitNode == flagBitNode
|
||||
}
|
||||
|
|
|
@ -4,9 +4,10 @@ import (
|
|||
"bytes"
|
||||
"errors"
|
||||
"io"
|
||||
|
||||
//"git.hpds.cc/Component/mq_coder/encoding"
|
||||
"mq_coder/encoding"
|
||||
|
||||
"git.hpds.cc/Component/mq_coder/encoding"
|
||||
//"mq_coder/encoding"
|
||||
)
|
||||
|
||||
// L is the Length in a TLV structure
|
||||
|
|
Loading…
Reference in New Issue