tape: Integer encoding accepts oversize buffers now
This commit is contained in:
parent
2080d60793
commit
4f3b256821
@ -35,26 +35,26 @@ type String interface { ~string }
|
||||
|
||||
// DecodeI8 decodes an 8 bit integer from the given data.
|
||||
func DecodeI8[T Int8](data []byte) (value T, n int, err error) {
|
||||
if len(data) != 1 { return 0, 0, fmt.Errorf("decoding int8: %w", ErrWrongBufferLength) }
|
||||
if len(data) < 1 { return 0, 0, fmt.Errorf("decoding int8: %w", ErrWrongBufferLength) }
|
||||
return T(data[0]), 1, nil
|
||||
}
|
||||
|
||||
// EncodeI8 encodes an 8 bit integer into the given buffer.
|
||||
func EncodeI8[T Int8](buffer []byte, value T) (n int, err error) {
|
||||
if len(buffer) != 1 { return 0, fmt.Errorf("encoding int8: %w", ErrWrongBufferLength) }
|
||||
if len(buffer) < 1 { return 0, fmt.Errorf("encoding int8: %w", ErrWrongBufferLength) }
|
||||
buffer[0] = byte(value)
|
||||
return 1, nil
|
||||
}
|
||||
|
||||
// DecodeI16 decodes a 16 bit integer from the given data.
|
||||
func DecodeI16[T Int16](data []byte) (value T, n int, err error) {
|
||||
if len(data) != 2 { return 0, 0, fmt.Errorf("decoding int16: %w", ErrWrongBufferLength) }
|
||||
if len(data) < 2 { return 0, 0, fmt.Errorf("decoding int16: %w", ErrWrongBufferLength) }
|
||||
return T(data[0]) << 8 | T(data[1]), 2, nil
|
||||
}
|
||||
|
||||
// EncodeI16 encodes a 16 bit integer into the given buffer.
|
||||
func EncodeI16[T Int16](buffer []byte, value T) (n int, err error) {
|
||||
if len(buffer) != 2 { return 0, fmt.Errorf("encoding int16: %w", ErrWrongBufferLength) }
|
||||
if len(buffer) < 2 { return 0, fmt.Errorf("encoding int16: %w", ErrWrongBufferLength) }
|
||||
buffer[0] = byte(value >> 8)
|
||||
buffer[1] = byte(value)
|
||||
return 2, nil
|
||||
@ -62,7 +62,7 @@ func EncodeI16[T Int16](buffer []byte, value T) (n int, err error) {
|
||||
|
||||
// DecodeI32 decodes a 32 bit integer from the given data.
|
||||
func DecodeI32[T Int32](data []byte) (value T, n int, err error) {
|
||||
if len(data) != 4 { return 0, 0, fmt.Errorf("decoding int32: %w", ErrWrongBufferLength) }
|
||||
if len(data) < 4 { return 0, 0, fmt.Errorf("decoding int32: %w", ErrWrongBufferLength) }
|
||||
return T(data[0]) << 24 |
|
||||
T(data[1]) << 16 |
|
||||
T(data[2]) << 8 |
|
||||
@ -71,7 +71,7 @@ func DecodeI32[T Int32](data []byte) (value T, n int, err error) {
|
||||
|
||||
// EncodeI32 encodes a 32 bit integer into the given buffer.
|
||||
func EncodeI32[T Int32](buffer []byte, value T) (n int, err error) {
|
||||
if len(buffer) != 4 { return 0, fmt.Errorf("encoding int32: %w", ErrWrongBufferLength) }
|
||||
if len(buffer) < 4 { return 0, fmt.Errorf("encoding int32: %w", ErrWrongBufferLength) }
|
||||
buffer[0] = byte(value >> 24)
|
||||
buffer[1] = byte(value >> 16)
|
||||
buffer[2] = byte(value >> 8)
|
||||
@ -81,7 +81,7 @@ func EncodeI32[T Int32](buffer []byte, value T) (n int, err error) {
|
||||
|
||||
// DecodeI64 decodes a 64 bit integer from the given data.
|
||||
func DecodeI64[T Int64](data []byte) (value T, n int, err error) {
|
||||
if len(data) != 8 { return 0, 0, fmt.Errorf("decoding int64: %w", ErrWrongBufferLength) }
|
||||
if len(data) < 8 { return 0, 0, fmt.Errorf("decoding int64: %w", ErrWrongBufferLength) }
|
||||
return T(data[0]) << 56 |
|
||||
T(data[1]) << 48 |
|
||||
T(data[2]) << 40 |
|
||||
@ -94,7 +94,7 @@ func DecodeI64[T Int64](data []byte) (value T, n int, err error) {
|
||||
|
||||
// EncodeI64 encodes a 64 bit integer into the given buffer.
|
||||
func EncodeI64[T Int64](buffer []byte, value T) (n int, err error) {
|
||||
if len(buffer) != 8 { return 0, fmt.Errorf("encoding int64: %w", ErrWrongBufferLength) }
|
||||
if len(buffer) < 8 { return 0, fmt.Errorf("encoding int64: %w", ErrWrongBufferLength) }
|
||||
buffer[0] = byte(value >> 56)
|
||||
buffer[1] = byte(value >> 48)
|
||||
buffer[2] = byte(value >> 40)
|
||||
|
@ -12,11 +12,11 @@ const randStringBytes = "-abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTU
|
||||
func TestI8(test *testing.T) {
|
||||
var buffer [16]byte
|
||||
_, err := EncodeI8[uint8](buffer[:], 5)
|
||||
if err.Error() != "encoding int8: wrong buffer length" { test.Fatal(err) }
|
||||
if err != nil { test.Fatal(err) }
|
||||
_, err = EncodeI8[uint8](buffer[:0], 5)
|
||||
if err.Error() != "encoding int8: wrong buffer length" { test.Fatal(err) }
|
||||
_, _, err = DecodeI8[uint8](buffer[:])
|
||||
if err.Error() != "decoding int8: wrong buffer length" { test.Fatal(err) }
|
||||
if err != nil { test.Fatal(err) }
|
||||
_, _, err = DecodeI8[uint8](buffer[:0])
|
||||
if err.Error() != "decoding int8: wrong buffer length" { test.Fatal(err) }
|
||||
|
||||
@ -34,11 +34,11 @@ func TestI8(test *testing.T) {
|
||||
func TestI16(test *testing.T) {
|
||||
var buffer [16]byte
|
||||
_, err := EncodeI16[uint16](buffer[:], 5)
|
||||
if err.Error() != "encoding int16: wrong buffer length" { test.Fatal(err) }
|
||||
if err != nil { test.Fatal(err) }
|
||||
_, err = EncodeI16[uint16](buffer[:0], 5)
|
||||
if err.Error() != "encoding int16: wrong buffer length" { test.Fatal(err) }
|
||||
_, _, err = DecodeI16[uint16](buffer[:])
|
||||
if err.Error() != "decoding int16: wrong buffer length" { test.Fatal(err) }
|
||||
if err != nil { test.Fatal(err) }
|
||||
_, _, err = DecodeI16[uint16](buffer[:0])
|
||||
if err.Error() != "decoding int16: wrong buffer length" { test.Fatal(err) }
|
||||
|
||||
@ -57,11 +57,11 @@ func TestI16(test *testing.T) {
|
||||
func TestI32(test *testing.T) {
|
||||
var buffer [16]byte
|
||||
_, err := EncodeI32[uint32](buffer[:], 5)
|
||||
if err.Error() != "encoding int32: wrong buffer length" { test.Fatal(err) }
|
||||
if err != nil { test.Fatal(err) }
|
||||
_, err = EncodeI32[uint32](buffer[:0], 5)
|
||||
if err.Error() != "encoding int32: wrong buffer length" { test.Fatal(err) }
|
||||
_, _, err = DecodeI32[uint32](buffer[:])
|
||||
if err.Error() != "decoding int32: wrong buffer length" { test.Fatal(err) }
|
||||
if err != nil { test.Fatal(err) }
|
||||
_, _, err = DecodeI32[uint32](buffer[:0])
|
||||
if err.Error() != "decoding int32: wrong buffer length" { test.Fatal(err) }
|
||||
|
||||
@ -80,11 +80,11 @@ func TestI32(test *testing.T) {
|
||||
func TestI64(test *testing.T) {
|
||||
var buffer [16]byte
|
||||
_, err := EncodeI64[uint64](buffer[:], 5)
|
||||
if err.Error() != "encoding int64: wrong buffer length" { test.Fatal(err) }
|
||||
if err != nil { test.Fatal(err) }
|
||||
_, err = EncodeI64[uint64](buffer[:0], 5)
|
||||
if err.Error() != "encoding int64: wrong buffer length" { test.Fatal(err) }
|
||||
_, _, err = DecodeI64[uint64](buffer[:])
|
||||
if err.Error() != "decoding int64: wrong buffer length" { test.Fatal(err) }
|
||||
if err != nil { test.Fatal(err) }
|
||||
_, _, err = DecodeI64[uint64](buffer[:0])
|
||||
if err.Error() != "decoding int64: wrong buffer length" { test.Fatal(err) }
|
||||
|
||||
|
Loading…
Reference in New Issue
Block a user