-
Notifications
You must be signed in to change notification settings - Fork 0
/
versionstring.go
164 lines (144 loc) · 3.02 KB
/
versionstring.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
package versionstring
import (
"strconv"
"strings"
)
// "1.02.3." => []int{1, 2, 3}
// "1.2..3" => []int{1, 2, 3}
// "1.00.2" => []int{1, 0, 2}
func ParseVersionString(vs string) []int {
tmp := strings.Split(vs, ".")
arr := make([]int, 0, len(tmp))
for _, val := range tmp {
n, err := strconv.Atoi(val)
if err != nil {
continue
}
arr = append(arr, n)
}
return arr
}
// 0 = equal, 1 = greater, -1 = smaller
// "1.2.3", "1.2.3" => 0
// "1.2.3", "1.1" => -1
// "1.2.3", "1.3" => 1
// "1.2.3", "1.2.4" => 1
// "1.2.3", "1.2.2" => -1
// "1.2.3", "1.2.2.2" => -1
// "1.2.3", "1.2.3.0.0" => 0
// "1.2.3", "1.2.3.2" => 1
// "1.2.3", "1.2.3.2.4" => 1
func CompareStrings(lvs, rvs string) int {
left := ParseVersionString(lvs)
right := ParseVersionString(rvs)
return CompareVersions(left, right)
}
// 0 = equal, 1 = greater, -1 = smaller
// "1.2.3", "1.2.3" => 0
// "1.2.3", "1.1" => -1
// "1.2.3", "1.3" => 1
// "1.2.3", "1.2.4" => 1
// "1.2.3", "1.2.2" => -1
// "1.2.3", "1.2.2.2" => -1
// "1.2.3", "1.2.3.0.0" => 0
// "1.2.3", "1.2.3.2" => 1
// "1.2.3", "1.2.3.2.4" => 1
func CompareVersions(left, right []int) int {
// compare the version strings as long as is possible
min := min(len(left), len(right))
for i := 0; i < min; i++ {
if left[i] == right[i] {
continue
}
if left[i] < right[i] {
return 1
}
return -1
}
// both versions are equal as far as we know
// - if they are of equal length => 0
if len(left) == len(right) {
return 0
}
// Worst case
// - version string are of unequal length
// the longest of the version strings has to
// have just one value != 0 to win
// - Could be simplified with pointer
// but it
if len(left) < len(right) {
for i := min; i < len(right); i++ {
if right[i] != 0 {
return 1
}
}
} else {
for i := min; i < len(left); i++ {
if left[i] != 0 {
return -1
}
}
}
// alright, they're equal.
return 0
}
func MaxVersionString(lvs, rvs string) []int {
left := ParseVersionString(lvs)
right := ParseVersionString(rvs)
return MaxVersion(left, right)
}
func MaxVersion(left, right []int) []int {
// compare the version strings as long as is possible
min := min(len(left), len(right))
for i := 0; i < min; i++ {
if left[i] == right[i] {
continue
}
if left[i] < right[i] {
return right
}
return left
}
// both versions are equal as far as we know
// - if they are of equal length => 0
if len(left) == len(right) {
return left
}
// Worst case
// - version string are of unequal length
// the longest of the version strings has to
// have just one value != 0 to win
// - Could be simplified with pointer
// but it
if len(left) < len(right) {
for i := min; i < len(right); i++ {
if right[i] != 0 {
return right
}
}
return left
} else {
for i := min; i < len(left); i++ {
if left[i] != 0 {
return left
}
}
return right
}
// enver happens
return left
}
func max(n, m int) int {
if n < m {
return m
}
// n >= m
return n
}
func min(n, m int) int {
if n < m {
return n
}
// m <= n
return m
}