gtsocial-umbx

Unnamed repository; edit this file 'description' to name the repository.
Log | Files | Refs | README | LICENSE

hex.go (709B)


      1 package internal
      2 
      3 import (
      4 	fasthex "github.com/tmthrgd/go-hex"
      5 )
      6 
      7 type HexEncoder struct {
      8 	b       []byte
      9 	written bool
     10 }
     11 
     12 func NewHexEncoder(b []byte) *HexEncoder {
     13 	return &HexEncoder{
     14 		b: b,
     15 	}
     16 }
     17 
     18 func (enc *HexEncoder) Bytes() []byte {
     19 	return enc.b
     20 }
     21 
     22 func (enc *HexEncoder) Write(b []byte) (int, error) {
     23 	if !enc.written {
     24 		enc.b = append(enc.b, '\'')
     25 		enc.b = append(enc.b, `\x`...)
     26 		enc.written = true
     27 	}
     28 
     29 	i := len(enc.b)
     30 	enc.b = append(enc.b, make([]byte, fasthex.EncodedLen(len(b)))...)
     31 	fasthex.Encode(enc.b[i:], b)
     32 
     33 	return len(b), nil
     34 }
     35 
     36 func (enc *HexEncoder) Close() error {
     37 	if enc.written {
     38 		enc.b = append(enc.b, '\'')
     39 	} else {
     40 		enc.b = append(enc.b, "NULL"...)
     41 	}
     42 	return nil
     43 }