|
|
|
@ -35,26 +35,26 @@ type String interface { ~string }
|
|
|
|
|
|
|
|
|
|
|
|
// DecodeI8 decodes an 8 bit integer from the given data.
|
|
|
|
// DecodeI8 decodes an 8 bit integer from the given data.
|
|
|
|
func DecodeI8[T Int8](data []byte) (value T, n int, err error) {
|
|
|
|
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
|
|
|
|
return T(data[0]), 1, nil
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
// EncodeI8 encodes an 8 bit integer into the given buffer.
|
|
|
|
// EncodeI8 encodes an 8 bit integer into the given buffer.
|
|
|
|
func EncodeI8[T Int8](buffer []byte, value T) (n int, err error) {
|
|
|
|
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)
|
|
|
|
buffer[0] = byte(value)
|
|
|
|
return 1, nil
|
|
|
|
return 1, nil
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
// DecodeI16 decodes a 16 bit integer from the given data.
|
|
|
|
// DecodeI16 decodes a 16 bit integer from the given data.
|
|
|
|
func DecodeI16[T Int16](data []byte) (value T, n int, err error) {
|
|
|
|
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
|
|
|
|
return T(data[0]) << 8 | T(data[1]), 2, nil
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
// EncodeI16 encodes a 16 bit integer into the given buffer.
|
|
|
|
// EncodeI16 encodes a 16 bit integer into the given buffer.
|
|
|
|
func EncodeI16[T Int16](buffer []byte, value T) (n int, err error) {
|
|
|
|
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[0] = byte(value >> 8)
|
|
|
|
buffer[1] = byte(value)
|
|
|
|
buffer[1] = byte(value)
|
|
|
|
return 2, nil
|
|
|
|
return 2, nil
|
|
|