diff --git a/misc.go b/misc.go index 5ee41a1c..650d1376 100644 --- a/misc.go +++ b/misc.go @@ -11,7 +11,6 @@ import ( "os" "path/filepath" "regexp" - "strconv" "strings" "unicode" @@ -393,14 +392,16 @@ func naturalCmp(s1, s2 string) int { tok2 := s2[lo2:hi2] if isDigit1 && isDigit2 { - num1, err1 := strconv.Atoi(tok1) - num2, err2 := strconv.Atoi(tok2) - if err1 == nil && err2 == nil { - if num1 != num2 { - return cmp.Compare(num1, num2) - } else if len(tok1) != len(tok2) { - return cmp.Compare(len(tok1), len(tok2)) - } + // compare the digits directly instead of parsing them, so that + // numbers too large for an int are still ordered by value + num1 := strings.TrimLeft(tok1, "0") + num2 := strings.TrimLeft(tok2, "0") + if len(num1) != len(num2) { + return cmp.Compare(len(num1), len(num2)) + } else if num1 != num2 { + return cmp.Compare(num1, num2) + } else if len(tok1) != len(tok2) { + return cmp.Compare(len(tok1), len(tok2)) } } diff --git a/misc_test.go b/misc_test.go index 5474fbae..7df1c8ae 100644 --- a/misc_test.go +++ b/misc_test.go @@ -399,6 +399,12 @@ func TestNaturalCmp(t *testing.T) { {"foo0bar", "foo00bar", -1}, {"foo1", "foo01", -1}, {"foo1bar", "foo01bar", -1}, + {"9", "10000000000000000000", -1}, + {"foo9", "foo10000000000000000000", -1}, + {"9223372036854775807", "9223372036854775808", -1}, + {"99999999999999999999", "100000000000000000000", -1}, + {"100000000000000000000", "0100000000000000000000", -1}, + {"100000000000000000000a", "100000000000000000000b", -1}, } for _, test := range tests {