| 1 |
package uuid |
| 2 |
|
| 3 |
import ( |
| 4 |
"errors" |
| 5 |
"strings" |
| 6 |
) |
| 7 |
|
| 8 |
// Format represents different styles a UUID can be printed in constants |
| 9 |
// represent a pattern used by the package with which to print a UUID. |
| 10 |
type Format string |
| 11 |
|
| 12 |
const ( |
| 13 |
FormatHex Format = "%x%x%x%x%x" |
| 14 |
FormatHexCurly Format = "{%x%x%x%x%x}" |
| 15 |
FormatHexBracket Format = "(%x%x%x%x%x)" |
| 16 |
|
| 17 |
// This is the canonical format. |
| 18 |
FormatCanonical Format = "%x-%x-%x-%x-%x" |
| 19 |
|
| 20 |
FormatCanonicalCurly Format = "{%x-%x-%x-%x-%x}" |
| 21 |
FormatCanonicalBracket Format = "(%x-%x-%x-%x-%x)" |
| 22 |
FormatUrn Format = "urn:uuid:" + FormatCanonical |
| 23 |
) |
| 24 |
|
| 25 |
var printFormat Format = FormatCanonical |
| 26 |
|
| 27 |
var defaultFormats map[Format]bool = make(map[Format]bool) |
| 28 |
|
| 29 |
func init() { |
| 30 |
defaultFormats[FormatHex] = true |
| 31 |
defaultFormats[FormatHexCurly] = true |
| 32 |
defaultFormats[FormatHexBracket] = true |
| 33 |
defaultFormats[FormatCanonical] = true |
| 34 |
defaultFormats[FormatCanonicalCurly] = true |
| 35 |
defaultFormats[FormatCanonicalBracket] = true |
| 36 |
defaultFormats[FormatUrn] = true |
| 37 |
} |
| 38 |
|
| 39 |
// SwitchFormat switches the default printing format for ALL UUIDs. |
| 40 |
// |
| 41 |
// The default is the canonical uuid.Format.FormatCanonical which has been |
| 42 |
// optimised for use with this package. It is twice as fast compared to other |
| 43 |
// formats; supplied or given. However, the benchmark for non default formats |
| 44 |
// is still very quick and quite usable. The package has moved away from using |
| 45 |
// fmt.Sprintf which was up to 5 times slower in comparison to custom formats |
| 46 |
// and 10 times slower in comparison to the canonical format. |
| 47 |
// |
| 48 |
// A valid format will have 5 groups of [%x|%X] or follow the pattern, |
| 49 |
// *%[xX]*%[xX]*%[xX]*%[xX]*%[xX]*. If the supplied format does not meet this |
| 50 |
// standard the function will panic. Note any extra uses of [%] outside of the |
| 51 |
// [%x|%X] will also cause a panic. |
| 52 |
// Constant uuid.Formats have been provided for the most likely formats. |
| 53 |
func SwitchFormat(pFormat Format) { |
| 54 |
checkFormat(pFormat) |
| 55 |
printFormat = pFormat |
| 56 |
} |
| 57 |
|
| 58 |
// SwitchFormatToUpper is a convenience function to set the Format to uppercase |
| 59 |
// versions of the given constants. |
| 60 |
func SwitchFormatToUpper(pFormat Format) { |
| 61 |
SwitchFormat(Format(strings.ToUpper(string(pFormat)))) |
| 62 |
} |
| 63 |
|
| 64 |
// Formatter will return a string representation of the given UUID. |
| 65 |
// |
| 66 |
// Use this for one time formatting when setting the default using |
| 67 |
// uuid.SwitchFormat would be overkill. |
| 68 |
// |
| 69 |
// A valid format will have 5 groups of [%x|%X] or follow the pattern, |
| 70 |
// *%[xX]*%[xX]*%[xX]*%[xX]*%[xX]*. If the supplied format does not meet this |
| 71 |
// standard the function will panic. Note any extra uses of [%] outside of the |
| 72 |
// [%x|%X] will also cause a panic. |
| 73 |
func Formatter(pId UUID, pFormat Format) string { |
| 74 |
checkFormat(pFormat) |
| 75 |
return formatUuid(pId.Bytes(), pFormat) |
| 76 |
} |
| 77 |
|
| 78 |
func checkFormat(pFormat Format) { |
| 79 |
if defaultFormats[pFormat] { |
| 80 |
return |
| 81 |
} |
| 82 |
s := strings.ToLower(string(pFormat)) |
| 83 |
if strings.Count(s, "%x") != 5 { |
| 84 |
panic(errors.New("uuid.Format: invalid format")) |
| 85 |
} |
| 86 |
s = strings.Replace(s, "%x", "", -1) |
| 87 |
if strings.Count(s, "%") > 0 { |
| 88 |
panic(errors.New("uuid.Format: invalid format")) |
| 89 |
} |
| 90 |
} |
| 91 |
|
| 92 |
const ( |
| 93 |
hexTable = "0123456789abcdef" |
| 94 |
hexUpperTable = "0123456789ABCDEF" |
| 95 |
|
| 96 |
canonicalLength = length*2 + 4 |
| 97 |
formatArgCount = 10 |
| 98 |
uuidStringBufferSize = length*2 - formatArgCount |
| 99 |
) |
| 100 |
|
| 101 |
var groups = [...]int{4, 2, 2, 2, 6} |
| 102 |
|
| 103 |
func formatUuid(pSrc []byte, pFormat Format) string { |
| 104 |
if pFormat == FormatCanonical { |
| 105 |
return string(formatCanonical(pSrc)) |
| 106 |
} |
| 107 |
return string(format(pSrc, string(pFormat))) |
| 108 |
} |
| 109 |
|
| 110 |
func format(pSrc []byte, pFormat string) []byte { |
| 111 |
end := len(pFormat) |
| 112 |
buf := make([]byte, end+uuidStringBufferSize) |
| 113 |
|
| 114 |
var s, ls, b, e, p int |
| 115 |
var u bool |
| 116 |
for _, v := range groups { |
| 117 |
ls = s |
| 118 |
for ; s < end && pFormat[s] != '%'; s++ { |
| 119 |
} |
| 120 |
copy(buf[p:], pFormat[ls:s]) |
| 121 |
p += s - ls |
| 122 |
s++ |
| 123 |
u = pFormat[s] == 'X' |
| 124 |
s++ |
| 125 |
e = b + v |
| 126 |
for i, t := range pSrc[b:e] { |
| 127 |
j := p + i + i |
| 128 |
table := hexTable |
| 129 |
if u { |
| 130 |
table = hexUpperTable |
| 131 |
} |
| 132 |
buf[j] = table[t>>4] |
| 133 |
buf[j+1] = table[t&0x0f] |
| 134 |
} |
| 135 |
b = e |
| 136 |
p += v + v |
| 137 |
} |
| 138 |
ls = s |
| 139 |
for ; s < end && pFormat[s] != '%'; s++ { |
| 140 |
} |
| 141 |
copy(buf[p:], pFormat[ls:s]) |
| 142 |
p += s - ls |
| 143 |
return buf |
| 144 |
} |
| 145 |
|
| 146 |
func formatCanonical(pSrc []byte) []byte { |
| 147 |
buf := make([]byte, canonicalLength) |
| 148 |
var b, p, e int |
| 149 |
for h, v := range groups { |
| 150 |
e = b + v |
| 151 |
for i, t := range pSrc[b:e] { |
| 152 |
j := p + i + i |
| 153 |
buf[j] = hexTable[t>>4] |
| 154 |
buf[j+1] = hexTable[t&0x0f] |
| 155 |
} |
| 156 |
b = e |
| 157 |
p += v + v |
| 158 |
if h < 4 { |
| 159 |
buf[p] = '-' |
| 160 |
p += 1 |
| 161 |
} |
| 162 |
} |
| 163 |
return buf |
| 164 |
} |