VersionTuple.cpp 2.3 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788
  1. //===- VersionTuple.cpp - Version Number Handling ---------------*- C++ -*-===//
  2. //
  3. // The LLVM Compiler Infrastructure
  4. //
  5. // This file is distributed under the University of Illinois Open Source
  6. // License. See LICENSE.TXT for details.
  7. //
  8. //===----------------------------------------------------------------------===//
  9. //
  10. // This file implements the VersionTuple class, which represents a version in
  11. // the form major[.minor[.subminor]].
  12. //
  13. //===----------------------------------------------------------------------===//
  14. #include "clang/Basic/VersionTuple.h"
  15. #include "llvm/Support/raw_ostream.h"
  16. using namespace clang;
  17. std::string VersionTuple::getAsString() const {
  18. std::string Result;
  19. {
  20. llvm::raw_string_ostream Out(Result);
  21. Out << *this;
  22. }
  23. return Result;
  24. }
  25. raw_ostream& clang::operator<<(raw_ostream &Out,
  26. const VersionTuple &V) {
  27. Out << V.getMajor();
  28. if (Optional<unsigned> Minor = V.getMinor())
  29. Out << '.' << *Minor;
  30. if (Optional<unsigned> Subminor = V.getSubminor())
  31. Out << '.' << *Subminor;
  32. return Out;
  33. }
  34. static bool parseInt(StringRef &input, unsigned &value) {
  35. assert(value == 0);
  36. if (input.empty()) return true;
  37. char next = input[0];
  38. input = input.substr(1);
  39. if (next < '0' || next > '9') return true;
  40. value = (unsigned) (next - '0');
  41. while (!input.empty()) {
  42. next = input[0];
  43. if (next < '0' || next > '9') return false;
  44. input = input.substr(1);
  45. value = value * 10 + (unsigned) (next - '0');
  46. }
  47. return false;
  48. }
  49. bool VersionTuple::tryParse(StringRef input) {
  50. unsigned major = 0, minor = 0, micro = 0;
  51. // Parse the major version, [0-9]+
  52. if (parseInt(input, major)) return true;
  53. if (input.empty()) {
  54. *this = VersionTuple(major);
  55. return false;
  56. }
  57. // If we're not done, parse the minor version, \.[0-9]+
  58. if (input[0] != '.') return true;
  59. input = input.substr(1);
  60. if (parseInt(input, minor)) return true;
  61. if (input.empty()) {
  62. *this = VersionTuple(major, minor);
  63. return false;
  64. }
  65. // If we're not done, parse the micro version, \.[0-9]+
  66. if (input[0] != '.') return true;
  67. input = input.substr(1);
  68. if (parseInt(input, micro)) return true;
  69. // If we have characters left over, it's an error.
  70. if (!input.empty()) return true;
  71. *this = VersionTuple(major, minor, micro);
  72. return false;
  73. }