1 //===- VersionTuple.cpp - Version Number Handling ---------------*- C++ -*-===//
3 // The LLVM Compiler Infrastructure
5 // This file is distributed under the University of Illinois Open Source
6 // License. See LICENSE.TXT for details.
8 //===----------------------------------------------------------------------===//
10 // This file implements the VersionTuple class, which represents a version in
11 // the form major[.minor[.subminor]].
13 //===----------------------------------------------------------------------===//
14 #include "clang/Basic/VersionTuple.h"
15 #include "llvm/Support/raw_ostream.h"
17 using namespace clang;
19 std::string VersionTuple::getAsString() const {
22 llvm::raw_string_ostream Out(Result);
28 raw_ostream& clang::operator<<(raw_ostream &Out,
29 const VersionTuple &V) {
31 if (Optional<unsigned> Minor = V.getMinor())
32 Out << (V.usesUnderscores() ? '_' : '.') << *Minor;
33 if (Optional<unsigned> Subminor = V.getSubminor())
34 Out << (V.usesUnderscores() ? '_' : '.') << *Subminor;
38 static bool parseInt(StringRef &input, unsigned &value) {
40 if (input.empty()) return true;
43 input = input.substr(1);
44 if (next < '0' || next > '9') return true;
45 value = (unsigned) (next - '0');
47 while (!input.empty()) {
49 if (next < '0' || next > '9') return false;
50 input = input.substr(1);
51 value = value * 10 + (unsigned) (next - '0');
57 bool VersionTuple::tryParse(StringRef input) {
58 unsigned major = 0, minor = 0, micro = 0;
60 // Parse the major version, [0-9]+
61 if (parseInt(input, major)) return true;
64 *this = VersionTuple(major);
68 // If we're not done, parse the minor version, \.[0-9]+
69 if (input[0] != '.') return true;
70 input = input.substr(1);
71 if (parseInt(input, minor)) return true;
74 *this = VersionTuple(major, minor);
78 // If we're not done, parse the micro version, \.[0-9]+
79 if (input[0] != '.') return true;
80 input = input.substr(1);
81 if (parseInt(input, micro)) return true;
83 // If we have characters left over, it's an error.
84 if (!input.empty()) return true;
86 *this = VersionTuple(major, minor, micro);