compare.go 1.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960
  1. // Copyright 2019 Yunion
  2. //
  3. // Licensed under the Apache License, Version 2.0 (the "License");
  4. // you may not use this file except in compliance with the License.
  5. // You may obtain a copy of the License at
  6. //
  7. // http://www.apache.org/licenses/LICENSE-2.0
  8. //
  9. // Unless required by applicable law or agreed to in writing, software
  10. // distributed under the License is distributed on an "AS IS" BASIS,
  11. // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  12. // See the License for the specific language governing permissions and
  13. // limitations under the License.
  14. package version
  15. import (
  16. "strconv"
  17. "strings"
  18. )
  19. func less(v1Str, v2Str string) (bool, bool) {
  20. v1 := strings.Split(v1Str, ".")
  21. v2 := strings.Split(v2Str, ".")
  22. var i = 0
  23. for ; i < len(v2); i++ {
  24. if i >= len(v1) {
  25. return true, false
  26. }
  27. v, _ := strconv.ParseInt(v2[i], 10, 0)
  28. compareV, _ := strconv.ParseInt(v1[i], 10, 0)
  29. if v < compareV {
  30. return false, false
  31. } else if compareV < v {
  32. return true, false
  33. }
  34. }
  35. if i < len(v1)-1 {
  36. return false, false
  37. }
  38. return true, true
  39. }
  40. func LE(v1Str, v2Str string) bool {
  41. l, _ := less(v1Str, v2Str)
  42. return l
  43. }
  44. func LT(v1Str, v2Str string) bool {
  45. l, e := less(v1Str, v2Str)
  46. return l && !e
  47. }
  48. func GT(v1Str, v2Str string) bool {
  49. return LT(v2Str, v1Str)
  50. }
  51. func GE(v1Str, v2Str string) bool {
  52. return LE(v2Str, v1Str)
  53. }