You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

124 lines
3.1 KiB

2 years ago
  1. package util
  2. import (
  3. "encoding/hex"
  4. "fmt"
  5. "regexp"
  6. "sort"
  7. "strings"
  8. uuid "github.com/satori/go.uuid"
  9. )
  10. var GUUID *UUIDFactory = new(UUIDFactory)
  11. var EmptyUUID = "00000000-0000-0000-0000-000000000000"
  12. type UUIDFactory struct {
  13. uid uuid.UUID
  14. }
  15. func (nu *UUIDFactory) NewV1() *UUIDFactory {
  16. uid := uuid.NewV1()
  17. nu.uid = uid
  18. return nu
  19. }
  20. func (nu *UUIDFactory) Sort() []byte {
  21. return UUIDToBytes(nu.String())
  22. }
  23. func (nu *UUIDFactory) String() string {
  24. return Sort(nu.uid.String()) //strings.Replace(Sort(nu.uid.String()), "-", "", -1)
  25. }
  26. func BytesToUUID(b []byte) string {
  27. hs := hex.EncodeToString(b)
  28. return hs[0:8] + "-" + hs[8:12] + "-" + hs[12:16] + "-" + hs[16:20] + "-" + hs[20:32]
  29. }
  30. func UUIDToBytes(s string) []byte {
  31. if s == "" {
  32. fmt.Println("空uid")
  33. return []byte("00000000-0000-0000-0000-000000000000")
  34. }
  35. if !IsUUID(s) {
  36. fmt.Errorf("非法UUID:%s", s)
  37. }
  38. if strings.Contains(s, "-") {
  39. s = strings.Replace(s, "-", "", -1)
  40. }
  41. b, err := hex.DecodeString(s)
  42. if err != nil {
  43. //CheckError(err)
  44. }
  45. return b
  46. }
  47. //校验是否为合法的UUID,合法UUID格式为16进制字符和“-”组成,类似如下的格式:
  48. //(1)11e90a62-f2fa-07ce-9e60-0242ac110007
  49. //(2)11E90A62-F2fa-07ce-9e60-0242ac11000A
  50. //(3)11e90a62f2fa07ce9e600242ac110007
  51. //(4)11E90A62F2fa07ce9e600242ac11000A
  52. //以下格式字符串为非法UUID:
  53. //(1)11e90a62-f2fa-07ce-9e60-0242ac11000x(包含16进制字符之外的字符“x”,非法)
  54. //(2)11e90a62-f2fa-07ce-9e60-0242ac11000X(包含16进制字符之外的字符“X”,非法)
  55. //(3)11e90a62f2fa07ce9e600242ac11000x(包含16进制字符之外的字符“x”,非法)
  56. //(4)11e90a62f2fa07ce9e600242ac11000X(包含16进制字符之外的字符“X”,非法)
  57. //(5)11e90a62f2fa07ce9e600242ac11000(不包含“-”27位,非法)
  58. //(6)11e90a62-f2fa-07ce-9e60-0242ac11000(31位,非法)
  59. //(7)11e90a62-f2fa-07ce-9e600242ac110007(缺少“-”的31位字符串,非法)
  60. func IsUUID(s string) bool {
  61. const pattern = `[0-9a-fA-F]{8}(-[0-9a-fA-F]{4}){3}-[0-9a-fA-F]{12}|[0-9a-fA-F]{32}`
  62. if b, err := regexp.MatchString(pattern, s); b && err == nil {
  63. return true
  64. }
  65. return false
  66. }
  67. func Sort(uid string) string {
  68. us := strings.Split(uid, "-")
  69. var res []string
  70. res = append(res, us[1]+us[2])
  71. res = append(res, string([]rune(us[0])[4:8]))
  72. res = append(res, string([]rune(us[0])[:4]))
  73. res = append(res, us[3])
  74. res = append(res, us[4])
  75. return strings.Join(res, "-")
  76. }
  77. func IsEmptyUUID(uid string) bool {
  78. return len(uid) == 0 ||
  79. len(strings.TrimSpace(uid)) == 0 ||
  80. uid == "00000000-0000-0000-0000-000000000000" ||
  81. uid == "00000000000000000000000000000000" ||
  82. uid == "30780000-0000-0000-0000-000000000000"
  83. }
  84. func IsValidUUID(uid string) bool {
  85. return !IsEmptyUUID(uid) && IsUUID(uid)
  86. }
  87. // 数组切片去重
  88. func SliceRemoveDuplicates(slice []string) []string {
  89. sort.Strings(slice)
  90. i := 0
  91. var j int
  92. for {
  93. if i >= len(slice)-1 {
  94. break
  95. }
  96. for j = i + 1; j < len(slice) && slice[i] == slice[j]; j++ {
  97. }
  98. slice = append(slice[:i+1], slice[j:]...)
  99. i++
  100. }
  101. return slice
  102. }