-
Notifications
You must be signed in to change notification settings - Fork 0
/
semver.go
158 lines (141 loc) · 3.54 KB
/
semver.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
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
package semver
import (
"errors"
"sort"
"strconv"
"strings"
)
var (
// ErrEmpty means that given value is empty.
ErrEmpty = errors.New("semver: empty string")
// ErrEmptyElement means that some elements of version is empty.
ErrEmptyElement = errors.New("semver: empty element")
// ErrInvalidFormat means that given value to parse isn't in semver format.
ErrInvalidFormat = errors.New("semver: invalid format")
// ErrLeadingZeroes means that some elements of version has leading zeroes.
ErrLeadingZeroes = errors.New("semver: leading zeros")
// ErrInvalidCharacter means that there is some unsupported characters in version specification.
ErrInvalidCharacter = errors.New("semver: invalid character")
)
const (
major int = iota
minor
patch
)
const (
numbers = "0123456789"
)
var zero = Version{}
type Versions []Version
func (vs Versions) Len() int { return len(vs) }
func (vs Versions) Less(i, j int) bool { return vs[i].Lt(vs[j]) }
func (vs Versions) Swap(i, j int) { vs[i], vs[j] = vs[j], vs[i] }
func (vs Versions) Sort() { sort.Sort(vs) }
func (vs Versions) Ascending() { vs.Sort() }
func Sort(vs ...Version) Versions { sorted := Versions(vs); sorted.Sort(); return sorted }
type Version struct {
Major uint64
Minor uint64
Patch uint64
}
func (v Version) String() string {
b := make([]byte, 0, 6)
b = append(b, 'v')
b = strconv.AppendUint(b, v.Major, 10)
b = append(b, '.')
b = strconv.AppendUint(b, v.Minor, 10)
b = append(b, '.')
b = strconv.AppendUint(b, v.Patch, 10)
return string(b)
}
const (
lt = iota - 1
eq
gt
)
func (v Version) Eq(to Version) bool { return v.Compare(to) == eq }
func (v Version) Lt(to Version) bool { return v.Compare(to) == lt }
func (v Version) Lte(to Version) bool { return v.Compare(to) <= eq }
func (v Version) Gt(to Version) bool { return v.Compare(to) == gt }
func (v Version) Gte(to Version) bool { return v.Compare(to) >= eq }
func (v Version) Compare(to Version) int {
switch {
case v.Major > to.Major:
return gt
case v.Major < to.Major:
return lt
case v.Minor > to.Minor:
return gt
case v.Minor < to.Minor:
return lt
case v.Patch > to.Patch:
return gt
case v.Patch < to.Patch:
return lt
default:
return eq
}
}
func New(s string) (Version, error) { return Parse(s) }
func V(s string) Version { return Must(s) }
func Must(s string) Version {
v, err := Parse(s)
if err != nil {
panic(err)
}
return v
}
func Parse(s string) (v Version, err error) {
if len(s) == 0 {
return zero, ErrEmpty
}
parts := strings.Split(strings.TrimPrefix(s, "v"), ".")
if len(parts) != 3 {
return zero, ErrInvalidFormat
}
if empty(parts...) {
return zero, ErrEmptyElement
}
if !containsOnly(numbers, parts...) {
return zero, ErrInvalidCharacter
}
if leadingZeroes(parts...) {
return zero, ErrLeadingZeroes
}
if v.Major, err = strconv.ParseUint(parts[major], 10, 64); err != nil {
return zero, err
}
if v.Minor, err = strconv.ParseUint(parts[minor], 10, 64); err != nil {
return zero, err
}
if v.Patch, err = strconv.ParseUint(parts[patch], 10, 64); err != nil {
return zero, err
}
return v, nil
}
func empty(vs ...string) bool {
for _, v := range vs {
if v == "" {
return true
}
}
return false
}
func leadingZeroes(vs ...string) bool {
for _, v := range vs {
if len(v) > 1 && v[0] == '0' {
return true
}
}
return false
}
func containsOnly(set string, vs ...string) bool {
for _, v := range vs {
if strings.IndexFunc(v, func(r rune) bool {
return !strings.ContainsRune(set, r)
}) > -1 {
return false
}
}
return true
}