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 //===----------------------------------------------------------------------===//
14 #include "llvm/Support/VersionTuple.h"
15 #include "llvm/ADT/StringRef.h"
16 #include "llvm/Support/raw_ostream.h"
21 std::string
VersionTuple::getAsString() const {
24 llvm::raw_string_ostream
Out(Result
);
30 raw_ostream
&llvm::operator<<(raw_ostream
&Out
, const VersionTuple
&V
) {
32 if (std::optional
<unsigned> Minor
= V
.getMinor())
34 if (std::optional
<unsigned> Subminor
= V
.getSubminor())
35 Out
<< '.' << *Subminor
;
36 if (std::optional
<unsigned> Build
= V
.getBuild())
41 static bool parseInt(StringRef
&input
, unsigned &value
) {
47 input
= input
.substr(1);
48 if (next
< '0' || next
> '9')
50 value
= (unsigned)(next
- '0');
52 while (!input
.empty()) {
54 if (next
< '0' || next
> '9')
56 input
= input
.substr(1);
57 value
= value
* 10 + (unsigned)(next
- '0');
63 bool VersionTuple::tryParse(StringRef input
) {
64 unsigned major
= 0, minor
= 0, micro
= 0, build
= 0;
66 // Parse the major version, [0-9]+
67 if (parseInt(input
, major
))
71 *this = VersionTuple(major
);
75 // If we're not done, parse the minor version, \.[0-9]+
78 input
= input
.substr(1);
79 if (parseInt(input
, minor
))
83 *this = VersionTuple(major
, minor
);
87 // If we're not done, parse the micro version, \.[0-9]+
88 if (!input
.consume_front("."))
90 if (parseInt(input
, micro
))
94 *this = VersionTuple(major
, minor
, micro
);
98 // If we're not done, parse the micro version, \.[0-9]+
99 if (!input
.consume_front("."))
101 if (parseInt(input
, build
))
104 // If we have characters left over, it's an error.
108 *this = VersionTuple(major
, minor
, micro
, build
);