-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathdebversion.go
170 lines (151 loc) · 3.31 KB
/
debversion.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
159
160
161
162
163
164
165
166
167
168
169
170
package main
import (
"strconv"
"strings"
"unicode"
)
// DebVersion contains the componenet of a debian version
type DebVersion struct {
Epoch int // Primarily used for fixing previous errors in versiongin
Version string // The "upstream" version, of the actual packaged applications
Revision string // This is the revision for the packaging of the main upstream version
}
func (d *DebVersion) String() string {
output := ""
if d.Epoch != 0 {
output = strconv.FormatInt(int64(d.Epoch), 10) + ":"
}
output = output + d.Version
if d.Revision != "" {
output = output + "-" + d.Revision
}
return output
}
// ParseDebVersion converts a string to the debian version components
func ParseDebVersion(str string) (version DebVersion, err error) {
epochSplit := strings.SplitN(str, ":", 2)
if len(epochSplit) > 1 {
var epoch int64
epochStr := epochSplit[0]
epoch, err = strconv.ParseInt(epochStr, 10, 64)
if err != nil {
return
}
version.Epoch = int(epoch)
}
verRevStr := epochSplit[len(epochSplit)-1]
verRevSplit := strings.Split(verRevStr, "-")
if len(verRevSplit) > 1 {
version.Version = strings.Join(verRevSplit[:len(verRevSplit)-1], "-")
version.Revision = verRevSplit[len(verRevSplit)-1]
} else {
version.Version = verRevSplit[0]
}
return
}
// DebVersionCompare compares two version, returning 0 if equal, < 0 if a < b or
// > 0 if a > b. Annoyingly 'subtle' code, poached from
// http://anonscm.debian.org/cgit/dpkg/dpkg.git/tree/lib/dpkg/version.c?h=wheezy
func DebVersionCompare(a DebVersion, b DebVersion) int {
if a.Epoch > b.Epoch {
return 1
}
if a.Epoch < b.Epoch {
return -1
}
res := compareComponent(a.Version, b.Version)
if res == 0 {
res = compareComponent(a.Revision, b.Revision)
}
return res
}
func charOrder(c int) int {
/**
* @param c An ASCII character.
*/
switch {
case unicode.IsDigit(rune(c)):
return 0
case unicode.IsLetter(rune(c)):
return c
case c == '~':
return -1
case c != 0:
return c + 256
default:
return 0
}
}
func compareComponent(a string, b string) int {
var i, j int
for {
if i < len(a) || j < len(b) {
firstdiff := 0
for {
if (i < len(a) && !unicode.IsDigit(rune(a[i]))) ||
(j < len(b) && !unicode.IsDigit(rune(b[j]))) {
var ac, bc int
if i < len(a) {
ac = charOrder(int(a[i]))
}
if j < len(b) {
bc = charOrder(int(b[j]))
}
if ac != bc {
return ac - bc
}
i++
j++
} else {
break
}
}
for {
if i < len(a) && rune(a[i]) == '0' {
i++
} else {
break
}
}
for {
if j < len(b) && rune(b[j]) == '0' {
j++
} else {
break
}
}
for {
if (i < len(a) && unicode.IsDigit(rune(a[i]))) &&
(j < len(b) && unicode.IsDigit(rune(b[j]))) {
if firstdiff == 0 {
firstdiff = int(a[i]) - int(b[j])
}
i++
j++
} else {
break
}
}
if i < len(a) && unicode.IsDigit(rune(a[i])) {
return 1
}
if j < len(b) && unicode.IsDigit(rune(b[j])) {
return -1
}
if firstdiff != 0 {
return firstdiff
}
} else {
break
}
}
return 0
}
// MustParseDebVersion panic if debversion doesn't parse, Useful for writing tests
func MustParseDebVersion(str string) (version DebVersion) {
ver, err := ParseDebVersion(str)
if err != nil {
panic(err)
}
return ver
}