vid.go 575 B

1234567891011121314151617181920212223242526272829303132333435363738
  1. package core
  2. import (
  3. "encoding/hex"
  4. "fmt"
  5. )
  6. type VID [16]byte
  7. var byteGroups = []int{8, 4, 4, 4, 12}
  8. // TODO: leverage a full functional UUID library
  9. func UUIDToVID(uuid string) (v VID, err error) {
  10. text := []byte(uuid)
  11. if len(text) < 32 {
  12. err = fmt.Errorf("uuid: invalid UUID string: %s", text)
  13. return
  14. }
  15. b := v[:]
  16. for _, byteGroup := range byteGroups {
  17. if text[0] == '-' {
  18. text = text[1:]
  19. }
  20. _, err = hex.Decode(b[:byteGroup/2], text[:byteGroup])
  21. if err != nil {
  22. return
  23. }
  24. text = text[byteGroup:]
  25. b = b[byteGroup/2:]
  26. }
  27. return
  28. }