forked from OpenPrinting/ipp-usb
-
Notifications
You must be signed in to change notification settings - Fork 0
/
uuid.go
57 lines (46 loc) · 1.04 KB
/
uuid.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
/* ipp-usb - HTTP reverse proxy, backed by IPP-over-USB connection to device
*
* Copyright (C) 2020 and up by Alexander Pevzner ([email protected])
* See LICENSE for license terms and conditions
*
* UUID normalizer
*/
package main
import (
"bytes"
)
// UUIDNormalize parses an UUID and then reformats it into
// the standard form (xxxxxxxx-xxxx-xxxx-xxxx-xxxxxxxxxxxx)
//
// If input is not a valid UUID, it returns an empty string
// Many standard formats of UUIDs are recognized
func UUIDNormalize(uuid string) string {
var buf [32]byte
var cnt int
in := bytes.ToLower([]byte(uuid))
if bytes.HasPrefix(in, []byte("urn:")) {
in = in[4:]
}
if bytes.HasPrefix(in, []byte("uuid:")) {
in = in[5:]
}
for len(in) != 0 {
c := in[0]
in = in[1:]
if '0' <= c && c <= '9' || 'a' <= c && c <= 'f' {
if cnt == 32 {
return ""
}
buf[cnt] = c
cnt++
}
}
if cnt != 32 {
return ""
}
return string(buf[0:8]) + "-" +
string(buf[8:12]) + "-" +
string(buf[12:16]) + "-" +
string(buf[16:20]) + "-" +
string(buf[20:32])
}