-
Notifications
You must be signed in to change notification settings - Fork 4
/
Copy pathuuid_base.go
43 lines (36 loc) · 1.11 KB
/
uuid_base.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
package uuid
import "fmt"
type uuidBase [16]byte
// UnmarshalBinary implements encoding.BinaryUnmarshaler.
func (uuid *uuidBase) UnmarshalBinary(data []byte) error {
if len(data) != 16 {
return fmt.Errorf("invalid UUID (got %d bytes)", len(data))
}
copy(uuid[:], data)
return nil
}
// ToString returns UUID as a string in the following format
// xxxxxxxx-xxxx-xxxx-xxxx-xxxxxxxxxxxx
func (u uuidBase) ToString() string {
return fmt.Sprintf("%x-%x-%x-%x-%x", u[0:5], u[5:7], u[7:9], u[9:11], u[11:16])
}
// ToMicrosoftString returns UUID as a string in the following format
// {XXXXXXXX-XXXX-XXXX-XXXX-XXXXXXXXXXXX}
func (u uuidBase) ToMicrosoftString() string {
return fmt.Sprintf("{%X-%X-%X-%X-%X}", u[0:5], u[5:7], u[7:9], u[9:11], u[11:16])
}
func (u uuidBase) ToBitArray() []bool {
ret := make([]bool, 128)
for i := 0; i < 128; i++ {
ret[i] = getBit(u[:], i)
}
return ret
}
// ToBinaryString returns UUID as a string of binary digits grouped in 8
func (u uuidBase) ToBinaryString() string {
var ret string
for _, n := range u {
ret += fmt.Sprintf("%0*b ", 8, n) // prints 00000000 11111101
}
return ret
}