Skip to content

Commit f2f2134

Browse files
authored
Merge pull request #395 from knqyf263/handle_tilde
versionfmt/rpm: handle a tilde correctly
2 parents 8816aed + db8a133 commit f2f2134

File tree

2 files changed

+12
-14
lines changed

2 files changed

+12
-14
lines changed

ext/versionfmt/rpm/parser.go

Lines changed: 7 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -212,25 +212,11 @@ func rpmvercmp(strA, strB string) int {
212212
} else if len(b) > len(a) {
213213
return -1
214214
}
215-
216215
} else if unicode.IsNumber([]rune(b)[0]) {
217216
// a is alpha, b is numeric
218217
return -1
219218
}
220219

221-
// This is the last iteration.
222-
if i == segs-1 {
223-
// If there is a tilde in a segment past the min number of segments, find
224-
// it before we rely on string compare.
225-
lia := strings.LastIndex(strA, "~")
226-
lib := strings.LastIndex(strB, "~")
227-
if lia > lib {
228-
return -1
229-
} else if lia < lib {
230-
return 1
231-
}
232-
}
233-
234220
// string compare
235221
if a < b {
236222
return -1
@@ -244,6 +230,13 @@ func rpmvercmp(strA, strB string) int {
244230
return 0
245231
}
246232

233+
// If there is a tilde in a segment past the min number of segments, find it.
234+
if len(segsa) > segs && []rune(segsa[segs])[0] == '~' {
235+
return -1
236+
} else if len(segsb) > segs && []rune(segsb[segs])[0] == '~' {
237+
return 1
238+
}
239+
247240
// whoever has the most segments wins
248241
if len(segsa) > len(segsb) {
249242
return 1

ext/versionfmt/rpm/parser_test.go

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -162,6 +162,11 @@ func TestParseAndCompare(t *testing.T) {
162162
{"1.0~rc1~git123", EQUAL, "1.0~rc1~git123"},
163163
{"1.0~rc1~git123", LESS, "1.0~rc1"},
164164
{"1.0~rc1", GREATER, "1.0~rc1~git123"},
165+
{"1~", GREATER, "1~~"},
166+
{"2~", GREATER, "1"},
167+
{"1.0", GREATER, "1.0-~"},
168+
{"1.0", LESS, "1.0-1~"},
169+
{"~", GREATER, "~~"},
165170
}
166171

167172
var (

0 commit comments

Comments
 (0)