version.go 3.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117
  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. )
  24. // Less reports whether v1 is less than v2.
  25. //
  26. // Note that "12" is less than "12.0".
  27. func Less(v1, v2 string) bool {
  28. return Compare(v1, v2) < 0
  29. }
  30. // LessEq reports whether v1 is less than or equal to v2.
  31. //
  32. // Note that "12" is less than "12.0".
  33. func LessEq(v1, v2 string) bool {
  34. return Compare(v1, v2) <= 0
  35. }
  36. func isnum(r rune) bool {
  37. return r >= '0' && r <= '9'
  38. }
  39. func notnum(r rune) bool {
  40. return !isnum(r)
  41. }
  42. // Compare returns an integer comparing two strings as version numbers.
  43. // The result will be -1, 0, or 1 representing the sign of v1 - v2:
  44. //
  45. // Compare(v1, v2) < 0 if v1 < v2
  46. // == 0 if v1 == v2
  47. // > 0 if v1 > v2
  48. func Compare(v1, v2 string) int {
  49. var (
  50. f1, f2 string
  51. n1, n2 uint64
  52. err error
  53. )
  54. for v1 != "" || v2 != "" {
  55. // Compare the non-numeric character run lexicographically.
  56. f1, v1 = splitPrefixFunc(v1, notnum)
  57. f2, v2 = splitPrefixFunc(v2, notnum)
  58. if res := strings.Compare(f1, f2); res != 0 {
  59. return res
  60. }
  61. // Compare the numeric character run numerically.
  62. f1, v1 = splitPrefixFunc(v1, isnum)
  63. f2, v2 = splitPrefixFunc(v2, isnum)
  64. // ParseUint refuses to parse empty strings, which would only
  65. // happen if we reached end-of-string. We follow the Debian
  66. // convention that empty strings mean zero, because
  67. // empirically that produces reasonable-feeling comparison
  68. // behavior.
  69. n1 = 0
  70. if f1 != "" {
  71. n1, err = strconv.ParseUint(f1, 10, 64)
  72. if err != nil {
  73. panic(fmt.Sprintf("all-number string %q didn't parse as string: %s", f1, err))
  74. }
  75. }
  76. n2 = 0
  77. if f2 != "" {
  78. n2, err = strconv.ParseUint(f2, 10, 64)
  79. if err != nil {
  80. panic(fmt.Sprintf("all-number string %q didn't parse as string: %s", f2, err))
  81. }
  82. }
  83. switch {
  84. case n1 == n2:
  85. case n1 < n2:
  86. return -1
  87. case n1 > n2:
  88. return 1
  89. }
  90. }
  91. // Only way to reach here is if v1 and v2 run out of fields
  92. // simultaneously - i.e. exactly equal versions.
  93. return 0
  94. }
  95. // splitPrefixFunc splits s at the first rune where f(rune) is false.
  96. func splitPrefixFunc(s string, f func(rune) bool) (string, string) {
  97. for i, r := range s {
  98. if !f(r) {
  99. return s[:i], s[i:]
  100. }
  101. }
  102. return s, s[:0]
  103. }