Documentation ¶
Index ¶
- Constants
- func EncodeBool(n int, b []byte, v bool, id uint16) int
- func EncodeFloat32(n int, b []byte, v float32, id uint16) int
- func EncodeFloat64(n int, b []byte, v float64, id uint16) int
- func EncodeInt(n int, b []byte, v int, id uint16) int
- func EncodeInt16(n int, b []byte, v int16, id uint16) int
- func EncodeInt32(n int, b []byte, v int32, id uint16) int
- func EncodeInt64(n int, b []byte, v int64, id uint16) int
- func EncodeInt8(n int, b []byte, v int8, id uint16) int
- func EncodeSafeString(n int, b []byte, v string, id uint16) int
- func EncodeString(n int, b []byte, v string, id uint16) int
- func EncodeUint(n int, b []byte, v uint, id uint16) int
- func EncodeUint16(n int, b []byte, v uint16, id uint16) int
- func EncodeUint32(n int, b []byte, v uint32, id uint16) int
- func EncodeUint64(n int, b []byte, v uint64, id uint16) int
- func EncodeUint8(n int, b []byte, v uint8, id uint16) int
- func SizeBool(id uint16) int
- func SizeContainer(id uint16) int
- func SizeFloat32(id uint16) int
- func SizeFloat64(id uint16) int
- func SizeInt(v int, id uint16) int
- func SizeInt16(id uint16) int
- func SizeInt32(id uint16) int
- func SizeInt64(id uint16) int
- func SizeInt8(id uint16) int
- func SizeString(v string, id uint16) int
- func SizeUint(v uint, id uint16) int
- func SizeUint16(id uint16) int
- func SizeUint32(id uint16) int
- func SizeUint64(id uint16) int
- func SizeUint8(id uint16) int
- type Benc
- type Ctr
- type Error
- func DecodeBool(n int, b []byte, r []uint16, id uint16) (int, bool, Error[string])
- func DecodeContainer(n int, b []byte, r []uint16, id uint16, dec func(n int) (int, Error[string])) (int, Error[string])
- func DecodeFloat32(n int, b []byte, r []uint16, id uint16) (int, float32, Error[string])
- func DecodeFloat64(n int, b []byte, r []uint16, id uint16) (int, float64, Error[string])
- func DecodeInt(n int, b []byte, r []uint16, id uint16) (int, int, Error[string])
- func DecodeInt16(n int, b []byte, r []uint16, id uint16) (int, int16, Error[string])
- func DecodeInt32(n int, b []byte, r []uint16, id uint16) (int, int32, Error[string])
- func DecodeInt64(n int, b []byte, r []uint16, id uint16) (int, int64, Error[string])
- func DecodeInt8(n int, b []byte, r []uint16, id uint16) (int, int8, Error[string])
- func DecodeSafeString(n int, b []byte, r []uint16, id uint16) (int, string, Error[string])
- func DecodeString(n int, b []byte, r []uint16, id uint16) (int, string, Error[string])
- func DecodeUint(n int, b []byte, r []uint16, id uint16) (int, uint, Error[string])
- func DecodeUint16(n int, b []byte, r []uint16, id uint16) (int, uint16, Error[string])
- func DecodeUint32(n int, b []byte, r []uint16, id uint16) (int, uint32, Error[string])
- func DecodeUint64(n int, b []byte, r []uint16, id uint16) (int, uint64, Error[string])
- func DecodeUint8(n int, b []byte, r []uint16, id uint16) (int, uint8, Error[string])
- func EncodeContainer(n int, b []byte, id uint16, enc func(n int) (_ int, err Error[string])) (int, Error[string])
- func NewErr[T ~string](format T, values ...any) Error[T]
- func NewNoneErr[T ~string]() Error[T]
- func SkipEncodedValueByType(n int, b []byte, typ uint8) (int, Error[string])
Constants ¶
const ( Container uint8 = iota Varint LenDlm Fixed8 Fixed16 Fixed32 Fixed64 )
const ErrBufTooSmall = "buffer too small"
ErrBufTooSmall indicates that the buffer given is too small to decode data.
const ErrEof = "eof"
ErrEof indicates not an error, rather indicates that no data can be decoded anymore.
const ErrIdIsZero = "specified id cannot be 0"
ErrIdIsZero indicates that the ID may not be 0.
const ErrInvalidType = "invalid type"
ErrInvalidType indicates that the decoded type is invalid.
const ErrNone = ""
ErrNone indicates that no error occoured.
const ErrOverflow = "overflow"
ErrOverflow indicates that the bytes number limit of the type was exceeded.
const ErrReuseBufTooSmall = "reuse buffer too small"
ErrReuseBufTooSmall indicates that the buffer is too small to be reused at the specified size.
Variables ¶
This section is empty.
Functions ¶
func EncodeBool ¶ added in v1.0.0
EncodeBool: ¶
Encodes the given bool at the given offset, and returns the new offset.
Important: ¶
`id` may not be 0
Bool: ¶
id type bool // uint16 byte->Fixed8 byte->1 | byte->0
func EncodeFloat32 ¶ added in v1.0.0
EncodeFloat32: ¶
Encodes the given 32-bit float at the given offset, and returns the new offset.
Important: ¶
`id` may not be 0
Float32: ¶
id type float32 // uint16 byte->Fixed32 4 bytes
func EncodeFloat64 ¶ added in v1.0.0
EncodeFloat64: ¶
Encodes the given 64-bit float at the given offset, and returns the new offset.
Important: ¶
`id` may not be 0
Float64: ¶
id type float64 // uint16 byte->Fixed64 8 bytes
func EncodeInt ¶ added in v1.0.0
EncodeInt: ¶
Encodes the given integer at the given offset, and returns the new offset.
Important: ¶
`id` may not be 0
Int: ¶
id type int // uint16 byte->Varint []byte
func EncodeInt16 ¶ added in v1.0.0
EncodeInt16: ¶
Encodes the given 16-bit integer at the given offset, and returns the new offset.
Important: ¶
`id` may not be 0
Int16: ¶
id type int16 // uint16 byte->Fixed16 2 bytes
func EncodeInt32 ¶ added in v1.0.0
EncodeInt32: ¶
Encodes the given 32-bit integer at the given offset, and returns the new offset.
Important: ¶
`id` may not be 0
Int32: ¶
id type int32 // uint16 byte->Fixed32 4 bytes
func EncodeInt64 ¶ added in v1.0.0
EncodeInt64: ¶
Encodes the given 64-bit integer at the given offset, and returns the new offset.
Important: ¶
`id` may not be 0
Int64: ¶
id type int64 // uint16 byte->Fixed64 8 bytes
func EncodeInt8 ¶ added in v1.0.0
EncodeInt8: ¶
Encodes the given 8-bit integer at the given offset, and returns the new offset.
Important: ¶
`id` may not be 0
Int8: ¶
id type int8 // uint16 byte->Fixed8 byte
func EncodeSafeString ¶ added in v1.0.0
EncodeSafeString: ¶
Encodes the given string at the given offset, and returns the new offset.
Important: ¶
`id` may not be 0
String: ¶
id type string size string // uint16 byte->LenDlm uint []byte
func EncodeString ¶ added in v1.0.0
EncodeString: ¶
Encodes the given string at the given offset, and returns the new offset.
Important: ¶
`id` may not be 0
Uses zero-allocation string conversion, using the `unsafe` package, to do safe string conversions use `EncodeSafeString`
String: ¶
id type string size string // uint16 byte->LenDlm uint []byte
func EncodeUint ¶ added in v1.0.0
EncodeUint: ¶
Encodes the given unsigned integer at the given offset, and returns the new offset.
Important: ¶
`id` may not be 0
Uint: ¶
id type uint // uint16 byte->Varint []byte
func EncodeUint16 ¶ added in v1.0.0
EncodeUint16: ¶
Encodes the given 16-bit unsigned integer at the given offset, and returns the new offset.
Important: ¶
`id` may not be 0
Uint16: ¶
id type uint16 // uint16 byte->Fixed16 2 bytes
func EncodeUint32 ¶ added in v1.0.0
EncodeUint32: ¶
Encodes the given 32-bit unsigned integer at the given offset, and returns the new offset.
Important: ¶
`id` may not be 0
Uint32: ¶
id type uint32 // uint16 byte->Fixed32 4 bytes
func EncodeUint64 ¶ added in v1.0.0
EncodeUint64: ¶
Encodes the given 64-bit unsigned integer at the given offset, and returns the new offset.
Important: ¶
`id` may not be 0
Uint64: ¶
id type uint64 // uint16 byte->Fixed64 8 bytes
func EncodeUint8 ¶ added in v1.0.0
EncodeUint8: ¶
Encodes the given 8-bit unsigned integer at the given offset, and returns the new offset.
Important: ¶
`id` may not be 0
Uint8: ¶
id type uint8 // uint16 byte->Fixed8 byte
func SizeBool ¶ added in v1.0.0
SizeBool: ¶
Sizes the given bool and returns its size in the benc format.
Bool: ¶
id type bool // uint16 byte->Fixed8 byte->1 | byte->0
func SizeContainer ¶ added in v1.0.0
SizeContainer: ¶
Calculates and returns the size required to encode a container in the Benc format.
Returns: ¶
The size, in bytes, needed to encode a container.
Container: ¶
Container Structure, in the Benc format:
id type content END END // uint16 byte->Container []byte byte->1 byte->1
func SizeFloat32 ¶ added in v1.0.0
SizeFloat32: ¶
Sizes the given 32-bit float and returns its size in the benc format.
Float32: ¶
id type float32 // uint16 byte->Fixed32 4 bytes
func SizeFloat64 ¶ added in v1.0.0
SizeFloat64: ¶
Sizes the given 64-bit float and returns its size in the benc format.
Float64: ¶
id type float64 // uint16 byte->Fixed64 8 bytes
func SizeInt ¶ added in v1.0.0
SizeInt: ¶
Sizes the given integer and returns its size in the benc format.
Int: ¶
id type int // uint16 byte->Varint []byte
func SizeInt16 ¶ added in v1.0.0
SizeInt16: ¶
Sizes the given 16-bit integer and returns its size in the benc format.
Int16: ¶
id type int16 // uint16 byte->Fixed16 2 bytes
func SizeInt32 ¶ added in v1.0.0
SizeInt32: ¶
Sizes the given 32-bit integer and returns its size in the benc format.
Int32: ¶
id type int32 // uint16 byte->Fixed32 4 bytes
func SizeInt64 ¶ added in v1.0.0
SizeInt64: ¶
Sizes the given 64-bit integer and returns its size in the benc format.
Int64: ¶
id type int64 // uint16 byte->Fixed64 8 bytes
func SizeInt8 ¶ added in v1.0.0
SizeInt8: ¶
Sizes the given 8-bit integer and returns its size in the benc format.
Int8: ¶
id type int8 // uint16 byte->Fixed8 byte
func SizeString ¶ added in v1.0.0
SizeString: ¶
Sizes the given string and returns its size in the benc format.
Note: ¶
Also for `EncodeSafeString` and `DecodeSafeString`
String: ¶
id type string size string // uint16 byte->LenDlm uint []byte
func SizeUint ¶ added in v1.0.0
SizeUint: ¶
Sizes the given unsigned integer and returns its size in the benc format.
Uint: ¶
id type uint // uint16 byte->Varint []byte
func SizeUint16 ¶ added in v1.0.0
SizeUint16: ¶
Sizes the given 16-bit unsigned integer and returns its size in the benc format.
Uint16: ¶
id type uint16 // uint16 byte->Fixed16 2 bytes
func SizeUint32 ¶ added in v1.0.0
SizeUint32: ¶
Sizes the given 32-bit unsigned integer and returns its size in the benc format.
Uint32: ¶
id type uint32 // uint16 byte->Fixed32 4 bytes
func SizeUint64 ¶ added in v1.0.0
SizeUint64: ¶
Sizes the given 64-bit unsigned integer and returns its size in the benc format.
Uint64: ¶
id type uint64 // uint16 byte->Fixed64 8 bytes
Types ¶
type Benc ¶
type Benc struct {
// contains filtered or unexported fields
}
Benc represents an encoder/decoder with a buffer for encoding and decoding data.
func NewEncoder ¶
NewEncoder: ¶
Creates a new encoder instance for encoding and decoding operations.
If a buffer size is provided, it allocates a buffer of the specified size. Otherwise, it allocates a buffer with a default size of 4096 bytes.
Example:
benc := bencv2.NewEncoder(1024)
func (*Benc) Decode ¶
Decode: ¶
Decodes the provided byte slice into the provided container (Ctr object).
Returns an Error[string] if decoding fails due to invalid data or buffer size limitations.
func (*Benc) Encode ¶
Encode: ¶
Encodes the provided container (Ctr object) into a byte slice.
Returns the byte slice containing the encoded container.
Returns an Error[string] if encoding fails due to invalid data or buffer size limitations.
func (*Benc) SetBufferReusing ¶ added in v0.0.4
SetBufferReusing: ¶
Sets whether buffer reusing is active or not.
type Ctr ¶ added in v1.0.0
type Ctr interface { // Size returns the size of the encoded container. Size(id uint16) int // Encode encodes the container into a byte slice. Encode(n int, b []byte, id uint16) (int, Error[string]) // Decode decodes the container from a byte slice. Decode(n int, b []byte, id uint16) (int, Error[string]) }
Ctr is an interface for encoding and decoding containers.
type Error ¶ added in v1.0.0
func DecodeBool ¶ added in v1.0.0
DecodeBool: ¶
Decodes a encoded bool at the given offset, returns the decoded bool and the new offset.
Bool: ¶
id type bool // uint16 byte->Fixed8 byte->1 | byte->0
func DecodeContainer ¶ added in v1.0.0
func DecodeContainer(n int, b []byte, r []uint16, id uint16, dec func(n int) (int, Error[string])) (int, Error[string])
DecodeContainer: ¶
Decodes a container in the Benc format.
r: A slice of reserved IDs.
dec: A function that decodes the content from the container.
Returns: ¶
The new offset after decoding the container or an Error[string] if decoding fails.
Container: ¶
Container Structure, in the Benc format:
id type content END END // uint16 byte->Container []byte byte->1 byte->1
func DecodeFloat32 ¶ added in v1.0.0
DecodeFloat32: ¶
Decodes a encoded 32-bit float at the given offset, returns the decoded 32-bit float and the new offset.
Float32: ¶
id type float32 // uint16 byte->Fixed32 4 bytes
func DecodeFloat64 ¶ added in v1.0.0
DecodeFloat64: ¶
Decodes a encoded 64-bit float at the given offset, returns the decoded 64-bit float and the new offset.
Float64: ¶
id type float64 // uint16 byte->Fixed64 8 bytes
func DecodeInt ¶ added in v1.0.0
DecodeInt: ¶
Decodes a encoded integer at the given offset, returns the decoded integer and the new offset.
Int: ¶
id type int // uint16 byte->Varint []byte
func DecodeInt16 ¶ added in v1.0.0
DecodeInt16: ¶
Decodes a encoded 16-bit integer at the given offset, returns the decoded 16-bit integer and the new offset.
Int16: ¶
id type int16 // uint16 byte->Fixed16 2 bytes
func DecodeInt32 ¶ added in v1.0.0
DecodeInt32: ¶
Decodes a encoded 32-bit integer at the given offset, returns the decoded 32-bit integer and the new offset.
Int32: ¶
id type int32 // uint16 byte->Fixed32 4 bytes
func DecodeInt64 ¶ added in v1.0.0
DecodeInt64: ¶
Decodes a encoded 64-bit integer at the given offset, returns the decoded 64-bit integer and the new offset.
Int64: ¶
id type int64 // uint16 byte->Fixed64 8 bytes
func DecodeInt8 ¶ added in v1.0.0
DecodeInt8: ¶
Decodes a encoded 8-bit integer at the given offset, returns the decoded 8-bit integer and the new offset.
Int8: ¶
id type int8 // uint16 byte->Fixed8 byte
func DecodeSafeString ¶ added in v1.0.0
DecodeSafeString: ¶
Decodes a encoded string at the given offset, returns the decoded string and the new offset.
String: ¶
id type string size string // uint16 byte->LenDlm uint []byte
func DecodeString ¶ added in v1.0.0
DecodeString: ¶
Decodes a encoded string at the given offset, returns the decoded string and the new offset.
String: ¶
id type string size string // uint16 byte->LenDlm uint []byte
func DecodeUint ¶ added in v1.0.0
DecodeUint: ¶
Decodes a encoded unsigned integer at the given offset, returns the decoded unsigned integer and the new offset.
Uint: ¶
id type uint // uint16 byte->Varint []byte
func DecodeUint16 ¶ added in v1.0.0
DecodeUint16: ¶
Decodes a encoded 16-bit unsigned integer at the given offset, returns the decoded 16-bit unsigned integer and the new offset.
Uint16: ¶
id type uint16 // uint16 byte->Fixed16 2 bytes
func DecodeUint32 ¶ added in v1.0.0
DecodeUint32: ¶
Decodes a encoded 32-bit unsigned integer at the given offset, returns the decoded 32-bit unsigned integer and the new offset.
Uint32: ¶
id type uint32 // uint16 byte->Fixed32 4 bytes
func DecodeUint64 ¶ added in v1.0.0
DecodeUint64: ¶
Decodes a encoded 64-bit unsigned integer at the given offset, returns the decoded 64-bit unsigned integer and the new offset.
Uint64: ¶
id type uint64 // uint16 byte->Fixed64 8 bytes
func DecodeUint8 ¶ added in v1.0.0
DecodeUint8: ¶
Decodes a encoded 8-bit unsigned integer at the given offset, returns the decoded 8-bit unsigned integer and the new offset.
Uint8: ¶
id type uint8 // uint16 byte->Fixed8 byte
func EncodeContainer ¶ added in v1.0.0
func EncodeContainer(n int, b []byte, id uint16, enc func(n int) (_ int, err Error[string])) (int, Error[string])
EncodeContainer: ¶
Encodes a container in the Benc format. If the container is the entry point, the ID has to be 0.
enc: A function that encodes the content of the message.
Returns: ¶
The new offset after encoding the message.
Container: ¶
Container Structure, in the Benc format:
id type content END END // uint16 byte->Container []byte byte->1 byte->1
func NewNoneErr ¶ added in v1.0.0
func SkipEncodedValueByType ¶ added in v1.0.0
SkipEncodedValueByType: ¶
Skips the encoding of a value based on its data type.