Repository navigation
Expand file tree
/
Copy pathbazel.go
More file actions
130 lines (117 loc) · 2.73 KB
/
Copy pathbazel.go
File metadata and controls
130 lines (117 loc) · 2.73 KB
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
package vers
import (
"regexp"
"strconv"
"strings"
)
var bazelVersionRegex = regexp.MustCompile(`^([A-Za-z0-9.]+)(-([A-Za-z0-9.-]+))?(\+([A-Za-z0-9.-]+))?$`)
type bazelVersion struct {
release []bazelIdentifier
prerelease []bazelIdentifier
normalized string
empty bool
}
type bazelIdentifier struct {
text string
number uint64
numeric bool
}
func parseBazelVersion(version string) (bazelVersion, bool) {
if version == "" {
return bazelVersion{empty: true}, true
}
match := bazelVersionRegex.FindStringSubmatch(version)
if match == nil {
return bazelVersion{}, false
}
release, ok := parseBazelIdentifiers(match[1], true)
if !ok {
return bazelVersion{}, false
}
prerelease, ok := parseBazelIdentifiers(match[3], true)
if !ok {
return bazelVersion{}, false
}
if _, ok := parseBazelIdentifiers(match[5], false); !ok {
return bazelVersion{}, false
}
normalized := match[1]
if match[3] != "" {
normalized += "-" + match[3]
}
return bazelVersion{release: release, prerelease: prerelease, normalized: normalized}, true
}
func parseBazelIdentifiers(value string, numericLimit bool) ([]bazelIdentifier, bool) {
if value == "" {
return nil, true
}
parts := strings.Split(value, ".")
identifiers := make([]bazelIdentifier, 0, len(parts))
for _, part := range parts {
if part == "" {
return nil, false
}
identifier := bazelIdentifier{text: part, numeric: isDigits(part)}
if identifier.numeric && numericLimit {
number, err := strconv.ParseUint(part, 10, 64)
if err != nil {
return nil, false
}
identifier.number = number
}
identifiers = append(identifiers, identifier)
}
return identifiers, true
}
func compareBazel(a, b string) int {
left, leftOK := parseBazelVersion(a)
right, rightOK := parseBazelVersion(b)
if !leftOK || !rightOK {
return cmpString(a, b)
}
if left.empty {
if right.empty {
return 0
}
return 1
}
if right.empty {
return -1
}
if result := compareBazelIdentifiers(left.release, right.release); result != 0 {
return result
}
if len(left.prerelease) == 0 && len(right.prerelease) != 0 {
return 1
}
if len(left.prerelease) != 0 && len(right.prerelease) == 0 {
return -1
}
return compareBazelIdentifiers(left.prerelease, right.prerelease)
}
func compareBazelIdentifiers(a, b []bazelIdentifier) int {
limit := min(len(a), len(b))
for i := 0; i < limit; i++ {
if result := compareBazelIdentifier(a[i], b[i]); result != 0 {
return result
}
}
return cmpInt(len(a), len(b))
}
func compareBazelIdentifier(a, b bazelIdentifier) int {
if a.numeric != b.numeric {
if a.numeric {
return -1
}
return 1
}
if a.numeric {
if a.number < b.number {
return -1
}
if a.number > b.number {
return 1
}
}
return cmpString(a.text, b.text)
}