-
Notifications
You must be signed in to change notification settings - Fork 7.1k
/
version.go
80 lines (59 loc) · 1.35 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
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
// Copyright (c) 2015-present Mattermost, Inc. All Rights Reserved.
// See LICENSE.txt for license information.
package version
import (
"regexp"
"strconv"
"strings"
)
type V string
func (v V) GreaterThanOrEqualTo(other V) bool {
return !v.LessThan(other)
}
func (v V) LessThan(other V) bool {
leftParts, leftCount := split(v)
rightParts, rightCount := split(other)
var length int
if leftCount < rightCount {
length = rightCount
} else {
length = leftCount
}
for i := 0; i < length; i++ {
var left, right string
if i < leftCount {
left = leftParts[i]
}
if i < rightCount {
right = rightParts[i]
}
if left == right {
continue
}
leftInt := parseInt(left)
rightInt := parseInt(right)
isNumericalComparison := leftInt != nil && rightInt != nil
if isNumericalComparison {
return *leftInt < *rightInt
}
return left < right
}
return false
}
func split(v V) ([]string, int) {
var chunks []string
for _, part := range strings.Split(string(v), ".") {
chunks = append(chunks, splitNumericalChunks(part)...)
}
return chunks, len(chunks)
}
var numericalOrAlphaRE = regexp.MustCompile(`(\d+|\D+)`)
func splitNumericalChunks(s string) []string {
return numericalOrAlphaRE.FindAllString(s, -1)
}
func parseInt(s string) *int64 {
if n, err := strconv.ParseInt(s, 10, 64); err == nil {
return &n
}
return nil
}