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 "llvm/Support/VersionTuple.h"
15 #include "llvm/Support/raw_ostream.h"
19 std::string
VersionTuple::getAsString() const {
22 llvm::raw_string_ostream
Out(Result
);
28 raw_ostream
&llvm::operator<<(raw_ostream
&Out
, const VersionTuple
&V
) {
30 if (Optional
<unsigned> Minor
= V
.getMinor())
32 if (Optional
<unsigned> Subminor
= V
.getSubminor())
33 Out
<< '.' << *Subminor
;
34 if (Optional
<unsigned> Build
= V
.getBuild())
39 static bool parseInt(StringRef
&input
, unsigned &value
) {
45 input
= input
.substr(1);
46 if (next
< '0' || next
> '9')
48 value
= (unsigned)(next
- '0');
50 while (!input
.empty()) {
52 if (next
< '0' || next
> '9')
54 input
= input
.substr(1);
55 value
= value
* 10 + (unsigned)(next
- '0');
61 bool VersionTuple::tryParse(StringRef input
) {
62 unsigned major
= 0, minor
= 0, micro
= 0, build
= 0;
64 // Parse the major version, [0-9]+
65 if (parseInt(input
, major
))
69 *this = VersionTuple(major
);
73 // If we're not done, parse the minor version, \.[0-9]+
76 input
= input
.substr(1);
77 if (parseInt(input
, minor
))
81 *this = VersionTuple(major
, minor
);
85 // If we're not done, parse the micro version, \.[0-9]+
88 input
= input
.substr(1);
89 if (parseInt(input
, micro
))
93 *this = VersionTuple(major
, minor
, micro
);
97 // If we're not done, parse the micro version, \.[0-9]+
100 input
= input
.substr(1);
101 if (parseInt(input
, build
))
104 // If we have characters left over, it's an error.
108 *this = VersionTuple(major
, minor
, micro
, build
);