-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathhelper.go
72 lines (65 loc) · 2.26 KB
/
helper.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
package uuidv8
import (
"encoding/hex"
"errors"
"fmt"
)
// Helper function to encode timestamp into the UUID byte array.
func encodeTimestamp(uuid []byte, timestamp uint64, timestampBits int) error {
switch timestampBits {
case TimestampBits32:
uuid[0], uuid[1], uuid[2], uuid[3] = byte(timestamp>>24), byte(timestamp>>16), byte(timestamp>>8), byte(timestamp)
uuid[4], uuid[5] = 0, 0
case TimestampBits48:
uuid[0], uuid[1], uuid[2], uuid[3], uuid[4], uuid[5] = byte(timestamp>>40), byte(timestamp>>32), byte(timestamp>>24), byte(timestamp>>16), byte(timestamp>>8), byte(timestamp)
case TimestampBits60:
uuid[0], uuid[1], uuid[2], uuid[3], uuid[4], uuid[5], uuid[6] = byte(timestamp>>52), byte(timestamp>>44), byte(timestamp>>36), byte(timestamp>>28), byte(timestamp>>20), byte(timestamp>>12), byte(timestamp>>4)
default:
return fmt.Errorf("unsupported timestamp bit size: %d", timestampBits)
}
return nil
}
// Helper function to decode a timestamp from the UUID byte array.
func decodeTimestamp(uuidBytes []byte) uint64 {
return uint64(uuidBytes[0])<<40 | uint64(uuidBytes[1])<<32 | uint64(uuidBytes[2])<<24 |
uint64(uuidBytes[3])<<16 | uint64(uuidBytes[4])<<8 | uint64(uuidBytes[5])
}
// Helper function to parse and sanitize a UUID string.
// Helper function to parse and sanitize a UUID string.
func parseUUID(uuid string) ([]byte, error) {
switch len(uuid) {
case 32:
// Fast path for UUIDs without dashes
return hex.DecodeString(uuid)
case 36:
// Validate dash positions
if uuid[8] != '-' || uuid[13] != '-' || uuid[18] != '-' || uuid[23] != '-' {
return nil, errors.New("invalid UUID format")
}
// Remove dashes while copying characters
result := make([]byte, 32)
j := 0
for i := 0; i < len(uuid); i++ {
if uuid[i] != '-' {
result[j] = uuid[i]
j++
}
}
return hex.DecodeString(string(result))
default:
return nil, errors.New("invalid UUID length")
}
}
// Helper function to check if a UUID is all zeros.
func isAllZeroUUID(uuidBytes []byte) bool {
for _, b := range uuidBytes {
if b != 0 {
return false
}
}
return true
}
// Helper function to format a UUID byte array as a string.
func formatUUID(uuid []byte) string {
return fmt.Sprintf("%08x-%04x-%04x-%04x-%012x", uuid[0:4], uuid[4:6], uuid[6:8], uuid[8:10], uuid[10:16])
}