Skip to content

Commit

Permalink
versionfmt/rpm: handle a tilde correctly
Browse files Browse the repository at this point in the history
  • Loading branch information
knqyf263 committed May 14, 2017
1 parent 8dbd4f3 commit db8a133
Show file tree
Hide file tree
Showing 2 changed files with 12 additions and 14 deletions.
21 changes: 7 additions & 14 deletions ext/versionfmt/rpm/parser.go
Original file line number Diff line number Diff line change
Expand Up @@ -212,25 +212,11 @@ func rpmvercmp(strA, strB string) int {
} else if len(b) > len(a) {
return -1
}

} else if unicode.IsNumber([]rune(b)[0]) {
// a is alpha, b is numeric
return -1
}

// This is the last iteration.
if i == segs-1 {
// If there is a tilde in a segment past the min number of segments, find
// it before we rely on string compare.
lia := strings.LastIndex(strA, "~")
lib := strings.LastIndex(strB, "~")
if lia > lib {
return -1
} else if lia < lib {
return 1
}
}

// string compare
if a < b {
return -1
Expand All @@ -244,6 +230,13 @@ func rpmvercmp(strA, strB string) int {
return 0
}

// If there is a tilde in a segment past the min number of segments, find it.
if len(segsa) > segs && []rune(segsa[segs])[0] == '~' {
return -1
} else if len(segsb) > segs && []rune(segsb[segs])[0] == '~' {
return 1
}

// whoever has the most segments wins
if len(segsa) > len(segsb) {
return 1
Expand Down
5 changes: 5 additions & 0 deletions ext/versionfmt/rpm/parser_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -162,6 +162,11 @@ func TestParseAndCompare(t *testing.T) {
{"1.0~rc1~git123", EQUAL, "1.0~rc1~git123"},
{"1.0~rc1~git123", LESS, "1.0~rc1"},
{"1.0~rc1", GREATER, "1.0~rc1~git123"},
{"1~", GREATER, "1~~"},
{"2~", GREATER, "1"},
{"1.0", GREATER, "1.0-~"},
{"1.0", LESS, "1.0-1~"},
{"~", GREATER, "~~"},
}

var (
Expand Down

0 comments on commit db8a133

Please sign in to comment.