Introduced `lol.Tracer` for function entry/exit logging across various packages. This improves traceability and debugging of function executions while preserving existing behavior. Removed unused files `doc.go` and `nothing.go` to clean up the repository.
42 lines
1.0 KiB
Go
42 lines
1.0 KiB
Go
package json
|
|
|
|
import (
|
|
"realy.lol/chk"
|
|
"realy.lol/errorf"
|
|
"realy.lol/hex"
|
|
"realy.lol/lol"
|
|
"realy.lol/text"
|
|
)
|
|
|
|
// Hex is a string representing binary data encoded as hexadecimal.
|
|
type Hex struct{ V []byte }
|
|
|
|
// Marshal a byte string into hexadecimal wrapped in double quotes.
|
|
func (h *Hex) Marshal(dst []byte) (b []byte) {
|
|
lol.Tracer("Marshal", dst)
|
|
defer func() { lol.Tracer("end Marshal", b) }()
|
|
b = dst
|
|
b = append(b, '"')
|
|
b = hex.EncAppend(b, h.V)
|
|
b = append(b, '"')
|
|
return
|
|
}
|
|
|
|
// Unmarshal a string wrapped in double quotes that should be a hexadecimal string. If it fails,
|
|
// it will return an error.
|
|
func (h *Hex) Unmarshal(dst []byte) (rem []byte, err error) {
|
|
lol.Tracer("Unmarshal", dst)
|
|
defer func() { lol.Tracer("end Unmarshal", rem, err) }()
|
|
var c []byte
|
|
if c, rem, err = text.UnmarshalQuoted(dst); chk.E(err) {
|
|
return
|
|
}
|
|
h.V = make([]byte, len(c)/2)
|
|
var n int
|
|
if n, err = hex.DecBytes(h.V, c); chk.E(err) {
|
|
err = errorf.E("failed to decode hex at position %d: %s", n, err)
|
|
return
|
|
}
|
|
return
|
|
}
|