version.go 2.6 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495
  1. // Copyright (c) Tailscale Inc & AUTHORS
  2. // SPDX-License-Identifier: BSD-3-Clause
  3. // Package cmpver implements a variant of debian version number
  4. // comparison.
  5. //
  6. // A version is a string consisting of alternating non-numeric and
  7. // numeric fields. When comparing two versions, each one is broken
  8. // down into its respective fields, and the fields are compared
  9. // pairwise. The comparison is lexicographic for non-numeric fields,
  10. // numeric for numeric fields. The first non-equal field pair
  11. // determines the ordering of the two versions.
  12. //
  13. // This comparison scheme is a simplified version of Debian's version
  14. // number comparisons. Debian differs in a few details of
  15. // lexicographical field comparison, where certain characters have
  16. // special meaning and ordering. We don't need that, because Tailscale
  17. // version numbers don't need it.
  18. package cmpver
  19. import (
  20. "fmt"
  21. "strconv"
  22. "strings"
  23. "unicode"
  24. )
  25. // Compare returns an integer comparing two strings as version
  26. // numbers. The result will be 0 if v1==v2, -1 if v1 < v2, and +1 if
  27. // v1 > v2.
  28. func Compare(v1, v2 string) int {
  29. notNumber := func(r rune) bool { return !unicode.IsNumber(r) }
  30. var (
  31. f1, f2 string
  32. n1, n2 uint64
  33. err error
  34. )
  35. for v1 != "" || v2 != "" {
  36. // Compare the non-numeric character run lexicographically.
  37. f1, v1 = splitPrefixFunc(v1, notNumber)
  38. f2, v2 = splitPrefixFunc(v2, notNumber)
  39. if res := strings.Compare(f1, f2); res != 0 {
  40. return res
  41. }
  42. // Compare the numeric character run numerically.
  43. f1, v1 = splitPrefixFunc(v1, unicode.IsNumber)
  44. f2, v2 = splitPrefixFunc(v2, unicode.IsNumber)
  45. // ParseUint refuses to parse empty strings, which would only
  46. // happen if we reached end-of-string. We follow the Debian
  47. // convention that empty strings mean zero, because
  48. // empirically that produces reasonable-feeling comparison
  49. // behavior.
  50. n1 = 0
  51. if f1 != "" {
  52. n1, err = strconv.ParseUint(f1, 10, 64)
  53. if err != nil {
  54. panic(fmt.Sprintf("all-number string %q didn't parse as string: %s", f1, err))
  55. }
  56. }
  57. n2 = 0
  58. if f2 != "" {
  59. n2, err = strconv.ParseUint(f2, 10, 64)
  60. if err != nil {
  61. panic(fmt.Sprintf("all-number string %q didn't parse as string: %s", f2, err))
  62. }
  63. }
  64. switch {
  65. case n1 == n2:
  66. case n1 < n2:
  67. return -1
  68. case n1 > n2:
  69. return 1
  70. }
  71. }
  72. // Only way to reach here is if v1 and v2 run out of fields
  73. // simultaneously - i.e. exactly equal versions.
  74. return 0
  75. }
  76. // splitPrefixFunc splits s at the first rune where f(rune) is false.
  77. func splitPrefixFunc(s string, f func(rune) bool) (string, string) {
  78. for i, r := range s {
  79. if !f(r) {
  80. return s[:i], s[i:]
  81. }
  82. }
  83. return s, s[:0]
  84. }