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
|
package main
// See pkgtools/pkg_install/files/lib/dewey.c
import (
"strings"
)
func imax(a, b int) int {
if a > b {
return a
}
return b
}
func icmp(a, b int) int {
if a < b {
return -1
}
if a > b {
return 1
}
return 0
}
func pkgverCmp(left, right string) int {
lv := newVersion(left)
rv := newVersion(right)
m := imax(len(lv.v), len(rv.v))
for i := 0; i < m; i++ {
if c := icmp(lv.Place(i), rv.Place(i)); c != 0 {
return c
}
}
return icmp(lv.nb, rv.nb)
}
type version struct {
v []int
nb int
}
func newVersion(vstr string) *version {
v := new(version)
rest := strings.ToLower(vstr)
for rest != "" {
switch {
case isdigit(rest[0]):
n := 0
i := 0
for i < len(rest) && isdigit(rest[i]) {
n = 10*n + int(rest[i]-'0')
i++
}
rest = rest[i:]
v.Add(n)
case rest[0] == '_' || rest[0] == '.':
v.Add(0)
rest = rest[1:]
case hasPrefix(rest, "alpha"):
v.Add(-3)
rest = rest[5:]
case hasPrefix(rest, "beta"):
v.Add(-2)
rest = rest[4:]
case hasPrefix(rest, "pre"):
v.Add(-1)
rest = rest[3:]
case hasPrefix(rest, "rc"):
v.Add(-1)
rest = rest[2:]
case hasPrefix(rest, "pl"):
v.Add(0)
rest = rest[2:]
case hasPrefix(rest, "nb"):
i := 2
n := 0
for i < len(rest) && isdigit(rest[i]) {
n = 10*n + int(rest[i]-'0')
i++
}
v.nb = n
rest = rest[i:]
case rest[0]-'a' <= 'z'-'a':
v.Add(int(rest[0] - 'a' + 1))
rest = rest[1:]
default:
rest = rest[1:]
}
}
return v
}
func (v *version) Add(i int) {
v.v = append(v.v, i)
}
func isdigit(b byte) bool {
return b-'0' <= 9
}
func (v *version) Place(i int) int {
if i < len(v.v) {
return v.v[i]
}
return 0
}
|