This commit is contained in:
wangjian 2022-10-07 15:41:18 +08:00
parent 0cf71ee320
commit 7ef000f6ed
7 changed files with 44 additions and 43 deletions

View File

@ -75,7 +75,7 @@ func (codec *VarCodec) DecodeNVarUInt64(buffer []byte, value *uint64) error {
// SizeOfNVarInt return the buffer size after encoding value as NVarInt // SizeOfNVarInt return the buffer size after encoding value as NVarInt
func sizeOfNVarInt(value int64, width int) int { 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) var lead = value >> (width - 1)
for size := width/unit - 1; size > 0; size-- { for size := width/unit - 1; size > 0; size-- {
@ -92,7 +92,7 @@ func (codec *VarCodec) encodeNVarInt(buffer []byte, value int64) error {
return errors.New("nothing to encode") 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 { for codec.Size > 0 {
if codec.Ptr >= len(buffer) { if codec.Ptr >= len(buffer) {
return ErrBufferInsufficient return ErrBufferInsufficient
@ -110,7 +110,7 @@ func (codec *VarCodec) decodeNVarInt(buffer []byte, value *int64) error {
return errors.New("nothing to decode") 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.Size > 0 { // initialize sign bit
if codec.Ptr >= len(buffer) { if codec.Ptr >= len(buffer) {
return ErrBufferInsufficient return ErrBufferInsufficient

View File

@ -74,7 +74,7 @@ func (codec *VarCodec) DecodePVarUInt64(buffer []byte, value *uint64) error {
} }
func sizeOfPVarInt(value int64, width int) int { 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) var lead = value >> (width - 1)
for size := width / unit; size > 0; size-- { for size := width / unit; size > 0; size-- {
@ -94,7 +94,7 @@ func (codec *VarCodec) encodePVarInt(buffer []byte, value int64) error {
return ErrBufferInsufficient 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 const more = -1 << 7 // continuation bits
for codec.Size > 1 { for codec.Size > 1 {
codec.Size-- codec.Size--
@ -123,9 +123,9 @@ func (codec *VarCodec) decodePVarInt(buffer []byte, value *int64) error {
return ErrBufferInsufficient 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 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) *value = int64(int8(buffer[codec.Ptr]) << flag >> unit)
} }

View File

@ -43,7 +43,7 @@ func (codec *VarCodec) DecodeVarFloat64(buffer []byte, value *float64) error {
} }
func sizeOfVarFloat(bits uint64, width int) int { 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 const mask = uint64(0xFF) // mask of encoding unit
for s := 0; width > 1; s += unit { for s := 0; width > 1; s += unit {
@ -99,8 +99,8 @@ func (codec *VarCodec) decodeVarFloat(buffer []byte, bits *uint64, width int) er
} }
func (codec *VarCodec) sizeOfGap(width int) (int, int) { func (codec *VarCodec) sizeOfGap(width int) (int, int) {
var ms = mbit.OnesCount(^uint(0)) // machine bit width for an int var ms = mbit.OnesCount(^uint(0)) // machine a bit of width for an int
var size = ms - 8 // bit width of effective size var size = ms - 8 // a bit width of effective size
var mask = -1 ^ (-1 << size) // mask of effective size var mask = -1 ^ (-1 << size) // mask of effective size
var gap = 0 // gap between encoded size and decoded 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 { if width > codec.Size {
gap = 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) codec.Size = sign | (gap << size) | (codec.Size & mask)
} else { } else {
gap = (codec.Size >> size) & 0x7F gap = (codec.Size >> size) & 0x7F

View File

@ -12,7 +12,7 @@ import (
type StreamPacket struct { type StreamPacket struct {
t spec.T t spec.T
l spec.L l spec.L
vbuf []byte vBuf []byte
vr io.Reader vr io.Reader
chunkMode bool chunkMode bool
chunkSize int chunkSize int
@ -38,8 +38,8 @@ func (p *StreamPacket) Bytes() []byte {
buf := new(bytes.Buffer) buf := new(bytes.Buffer)
// the raw bytes of T and L // the raw bytes of T and L
p.writeTL(buf) p.writeTL(buf)
// p.valbuf stores the raw bytes of V // p.valBuf stores the raw bytes of V
buf.Write(p.vbuf) buf.Write(p.vBuf)
return buf.Bytes() 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. // VReader return an io.Reader which can be read as the content of V.
func (p *StreamPacket) VReader() io.Reader { func (p *StreamPacket) VReader() io.Reader {
if !p.chunkMode { if !p.chunkMode {
return bytes.NewReader(p.vbuf) return bytes.NewReader(p.vBuf)
} }
return p.vr return p.vr
} }
@ -60,7 +60,7 @@ func (p *StreamPacket) Reader() io.Reader {
buf := new(bytes.Buffer) buf := new(bytes.Buffer)
buf.Write(p.t.Bytes()) buf.Write(p.t.Bytes())
buf.Write(p.l.Bytes()) buf.Write(p.l.Bytes())
buf.Write(p.vbuf) buf.Write(p.vBuf)
return buf return buf
} }
@ -68,7 +68,7 @@ func (p *StreamPacket) Reader() io.Reader {
// T and L of this packet // T and L of this packet
p.writeTL(buf) p.writeTL(buf)
// V of this packet // V of this packet
buf.Write(p.vbuf) buf.Write(p.vBuf)
return &chunkVReader{ return &chunkVReader{
buf: buf, buf: buf,
@ -98,59 +98,59 @@ func (p *StreamPacket) writeTL(buf *bytes.Buffer) {
// BytesV return V as bytes // BytesV return V as bytes
func (p *StreamPacket) BytesV() []byte { func (p *StreamPacket) BytesV() []byte {
return p.vbuf return p.vBuf
} }
// UTF8StringV return V as utf-8 string // UTF8StringV return V as utf-8 string
func (p *StreamPacket) UTF8StringV() string { func (p *StreamPacket) UTF8StringV() string {
return string(p.vbuf) return string(p.vBuf)
} }
// Int32V return V as int32 // Int32V return V as int32
func (p *StreamPacket) Int32V() (val int32, err error) { func (p *StreamPacket) Int32V() (val int32, err error) {
codec := encoding.VarCodec{Size: len(p.vbuf)} codec := encoding.VarCodec{Size: len(p.vBuf)}
err = codec.DecodeNVarInt32(p.vbuf, &val) err = codec.DecodeNVarInt32(p.vBuf, &val)
return val, err return val, err
} }
// UInt32V return V as uint32 // UInt32V return V as uint32
func (p *StreamPacket) UInt32V() (val uint32, err error) { func (p *StreamPacket) UInt32V() (val uint32, err error) {
codec := encoding.VarCodec{Size: len(p.vbuf)} codec := encoding.VarCodec{Size: len(p.vBuf)}
err = codec.DecodeNVarUInt32(p.vbuf, &val) err = codec.DecodeNVarUInt32(p.vBuf, &val)
return val, err return val, err
} }
// Int64V return V as int64 // Int64V return V as int64
func (p *StreamPacket) Int64V() (val int64, err error) { func (p *StreamPacket) Int64V() (val int64, err error) {
codec := encoding.VarCodec{Size: len(p.vbuf)} codec := encoding.VarCodec{Size: len(p.vBuf)}
err = codec.DecodeNVarInt64(p.vbuf, &val) err = codec.DecodeNVarInt64(p.vBuf, &val)
return val, err return val, err
} }
// UInt64V return V as uint64 // UInt64V return V as uint64
func (p *StreamPacket) UInt64V() (val uint64, err error) { func (p *StreamPacket) UInt64V() (val uint64, err error) {
codec := encoding.VarCodec{Size: len(p.vbuf)} codec := encoding.VarCodec{Size: len(p.vBuf)}
err = codec.DecodeNVarUInt64(p.vbuf, &val) err = codec.DecodeNVarUInt64(p.vBuf, &val)
return val, err return val, err
} }
// Float32V return V as float32 // Float32V return V as float32
func (p *StreamPacket) Float32V() (val float32, err error) { func (p *StreamPacket) Float32V() (val float32, err error) {
codec := encoding.VarCodec{Size: len(p.vbuf)} codec := encoding.VarCodec{Size: len(p.vBuf)}
err = codec.DecodeVarFloat32(p.vbuf, &val) err = codec.DecodeVarFloat32(p.vBuf, &val)
return val, err return val, err
} }
// Float64V return V as float64 // Float64V return V as float64
func (p *StreamPacket) Float64V() (val float64, err error) { func (p *StreamPacket) Float64V() (val float64, err error) {
codec := encoding.VarCodec{Size: len(p.vbuf)} codec := encoding.VarCodec{Size: len(p.vBuf)}
err = codec.DecodeVarFloat64(p.vbuf, &val) err = codec.DecodeVarFloat64(p.vBuf, &val)
return val, err return val, err
} }
// BoolV return V as bool // BoolV return V as bool
func (p *StreamPacket) BoolV() (val bool, err error) { func (p *StreamPacket) BoolV() (val bool, err error) {
codec := encoding.VarCodec{Size: len(p.vbuf)} codec := encoding.VarCodec{Size: len(p.vBuf)}
err = codec.DecodePVarBool(p.vbuf, &val) err = codec.DecodePVarBool(p.vBuf, &val)
return val, err return val, err
} }

View File

@ -13,7 +13,7 @@ const (
) )
var ( 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) { func readByte(reader io.Reader) (byte, error) {

View File

@ -12,13 +12,13 @@ type T byte
// will set MSB to T. // will set MSB to T.
func NewT(seqID int) (T, error) { func NewT(seqID int) (T, error) {
if seqID < 0 || seqID > maxSeqID { if seqID < 0 || seqID > maxSeqID {
return 0, errInvalidSeqID return 0, errInvalidSeqId
} }
return T(seqID), nil return T(seqID), nil
} }
// Sid returns the sequenceID of this packet. // Sid returns the sequenceId of this packet.
func (t T) Sid() int { func (t T) Sid() int {
return int(t & wipeFlagBits) return int(t & wipeFlagBits)
} }
@ -29,7 +29,7 @@ func (t T) Bytes() []byte {
} }
// IsNodeMode will return true if this packet contains other packets. // IsNodeMode will return true if this packet contains other packets.
// Otherwise return flase. // Otherwise, return false.
func (t T) IsNodeMode() bool { func (t T) IsNodeMode() bool {
return t&flagBitNode == flagBitNode return t&flagBitNode == flagBitNode
} }

View File

@ -4,9 +4,10 @@ import (
"bytes" "bytes"
"errors" "errors"
"io" "io"
//"git.hpds.cc/Component/mq_coder/encoding"
"mq_coder/encoding" "mq_coder/encoding"
"git.hpds.cc/Component/mq_coder/encoding"
//"mq_coder/encoding"
) )
// L is the Length in a TLV structure // L is the Length in a TLV structure