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;
35 if (Optional<unsigned> Build = V.getBuild())
36 Out << (V.usesUnderscores() ? '_' : '.') << *Build;
40 static bool parseInt(StringRef &input, unsigned &value) {
42 if (input.empty()) return true;
45 input = input.substr(1);
46 if (next < '0' || next > '9') return true;
47 value = (unsigned) (next - '0');
49 while (!input.empty()) {
51 if (next < '0' || next > '9') return false;
52 input = input.substr(1);
53 value = value * 10 + (unsigned) (next - '0');
59 bool VersionTuple::tryParse(StringRef input) {
60 unsigned major = 0, minor = 0, micro = 0, build = 0;
62 // Parse the major version, [0-9]+
63 if (parseInt(input, major)) return true;
66 *this = VersionTuple(major);
70 // If we're not done, parse the minor version, \.[0-9]+
71 if (input[0] != '.') return true;
72 input = input.substr(1);
73 if (parseInt(input, minor)) return true;
76 *this = VersionTuple(major, minor);
80 // If we're not done, parse the micro version, \.[0-9]+
81 if (input[0] != '.') return true;
82 input = input.substr(1);
83 if (parseInt(input, micro)) return true;
86 *this = VersionTuple(major, minor, micro);
90 // If we're not done, parse the micro version, \.[0-9]+
91 if (input[0] != '.') return true;
92 input = input.substr(1);
93 if (parseInt(input, build)) return true;
95 // If we have characters left over, it's an error.
96 if (!input.empty()) return true;
98 *this = VersionTuple(major, minor, micro, build);