version.go 2.6 KB

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