ADDED:
* netx.IsPub
* encodingx/hexx

Rest are mostly small corrections and docs
This commit is contained in:
brent saner
2026-06-22 18:51:13 -04:00
parent c6fc692f5e
commit 58556d7281
35 changed files with 5492 additions and 2486 deletions
+4
View File
@@ -0,0 +1,4 @@
/*
Package binaryx aims to extend functionality of the stdlib [encoding/binary] module.
*/
package binaryx
+122
View File
@@ -0,0 +1,122 @@
package binaryx
import (
"bytes"
"encoding"
"encoding/binary"
)
/*
Marshal provides a Golang convention marshaling function entry point
(e.g. like [encoding/json.Marshal], [encoding/xml.Marshal]) for arbitrary
binary data.
It simply wraps [OrderedMarshal] with [encoding/binary.NativeEndian]
(which can differ from platform to platform) as the ord parameter to [OrderedMarshal].
If you need to use a different or explicit [encoding/binary.ByteOrder],
use [OrderedMarshal] directly instead.
*/
func Marshal(v any) (data []byte, err error) {
if data, err = OrderedMarshal(v, binary.NativeEndian); err != nil {
return
}
return
}
/*
OrderedMarshal provides a slightly more flexible Golang convention marshaling
function entry point (e.g. similar to [encoding/json.MarshalIndent],
[encoding/xml.MarshalIndent]) for arbitrary binary data.
If v conforms to [encoding.BinaryMarshaler], then the marshal method will be called.
Otherwise, it wraps [encoding/binary.Write] with an internal buffer using ord
as the [encoding/binary.ByteOrder] (endianness), so refer to that function
for all caveats and other details.
Note that [encoding/binary.Write] is *much* more strict than marshaling,
and requires very basic typing.
If you have no need for a specific byte order/endianness or want to explicitly use this
system's native byte order/endianness,
use [Marshal] instead.
*/
func OrderedMarshal(v any, ord binary.ByteOrder) (data []byte, err error) {
var ok bool
var b binary.Marshaler
var buf *bytes.Buffer = new(bytes.Buffer)
if b, ok = v.(encoding.BinaryMarshaler); ok {
if data, err = b.MarshalBinary(); err != nil {
return
}
return
}
if err = binary.Write(buf, ord, v); err != nil {
return
}
data = buf.Bytes()
return
}
/*
Unmarshal provides a Golang convention unmarshaling function entry point
(e.g. like [encoding/json.Unmarshal], [encoding/xml.Unmarshal]) for arbitrary
binary data.
It simply wraps [OrderedUnmarshal] with [encoding/binary.NativeEndian]
(which can differ from platform to platform) as the ord parameter to [OrderedUnmarshal].
If you need to use a different or explicit [encoding/binary.ByteOrder],
use [OrderedUnmarshal] directly instead.
*/
func Unmarshal(data []byte, v any) (err error) {
if err = OrderedUnmarshal(data, v, binary.NativeEndian); err != nil {
return
}
return
}
/*
OrderedUnmarshal provides a slightly more flexible Golang convention unmarshaling
for arbitrary binary data.
If v conforms to [encoding.BinaryUnmarshaler], then the unmarshal method will be called.
Otherwise, it wraps [encoding/binary.Read] with an internal buffer using ord
as the [encoding/binary.ByteOrder] (endianness), so refer to that function
for all caveats and other details.
Note that [encoding/binary.Read] is *much* more strict than unmarshaling,
and requires very basic typing.
If you have no need for a specific byte order/endianness or want to explicitly use this
system's native byte order/endianness,
use [Unmarshal] instead.
*/
func OrderedUnmarshal(data []byte, v any, ord binary.ByteOrder) (err error) {
var ok bool
var b encoding.BinaryUnmarshaler
var buf *bytes.Reader = bytes.NewReader(data)
if b, ok = v.(encoding.BinaryUnmarshaler); ok {
if err = b.UnmarshalBinary(data); err != nil {
return
}
return
}
if err = binary.Read(buf, ord, v); err != nil {
return
}
return
}