This repository has been archived by the owner on Jan 28, 2024. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathversion.go
53 lines (44 loc) · 1.76 KB
/
version.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
package cl30
import (
"fmt"
"math"
)
// Version represents a major.minor.patch version combination, encoded in a 32-bit unsigned integer value.
type Version uint32
const (
// VersionMin is the lowest value that Version can represent. It has all components set to zero.
VersionMin Version = 0
// VersionMax is the highest value that Version can represent.
// It has all components set to their maximally possible value.
VersionMax Version = math.MaxUint32
versionMajorBits = 10
versionMinorBits = 10
versionPatchBits = 12
versionMajorMask = (1 << versionMajorBits) - 1
versionMinorMask = (1 << versionMinorBits) - 1
versionPatchMask = (1 << versionPatchBits) - 1
)
// VersionOf returns a Version that has the three provided components encoded.
// No particular limits-checking is performed, the provided values are cast and shifted into the final field.
func VersionOf(major, minor, patch int) Version {
return Version(
((uint32(major) & versionMajorMask) << (versionMinorBits + versionPatchBits)) |
((uint32(minor) & versionMinorMask) << versionPatchBits) | (uint32(patch) & versionPatchMask))
}
// String returns a common representation of <major> <.> <minor> <.> <patch> of the version, with the components
// in decimal format.
func (ver Version) String() string {
return fmt.Sprintf("%d.%d.%d", ver.Major(), ver.Minor(), ver.Patch())
}
// Major returns the major component value.
func (ver Version) Major() int {
return int((uint32(ver) >> (versionMinorBits + versionPatchBits)) & versionMajorMask)
}
// Minor returns the minor component value.
func (ver Version) Minor() int {
return int((uint32(ver) >> versionPatchBits) & versionMinorMask)
}
// Patch returns the patch component value.
func (ver Version) Patch() int {
return int(uint32(ver) & versionPatchMask)
}