uuid.go 555 B

1234567891011121314151617181920212223
  1. package pq
  2. import (
  3. "encoding/hex"
  4. "fmt"
  5. )
  6. // decodeUUIDBinary interprets the binary format of a uuid, returning it in text format.
  7. func decodeUUIDBinary(src []byte) ([]byte, error) {
  8. if len(src) != 16 {
  9. return nil, fmt.Errorf("pq: unable to decode uuid; bad length: %d", len(src))
  10. }
  11. dst := make([]byte, 36)
  12. dst[8], dst[13], dst[18], dst[23] = '-', '-', '-', '-'
  13. hex.Encode(dst[0:], src[0:4])
  14. hex.Encode(dst[9:], src[4:6])
  15. hex.Encode(dst[14:], src[6:8])
  16. hex.Encode(dst[19:], src[8:10])
  17. hex.Encode(dst[24:], src[10:16])
  18. return dst, nil
  19. }