Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
57 changes: 41 additions & 16 deletions version.go
Original file line number Diff line number Diff line change
Expand Up @@ -258,25 +258,50 @@ func allZero(segs []int64) bool {
return true
}

func comparePart(preSelf string, preOther string) int {
if preSelf == preOther {
return 0
// isNumericIdentifier reports whether s consists entirely of ASCII digits.
// Per the semver spec, numeric identifiers must not have leading zeroes,
// so a longer string always represents a larger value and lexicographic
// order equals numeric order for strings of the same length.
func isNumericIdentifier(s string) bool {
if s == "" {
return false
}
for _, c := range s {
if c < '0' || c > '9' {
return false
}
}
return true
}

var selfInt int64
selfNumeric := true
selfInt, err := strconv.ParseInt(preSelf, 10, 64)
if err != nil {
selfNumeric = false
// compareNumericIdentifiers compares two all-digit strings by value without
// parsing them into a fixed-width integer type, correctly handling values
// larger than math.MaxInt64.
func compareNumericIdentifiers(a, b string) int {
if len(a) != len(b) {
if len(a) > len(b) {
return 1
}
return -1
}
// Same length: lexicographic order equals numeric order.
if a > b {
return 1
}
if a < b {
return -1
}
return 0
}

var otherInt int64
otherNumeric := true
otherInt, err = strconv.ParseInt(preOther, 10, 64)
if err != nil {
otherNumeric = false
func comparePart(preSelf string, preOther string) int {
if preSelf == preOther {
return 0
}

selfNumeric := isNumericIdentifier(preSelf)
otherNumeric := isNumericIdentifier(preOther)

// if a part is empty, we use the other to decide
if preSelf == "" {
if otherNumeric {
Expand All @@ -296,9 +321,9 @@ func comparePart(preSelf string, preOther string) int {
return -1
} else if !selfNumeric && otherNumeric {
return 1
} else if !selfNumeric && !otherNumeric && preSelf > preOther {
return 1
} else if selfInt > otherInt {
} else if selfNumeric && otherNumeric {
return compareNumericIdentifiers(preSelf, preOther)
} else if preSelf > preOther {
return 1
}

Expand Down
6 changes: 6 additions & 0 deletions version_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -390,6 +390,12 @@ func TestComparePreReleases(t *testing.T) {
{"v1.2-beta.1", "v1.2-beta.2", -1},
{"v3.2-alpha.1", "v3.2-alpha", 1},
{"v3.2-rc.1-1-g123", "v3.2-rc.2", 1},
// Numeric identifiers exceeding math.MaxInt64 (9223372036854775807)
{"1.0-9223372036854775808", "1.0-9223372036854775807", 1},
{"1.0-9223372036854775807", "1.0-9223372036854775808", -1},
{"1.0-9223372036854775808", "1.0-9223372036854775808", 0},
{"1.0-99999999999999999999", "1.0-9999999999999999999", 1},
{"1.0-9999999999999999999", "1.0-99999999999999999999", -1},
}

for _, tc := range cases {
Expand Down
Loading