From db8a133d2130e8a6b9f598c4bd859b06a5a0a8af Mon Sep 17 00:00:00 2001 From: knqyf263 Date: Sun, 14 May 2017 19:11:16 +0900 Subject: [PATCH] versionfmt/rpm: handle a tilde correctly --- ext/versionfmt/rpm/parser.go | 21 +++++++-------------- ext/versionfmt/rpm/parser_test.go | 5 +++++ 2 files changed, 12 insertions(+), 14 deletions(-) diff --git a/ext/versionfmt/rpm/parser.go b/ext/versionfmt/rpm/parser.go index a05f14d6..34fbb9b9 100644 --- a/ext/versionfmt/rpm/parser.go +++ b/ext/versionfmt/rpm/parser.go @@ -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 @@ -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 diff --git a/ext/versionfmt/rpm/parser_test.go b/ext/versionfmt/rpm/parser_test.go index fe316bfc..73c6df89 100644 --- a/ext/versionfmt/rpm/parser_test.go +++ b/ext/versionfmt/rpm/parser_test.go @@ -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 (