1 //===- VersionTuple.cpp - Version Number Handling ---------------*- C++ -*-===//
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
7 //===----------------------------------------------------------------------===//
9 // This file implements the VersionTuple class, which represents a version in
10 // the form major[.minor[.subminor]].
12 //===----------------------------------------------------------------------===//
13 #include "llvm/Support/VersionTuple.h"
14 #include "llvm/Support/raw_ostream.h"
18 std::string
VersionTuple::getAsString() const {
21 llvm::raw_string_ostream
Out(Result
);
27 raw_ostream
&llvm::operator<<(raw_ostream
&Out
, const VersionTuple
&V
) {
29 if (Optional
<unsigned> Minor
= V
.getMinor())
31 if (Optional
<unsigned> Subminor
= V
.getSubminor())
32 Out
<< '.' << *Subminor
;
33 if (Optional
<unsigned> Build
= V
.getBuild())
38 static bool parseInt(StringRef
&input
, unsigned &value
) {
44 input
= input
.substr(1);
45 if (next
< '0' || next
> '9')
47 value
= (unsigned)(next
- '0');
49 while (!input
.empty()) {
51 if (next
< '0' || next
> '9')
53 input
= input
.substr(1);
54 value
= value
* 10 + (unsigned)(next
- '0');
60 bool VersionTuple::tryParse(StringRef input
) {
61 unsigned major
= 0, minor
= 0, micro
= 0, build
= 0;
63 // Parse the major version, [0-9]+
64 if (parseInt(input
, major
))
68 *this = VersionTuple(major
);
72 // If we're not done, parse the minor version, \.[0-9]+
75 input
= input
.substr(1);
76 if (parseInt(input
, minor
))
80 *this = VersionTuple(major
, minor
);
84 // If we're not done, parse the micro version, \.[0-9]+
87 input
= input
.substr(1);
88 if (parseInt(input
, micro
))
92 *this = VersionTuple(major
, minor
, micro
);
96 // If we're not done, parse the micro version, \.[0-9]+
99 input
= input
.substr(1);
100 if (parseInt(input
, build
))
103 // If we have characters left over, it's an error.
107 *this = VersionTuple(major
, minor
, micro
, build
);