hex.mx raw

   1  package hex
   2  
   3  import (
   4  	"encoding/hex"
   5  
   6  	"smesh.lol/pkg/lol/errorf"
   7  )
   8  
   9  var Enc = hex.EncodeToString
  10  var EncBytes = hex.Encode
  11  var Dec = hex.DecodeString
  12  var DecBytes = hex.Decode
  13  var DecLen = hex.DecodedLen
  14  
  15  type InvalidByteError = hex.InvalidByteError
  16  
  17  // EncAppend encodes src as hex and appends to dst.
  18  func EncAppend(dst, src []byte) []byte {
  19  	l := len(dst)
  20  	dst = append(dst, []byte{:len(src)*2}...)
  21  	hex.Encode(dst[l:], src)
  22  	return dst
  23  }
  24  
  25  // DecAppend decodes hex src and appends to dst.
  26  func DecAppend(dst, src []byte) ([]byte, error) {
  27  	if len(src) < 2 {
  28  		return dst, errorf.E([]byte("nothing to decode"))
  29  	}
  30  	if dst == nil {
  31  		dst = []byte{}
  32  	}
  33  	l := len(dst)
  34  	dst = append(dst, []byte{:len(src)/2}...)
  35  	_, err := hex.Decode(dst[l:], src)
  36  	return dst, err
  37  }
  38