From 7ef000f6ed0b0851bf1ea24f4e0b2e1658a5b728 Mon Sep 17 00:00:00 2001 From: wangjian Date: Fri, 7 Oct 2022 15:41:18 +0800 Subject: [PATCH] init --- encoding/nvarint.go | 8 ++++---- encoding/pvarint.go | 10 +++++----- encoding/varfloat.go | 12 ++++++------ packet.go | 44 ++++++++++++++++++++++---------------------- spec/spec.go | 2 +- spec/tlv.t.go | 6 +++--- spec/tvl.l.go | 5 +++-- 7 files changed, 44 insertions(+), 43 deletions(-) diff --git a/encoding/nvarint.go b/encoding/nvarint.go index 2b3552c..9cc6803 100644 --- a/encoding/nvarint.go +++ b/encoding/nvarint.go @@ -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 diff --git a/encoding/pvarint.go b/encoding/pvarint.go index ca1171e..9ced0c0 100644 --- a/encoding/pvarint.go +++ b/encoding/pvarint.go @@ -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 diff --git a/encoding/varfloat.go b/encoding/varfloat.go index e281980..c4afdb2 100644 --- a/encoding/varfloat.go +++ b/encoding/varfloat.go @@ -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<> ((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 diff --git a/packet.go b/packet.go index 4a3df2f..e322cde 100644 --- a/packet.go +++ b/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 } diff --git a/spec/spec.go b/spec/spec.go index 0258b7d..b2a5244 100644 --- a/spec/spec.go +++ b/spec/spec.go @@ -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) { diff --git a/spec/tlv.t.go b/spec/tlv.t.go index 84671eb..ec12dc4 100644 --- a/spec/tlv.t.go +++ b/spec/tlv.t.go @@ -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 } diff --git a/spec/tvl.l.go b/spec/tvl.l.go index 8ce8281..d1617a2 100644 --- a/spec/tvl.l.go +++ b/spec/tvl.l.go @@ -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