You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@plc4x.apache.org by sr...@apache.org on 2021/03/11 12:36:21 UTC

[plc4x] branch develop updated: remove debug statements from golang readbuffer

This is an automated email from the ASF dual-hosted git repository.

sruehl pushed a commit to branch develop
in repository https://gitbox.apache.org/repos/asf/plc4x.git


The following commit(s) were added to refs/heads/develop by this push:
     new 548104b  remove debug statements from golang readbuffer
548104b is described below

commit 548104b350127dc644ff686b10e3d0af446fff3b
Author: Sebastian Rühl <sr...@apache.org>
AuthorDate: Thu Mar 11 13:35:57 2021 +0100

    remove debug statements from golang readbuffer
---
 plc4go/internal/plc4go/spi/utils/ReadBuffer.go | 314 ++++++++++++-------------
 1 file changed, 153 insertions(+), 161 deletions(-)

diff --git a/plc4go/internal/plc4go/spi/utils/ReadBuffer.go b/plc4go/internal/plc4go/spi/utils/ReadBuffer.go
index 894d810..c4e619d 100644
--- a/plc4go/internal/plc4go/spi/utils/ReadBuffer.go
+++ b/plc4go/internal/plc4go/spi/utils/ReadBuffer.go
@@ -19,239 +19,231 @@
 package utils
 
 import (
-    "bytes"
-    "encoding/binary"
-    "errors"
-    "fmt"
-    "github.com/icza/bitio"
-    "math"
-    "math/big"
-    "math/bits"
+	"bytes"
+	"encoding/binary"
+	"errors"
+	"github.com/icza/bitio"
+	"math"
+	"math/big"
 )
 
 type ReadBuffer struct {
-    data   []uint8
-    reader *bitio.Reader
-    pos    uint64
+	data   []uint8
+	reader *bitio.Reader
+	pos    uint64
 }
 
 func NewReadBuffer(data []uint8) *ReadBuffer {
-    buffer := bytes.NewBuffer(data)
-    reader := bitio.NewReader(buffer)
-    return &ReadBuffer{
-        data:   data,
-        reader: reader,
-        pos:    uint64(0),
-    }
+	buffer := bytes.NewBuffer(data)
+	reader := bitio.NewReader(buffer)
+	return &ReadBuffer{
+		data:   data,
+		reader: reader,
+		pos:    uint64(0),
+	}
 }
 
 func (rb *ReadBuffer) Reset() {
-    rb.pos = uint64(0)
-    rb.reader = bitio.NewReader(bytes.NewBuffer(rb.data))
+	rb.pos = uint64(0)
+	rb.reader = bitio.NewReader(bytes.NewBuffer(rb.data))
 }
 
 func (rb *ReadBuffer) GetPos() uint16 {
-    return uint16(rb.pos / 8)
+	return uint16(rb.pos / 8)
 }
 
 func (rb *ReadBuffer) GetBytes() []uint8 {
-    return rb.data
+	return rb.data
 }
 
 func (rb *ReadBuffer) GetTotalBytes() uint64 {
-    return uint64(len(rb.data))
+	return uint64(len(rb.data))
 }
 
 func (rb *ReadBuffer) HasMore(bitLength uint8) bool {
-    return (rb.pos + uint64(bitLength)) <= (uint64(len(rb.data)) * 8)
+	return (rb.pos + uint64(bitLength)) <= (uint64(len(rb.data)) * 8)
 }
 
 func (rb *ReadBuffer) PeekByte(offset uint8) uint8 {
-    panic("Not implemented")
+	panic("Not implemented")
 }
 
 func (rb *ReadBuffer) ReadBit() (bool, error) {
-    rb.pos += 1
-    return rb.reader.ReadBool()
+	rb.pos += 1
+	return rb.reader.ReadBool()
 }
 
 func (rb *ReadBuffer) ReadUint8(bitLength uint8) (uint8, error) {
-    rb.pos += uint64(bitLength)
-    res := uint8(rb.reader.TryReadBits(bitLength))
-    if rb.reader.TryError != nil {
-        return 0, rb.reader.TryError
-    }
-    return res, nil
+	rb.pos += uint64(bitLength)
+	res := uint8(rb.reader.TryReadBits(bitLength))
+	if rb.reader.TryError != nil {
+		return 0, rb.reader.TryError
+	}
+	return res, nil
 }
 
 func (rb *ReadBuffer) ReadUint16(bitLength uint8) (uint16, error) {
-    rb.pos += uint64(bitLength)
-    res := uint16(rb.reader.TryReadBits(bitLength))
-    if rb.reader.TryError != nil {
-        return 0, rb.reader.TryError
-    }
-    return res, nil
+	rb.pos += uint64(bitLength)
+	res := uint16(rb.reader.TryReadBits(bitLength))
+	if rb.reader.TryError != nil {
+		return 0, rb.reader.TryError
+	}
+	return res, nil
 }
 
 func (rb *ReadBuffer) ReadUint32(bitLength uint8) (uint32, error) {
-    rb.pos += uint64(bitLength)
-    res := uint32(rb.reader.TryReadBits(bitLength))
-    if rb.reader.TryError != nil {
-        return 0, rb.reader.TryError
-    }
-    return res, nil
+	rb.pos += uint64(bitLength)
+	res := uint32(rb.reader.TryReadBits(bitLength))
+	if rb.reader.TryError != nil {
+		return 0, rb.reader.TryError
+	}
+	return res, nil
 }
 
 func (rb *ReadBuffer) ReadUint64(bitLength uint8) (uint64, error) {
-    rb.pos += uint64(bitLength)
-    res := uint64(rb.reader.TryReadBits(bitLength))
-    if rb.reader.TryError != nil {
-        return 0, rb.reader.TryError
-    }
-    return res, nil
+	rb.pos += uint64(bitLength)
+	res := uint64(rb.reader.TryReadBits(bitLength))
+	if rb.reader.TryError != nil {
+		return 0, rb.reader.TryError
+	}
+	return res, nil
 }
 
 func (rb *ReadBuffer) ReadInt8(bitLength uint8) (int8, error) {
-    rb.pos += uint64(bitLength)
-    res := int8(rb.reader.TryReadBits(bitLength))
-    if rb.reader.TryError != nil {
-        return 0, rb.reader.TryError
-    }
-    return res, nil
+	rb.pos += uint64(bitLength)
+	res := int8(rb.reader.TryReadBits(bitLength))
+	if rb.reader.TryError != nil {
+		return 0, rb.reader.TryError
+	}
+	return res, nil
 }
 
 func (rb *ReadBuffer) ReadInt16(bitLength uint8) (int16, error) {
-    rb.pos += uint64(bitLength)
-    res := int16(rb.reader.TryReadBits(bitLength))
-    if rb.reader.TryError != nil {
-        return 0, rb.reader.TryError
-    }
-    return res, nil
+	rb.pos += uint64(bitLength)
+	res := int16(rb.reader.TryReadBits(bitLength))
+	if rb.reader.TryError != nil {
+		return 0, rb.reader.TryError
+	}
+	return res, nil
 }
 
 func (rb *ReadBuffer) ReadInt32(bitLength uint8) (int32, error) {
-    rb.pos += uint64(bitLength)
-    res := int32(rb.reader.TryReadBits(bitLength))
-    if rb.reader.TryError != nil {
-        return 0, rb.reader.TryError
-    }
-    return res, nil
+	rb.pos += uint64(bitLength)
+	res := int32(rb.reader.TryReadBits(bitLength))
+	if rb.reader.TryError != nil {
+		return 0, rb.reader.TryError
+	}
+	return res, nil
 }
 
 func (rb *ReadBuffer) ReadInt64(bitLength uint8) (int64, error) {
-    rb.pos += uint64(bitLength)
-    res := int64(rb.reader.TryReadBits(bitLength))
-    if rb.reader.TryError != nil {
-        return 0, rb.reader.TryError
-    }
-    return res, nil
+	rb.pos += uint64(bitLength)
+	res := int64(rb.reader.TryReadBits(bitLength))
+	if rb.reader.TryError != nil {
+		return 0, rb.reader.TryError
+	}
+	return res, nil
 }
 
 func (rb *ReadBuffer) ReadBigInt(bitLength uint64) (*big.Int, error) {
-    // TODO: highly experimental remove this comment when tested or verifyed
-    res := big.NewInt(0)
+	// TODO: highly experimental remove this comment when tested or verifyed
+	res := big.NewInt(0)
 
-    // TODO: maybe we can use left shift of big int
-    rawBytes := make([]byte, 0)
-    correction := uint8(0)
+	// TODO: maybe we can use left shift and or of big int
+	rawBytes := make([]byte, 0)
+	correction := uint8(0)
 
-    for remainingBits := bitLength; remainingBits > 0; {
-        fmt.Printf("%d <--- remainingBits\n", remainingBits)
-        // we can max read 64 bit with bitio
-        bitToRead := uint8(64)
-        if remainingBits < 64 {
-            bitToRead = uint8(remainingBits)
-        }
-        // we now read the bits
-        data := rb.reader.TryReadBits(bitToRead)
-        fmt.Printf("0x%x <--- read %dbits from bitio\n", data, bitToRead)
+	for remainingBits := bitLength; remainingBits > 0; {
+		// we can max read 64 bit with bitio
+		bitToRead := uint8(64)
+		if remainingBits < 64 {
+			bitToRead = uint8(remainingBits)
+		}
+		// we now read the bits
+		data := rb.reader.TryReadBits(bitToRead)
 
-        // and check for uneven bits for a right shift at the end
-        correction = bits.UintSize - bitToRead
-        data <<= correction
-        fmt.Printf("0x%x <--- shifted with correction %d\n", data, correction)
+		// and check for uneven bits for a right shift at the end
+		correction = 64 - bitToRead
+		data <<= correction
 
-        dataBytes := make([]byte, 8)
-        binary.BigEndian.PutUint64(dataBytes, data)
-        fmt.Printf("0x%x <-- uint64 to byte[%d]\n", dataBytes, len(dataBytes))
-        rawBytes = append(rawBytes, dataBytes...)
+		dataBytes := make([]byte, 8)
+		binary.BigEndian.PutUint64(dataBytes, data)
+		rawBytes = append(rawBytes, dataBytes...)
 
-        if rb.reader.TryError != nil {
-            return big.NewInt(0), rb.reader.TryError
-        }
-        remainingBits -= uint64(bitToRead)
-    }
-    fmt.Printf("rawbytes\n 0x%x\n", rawBytes)
+		if rb.reader.TryError != nil {
+			return big.NewInt(0), rb.reader.TryError
+		}
+		remainingBits -= uint64(bitToRead)
+	}
 
-    res.SetBytes(rawBytes)
+	res.SetBytes(rawBytes)
 
-    fmt.Printf("need to shift now for %d. Before correction\n 0x%x\n", correction, res)
-    // now we need to shift the last correction to right again
-    res.Rsh(res, uint(correction))
+	// now we need to shift the last correction to right again
+	res.Rsh(res, uint(correction))
 
-    return res, nil
+	return res, nil
 }
 
 func (rb *ReadBuffer) ReadFloat32(signed bool, exponentBitLength uint8, mantissaBitLength uint8) (float32, error) {
-    bitLength := exponentBitLength + mantissaBitLength
-    if signed {
-        bitLength++
-    }
-    if signed && exponentBitLength == 8 && mantissaBitLength == 23 {
-        rb.pos += uint64(bitLength)
-        uintValue := uint32(rb.reader.TryReadBits(bitLength))
-        res := math.Float32frombits(uintValue)
-        if rb.reader.TryError != nil {
-            return 0, rb.reader.TryError
-        }
-        return res, nil
-    } else if bitLength < 32 {
-        // TODO: Note ... this is the format as described in the KNX specification
-        sign := true
-        var err error
-        if signed {
-            sign, err = rb.ReadBit()
-            if err != nil {
-                return 0.0, errors.New("error reading sign")
-            }
-        }
-        exp, err := rb.ReadInt32(exponentBitLength)
-        if err != nil {
-            return 0.0, errors.New("error reading exponent")
-        }
-        mantissa, err := rb.ReadUint32(mantissaBitLength)
-        // In the mantissa notation actually the first bit is omitted, we need to add it back
-        f := (0.01 * float64(mantissa)) * math.Pow(float64(2), float64(exp))
-        if sign {
-            return -float32(f), nil
-        }
-        return float32(f), nil
-    } else {
-        return 0.0, errors.New("too many bits for float32")
-    }
+	bitLength := exponentBitLength + mantissaBitLength
+	if signed {
+		bitLength++
+	}
+	if signed && exponentBitLength == 8 && mantissaBitLength == 23 {
+		rb.pos += uint64(bitLength)
+		uintValue := uint32(rb.reader.TryReadBits(bitLength))
+		res := math.Float32frombits(uintValue)
+		if rb.reader.TryError != nil {
+			return 0, rb.reader.TryError
+		}
+		return res, nil
+	} else if bitLength < 32 {
+		// TODO: Note ... this is the format as described in the KNX specification
+		sign := true
+		var err error
+		if signed {
+			sign, err = rb.ReadBit()
+			if err != nil {
+				return 0.0, errors.New("error reading sign")
+			}
+		}
+		exp, err := rb.ReadInt32(exponentBitLength)
+		if err != nil {
+			return 0.0, errors.New("error reading exponent")
+		}
+		mantissa, err := rb.ReadUint32(mantissaBitLength)
+		// In the mantissa notation actually the first bit is omitted, we need to add it back
+		f := (0.01 * float64(mantissa)) * math.Pow(float64(2), float64(exp))
+		if sign {
+			return -float32(f), nil
+		}
+		return float32(f), nil
+	} else {
+		return 0.0, errors.New("too many bits for float32")
+	}
 }
 
 func (rb *ReadBuffer) ReadFloat64(signed bool, exponentBitLength uint8, mantissaBitLength uint8) (float64, error) {
-    bitLength := 1 + exponentBitLength + mantissaBitLength
-    rb.pos += uint64(bitLength)
-    uintValue := rb.reader.TryReadBits(bitLength)
-    res := math.Float64frombits(uintValue)
-    if rb.reader.TryError != nil {
-        return 0, rb.reader.TryError
-    }
-    return res, nil
+	bitLength := 1 + exponentBitLength + mantissaBitLength
+	rb.pos += uint64(bitLength)
+	uintValue := rb.reader.TryReadBits(bitLength)
+	res := math.Float64frombits(uintValue)
+	if rb.reader.TryError != nil {
+		return 0, rb.reader.TryError
+	}
+	return res, nil
 }
 
 func (rb *ReadBuffer) ReadBigFloat(signed bool, exponentBitLength uint8, mantissaBitLength uint8) (*big.Float, error) {
-    // TODO: highly experimental remove this comment when tested or verifyed
-    return nil, errors.New("not implemented yet")
+	// TODO: highly experimental remove this comment when tested or verifyed
+	return nil, errors.New("not implemented yet")
 }
 
 func (rb *ReadBuffer) ReadString(bitLength uint8) (string, error) {
-    rb.pos += uint64(bitLength)
-    res := string(rb.reader.TryReadBits(bitLength))
-    if rb.reader.TryError != nil {
-        return "", rb.reader.TryError
-    }
-    return res, nil
+	rb.pos += uint64(bitLength)
+	res := string(rb.reader.TryReadBits(bitLength))
+	if rb.reader.TryError != nil {
+		return "", rb.reader.TryError
+	}
+	return res, nil
 }