VersionTuple.cpp 2.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112
  1. //===- VersionTuple.cpp - Version Number Handling ---------------*- C++ -*-===//
  2. //
  3. // Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
  4. // See https://llvm.org/LICENSE.txt for license information.
  5. // SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
  6. //
  7. //===----------------------------------------------------------------------===//
  8. //
  9. // This file implements the VersionTuple class, which represents a version in
  10. // the form major[.minor[.subminor]].
  11. //
  12. //===----------------------------------------------------------------------===//
  13. #include "llvm/Support/VersionTuple.h"
  14. #include "llvm/ADT/StringRef.h"
  15. #include "llvm/Support/raw_ostream.h"
  16. #include <cassert>
  17. using namespace llvm;
  18. std::string VersionTuple::getAsString() const {
  19. std::string Result;
  20. {
  21. llvm::raw_string_ostream Out(Result);
  22. Out << *this;
  23. }
  24. return Result;
  25. }
  26. raw_ostream &llvm::operator<<(raw_ostream &Out, const VersionTuple &V) {
  27. Out << V.getMajor();
  28. if (std::optional<unsigned> Minor = V.getMinor())
  29. Out << '.' << *Minor;
  30. if (std::optional<unsigned> Subminor = V.getSubminor())
  31. Out << '.' << *Subminor;
  32. if (std::optional<unsigned> Build = V.getBuild())
  33. Out << '.' << *Build;
  34. return Out;
  35. }
  36. static bool parseInt(StringRef &input, unsigned &value) {
  37. assert(value == 0);
  38. if (input.empty())
  39. return true;
  40. char next = input[0];
  41. input = input.substr(1);
  42. if (next < '0' || next > '9')
  43. return true;
  44. value = (unsigned)(next - '0');
  45. while (!input.empty()) {
  46. next = input[0];
  47. if (next < '0' || next > '9')
  48. return false;
  49. input = input.substr(1);
  50. value = value * 10 + (unsigned)(next - '0');
  51. }
  52. return false;
  53. }
  54. bool VersionTuple::tryParse(StringRef input) {
  55. unsigned major = 0, minor = 0, micro = 0, build = 0;
  56. // Parse the major version, [0-9]+
  57. if (parseInt(input, major))
  58. return true;
  59. if (input.empty()) {
  60. *this = VersionTuple(major);
  61. return false;
  62. }
  63. // If we're not done, parse the minor version, \.[0-9]+
  64. if (input[0] != '.')
  65. return true;
  66. input = input.substr(1);
  67. if (parseInt(input, minor))
  68. return true;
  69. if (input.empty()) {
  70. *this = VersionTuple(major, minor);
  71. return false;
  72. }
  73. // If we're not done, parse the micro version, \.[0-9]+
  74. if (input[0] != '.')
  75. return true;
  76. input = input.substr(1);
  77. if (parseInt(input, micro))
  78. return true;
  79. if (input.empty()) {
  80. *this = VersionTuple(major, minor, micro);
  81. return false;
  82. }
  83. // If we're not done, parse the micro version, \.[0-9]+
  84. if (input[0] != '.')
  85. return true;
  86. input = input.substr(1);
  87. if (parseInt(input, build))
  88. return true;
  89. // If we have characters left over, it's an error.
  90. if (!input.empty())
  91. return true;
  92. *this = VersionTuple(major, minor, micro, build);
  93. return false;
  94. }