You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

171 lines
3.8 KiB

  1. package version
  2. import (
  3. "regexp"
  4. "strconv"
  5. "strings"
  6. )
  7. var regexpSigns = regexp.MustCompile(`[_\-+]`)
  8. var regexpDotBeforeDigit = regexp.MustCompile(`([^.\d]+)`)
  9. var regexpMultipleDots = regexp.MustCompile(`\.{2,}`)
  10. var specialForms = map[string]int{
  11. "dev": -6,
  12. "alpha": -5,
  13. "a": -5,
  14. "beta": -4,
  15. "b": -4,
  16. "RC": -3,
  17. "rc": -3,
  18. "#": -2,
  19. "p": 1,
  20. "pl": 1,
  21. }
  22. var unknownForm int = -7
  23. // Compares two version number strings, for a particular relationship
  24. //
  25. // Usage
  26. // version.Compare("2.3.4", "v3.1.2", "<")
  27. // Returns: true
  28. //
  29. // version.Compare("1.0rc1", "1.0", ">=")
  30. // Returns: false
  31. func Compare(version1, version2, operator string) bool {
  32. version1N := Normalize(version1)
  33. version2N := Normalize(version2)
  34. return CompareNormalized(version1N, version2N, operator)
  35. }
  36. // Compares two normalizated version number strings, for a particular relationship
  37. //
  38. // The function first replaces _, - and + with a dot . in the version strings
  39. // and also inserts dots . before and after any non number so that for example
  40. // '4.3.2RC1' becomes '4.3.2.RC.1'.
  41. //
  42. // Then it splits the results like if you were using Split(version, '.').
  43. // Then it compares the parts starting from left to right. If a part contains
  44. // special version strings these are handled in the following order: any string
  45. // not found in this list:
  46. // < dev < alpha = a < beta = b < RC = rc < # < pl = p.
  47. //
  48. // Usage
  49. // version.CompareNormalized("1.0-dev", "1.0", "<")
  50. // Returns: true
  51. //
  52. // version.CompareNormalized("1.0rc1", "1.0", ">=")
  53. // Returns: false
  54. //
  55. // version.CompareNormalized("1.0", "1.0b1", "ge")
  56. // Returns: true
  57. func CompareNormalized(version1, version2, operator string) bool {
  58. compare := CompareSimple(version1, version2)
  59. switch {
  60. case operator == ">" || operator == "gt":
  61. return compare > 0
  62. case operator == ">=" || operator == "ge":
  63. return compare >= 0
  64. case operator == "<=" || operator == "le":
  65. return compare <= 0
  66. case operator == "==" || operator == "=" || operator == "eq":
  67. return compare == 0
  68. case operator == "<>" || operator == "!=" || operator == "ne":
  69. return compare != 0
  70. case operator == "" || operator == "<" || operator == "lt":
  71. return compare < 0
  72. }
  73. return false
  74. }
  75. // Compares two normalizated version number strings
  76. //
  77. // Just the same of CompareVersion but return a int result, 0 if both version
  78. // are equal, 1 if the right side is bigger and -1 if the right side is lower
  79. //
  80. // Usage
  81. // version.CompareSimple("1.2", "1.0.1")
  82. // Returns: 1
  83. //
  84. // version.CompareSimple("1.0rc1", "1.0")
  85. // Returns: -1
  86. func CompareSimple(version1, version2 string) int {
  87. var x, r, l int = 0, 0, 0
  88. v1, v2 := prepVersion(version1), prepVersion(version2)
  89. len1, len2 := len(v1), len(v2)
  90. if len1 > len2 {
  91. x = len1
  92. } else {
  93. x = len2
  94. }
  95. for i := 0; i < x; i++ {
  96. if i < len1 && i < len2 {
  97. if v1[i] == v2[i] {
  98. continue
  99. }
  100. }
  101. r = 0
  102. if i < len1 {
  103. r = numVersion(v1[i])
  104. }
  105. l = 0
  106. if i < len2 {
  107. l = numVersion(v2[i])
  108. }
  109. if r < l {
  110. return -1
  111. } else if r > l {
  112. return 1
  113. }
  114. }
  115. return 0
  116. }
  117. func prepVersion(version string) []string {
  118. if len(version) == 0 {
  119. return []string{""}
  120. }
  121. version = regexpSigns.ReplaceAllString(version, ".")
  122. version = regexpDotBeforeDigit.ReplaceAllString(version, ".$1.")
  123. version = regexpMultipleDots.ReplaceAllString(version, ".")
  124. return strings.Split(version, ".")
  125. }
  126. func numVersion(value string) int {
  127. if value == "" {
  128. return 0
  129. }
  130. if number, err := strconv.Atoi(value); err == nil {
  131. return number
  132. }
  133. if special, ok := specialForms[value]; ok {
  134. return special
  135. }
  136. return unknownForm
  137. }
  138. func ValidSimpleVersionFormat(value string) bool {
  139. normalized := Normalize(value)
  140. for _, component := range prepVersion(normalized) {
  141. if numVersion(component) == unknownForm {
  142. return false
  143. }
  144. }
  145. return true
  146. }