aliases.go raw
1 // Package hex is a set of aliases and helpers for using the templexxx SIMD hex
2 // encoder.
3 package hex
4
5 import (
6 "encoding/hex"
7
8 "github.com/templexxx/xhex"
9 "next.orly.dev/pkg/lol/chk"
10 "next.orly.dev/pkg/lol/errorf"
11 )
12
13 var Enc = hex.EncodeToString
14 var EncBytes = hex.Encode
15 var Dec = hex.DecodeString
16 var DecBytes = hex.Decode
17
18 // var EncAppend = hex.AppendEncode
19 // var DecAppend = hex.AppendDecode
20
21 var DecLen = hex.DecodedLen
22
23 type InvalidByteError = hex.InvalidByteError
24
25 // EncAppend uses xhex to encode a sice of bytes and appends it to a provided destination slice.
26 func EncAppend(dst, src []byte) (b []byte) {
27 l := len(dst)
28 dst = append(dst, make([]byte, len(src)*2)...)
29 xhex.Encode(dst[l:], src)
30 return dst
31 }
32
33 // DecAppend decodes a provided encoded hex encoded string and appends the decoded output to a
34 // provided input slice.
35 func DecAppend(dst, src []byte) (b []byte, err error) {
36 if src == nil || len(src) < 2 {
37 err = errorf.E("nothing to decode")
38 return
39 }
40 if dst == nil {
41 dst = []byte{}
42 }
43 l := len(dst)
44 b = dst
45 b = append(b, make([]byte, len(src)/2)...)
46 if err = xhex.Decode(b[l:], src); chk.T(err) {
47 return
48 }
49 return
50 }
51