forked from peterfromthehill/nexus-cli
-
Notifications
You must be signed in to change notification settings - Fork 0
/
versionSorter.go
executable file
·202 lines (171 loc) · 3.65 KB
/
versionSorter.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
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
package main
import (
"strings"
"unicode"
"math"
"sort"
)
func s(s1, s2 string) bool {
if Filevercmp(s1,s2) < 0 {
return true
} else {
return false
}
}
// Sort sort something
func Sort(strs []string) {
strSort := &strSorter{
strs: strs,
cmp: s,
}
sort.Sort(strSort)
}
type strSorter struct {
strs []string
cmp func(str1, str2 string) bool
}
func (s *strSorter) Len() int { return len(s.strs) }
func (s *strSorter) Swap(i, j int) { s.strs[i], s.strs[j] = s.strs[j], s.strs[i] }
func (s *strSorter) Less(i, j int) bool { return s.cmp(s.strs[i], s.strs[j]) }
func cIsdigit(c byte) bool {
return unicode.IsDigit(int32(c));
}
func cIsalpha(c byte) bool {
return unicode.IsLetter(int32(c))
}
func cIsalnum(c byte) bool {
return cIsalpha(c) || cIsdigit(c);
}
func strncmp(s1 string, s2 string, l int) int {
len1 := math.Min(float64(l), float64(len(s1)))
len2 := math.Min(float64(l), float64(len(s2)))
return strings.Compare(s1[:int(len1)], s2[:int(len2)]);
}
func matchSuffix(str string) string {
match := ""
readAlpha := false
for ; len(str) > 0 ; {
if readAlpha {
readAlpha = false
if !cIsalpha(str[0]) && '~' != str[0] {
match = ""
}
} else if '.' == str[0] {
readAlpha = true
if len(match) == 0 {
match = str
}
} else if !cIsalnum(str[0]) && '~' != str[0] {
match = ""
}
str = str[1:len(str)]
}
return match
}
func order(c byte) int {
if cIsdigit(c) {
return 0
} else if cIsalpha(c) {
return int(c)
} else if c == '~' {
return -1
} else {
return int(c) + 1 + unicode.MaxRune
}
}
func verrevcmp(s1 string, s2 string) int {
s1Pos := 0
s2Pos := 0
for ; s1Pos < len(s1) || s2Pos < len(s2); {
firstDiff := 0
for ; (s1Pos < len(s1) && !cIsdigit(s1[s1Pos])) || (s2Pos < len(s2) && !cIsdigit(s2[s2Pos])); {
s1C := 0
if s1Pos < len(s1) {
s1C = order(s1[s1Pos])
}
s2C := 0
if s2Pos < len(s2) {
s2C = order(s2[s2Pos])
}
if s1C != s2C {
return s1C - s2C
}
s1Pos++
s2Pos++
}
for ; s1Pos < len(s1) && s1[s1Pos] == '0'; {
s1Pos++
}
for ; s2Pos < len(s2) && s2[s2Pos] == '0'; {
s2Pos++
}
for ; (s1Pos < len(s1) && s2Pos < len(s2) && cIsdigit(s1[s1Pos]) && cIsdigit(s2[s2Pos])); {
if firstDiff == 0 {
firstDiff = int(s1[s1Pos]) - int(s2[s2Pos])
}
s1Pos++
s2Pos++
}
if s1Pos < len(s1) && cIsdigit(s1[s1Pos]) {
return 1
}
if s2Pos < len(s2) && cIsdigit(s2[s2Pos]) {
return -1
}
if firstDiff != 0 {
return firstDiff
}
}
return 0
}
// Filevercmp compares to strings
func Filevercmp(s1 string, s2 string) int {
s1Pos := 0
s2Pos := 0
result := 0
simpleCmp := strings.Compare (s1, s2);
if(simpleCmp == 0) {
return 0
}
if (s1 == "") {
return -1
}
if (s2 == "") {
return 1
}
if (s1 == ".") {
return -1
}
if (s2 == ".") {
return 1
}
if (s1 == "..") {
return -1
}
if (s2 == "..") {
return 1
}
if (s1[s1Pos] == '.' && s2[s2Pos] != '.') {
return -1
}
if (s1[s1Pos] != '.' && s2[s2Pos] == '.') {
return 1
}
if (s1[s1Pos] == '.' && s2[s2Pos] == '.') {
s1 = s1[1:]
s2 = s2[1:]
}
s1Suffix := matchSuffix(s1)
s2Suffix := matchSuffix(s2)
s1Len := len(s1) - len(s1Suffix)
s2Len := len(s2) - len(s2Suffix)
if ((len(s1Suffix) > 0 || len(s2Suffix) > 0) && (s1Len == s2Len) && 0 == strings.Compare(s1[:s1Len], s2[:s1Len])) {
s1Len = len(s1)
s2Len = len(s2)
}
result = verrevcmp(s1[:s1Len], s2[:s2Len])
if result == 0 {
return simpleCmp
}
return result
}