1eb11fae6SDimitry Andric //===- VersionTuple.cpp - Version Number Handling ---------------*- C++ -*-===//
2eb11fae6SDimitry Andric //
3e6d15924SDimitry Andric // Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
4e6d15924SDimitry Andric // See https://llvm.org/LICENSE.txt for license information.
5e6d15924SDimitry Andric // SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
6eb11fae6SDimitry Andric //
7eb11fae6SDimitry Andric //===----------------------------------------------------------------------===//
8eb11fae6SDimitry Andric //
9eb11fae6SDimitry Andric // This file implements the VersionTuple class, which represents a version in
10eb11fae6SDimitry Andric // the form major[.minor[.subminor]].
11eb11fae6SDimitry Andric //
12eb11fae6SDimitry Andric //===----------------------------------------------------------------------===//
13cfca06d7SDimitry Andric
14eb11fae6SDimitry Andric #include "llvm/Support/VersionTuple.h"
15cfca06d7SDimitry Andric #include "llvm/ADT/StringRef.h"
16eb11fae6SDimitry Andric #include "llvm/Support/raw_ostream.h"
17cfca06d7SDimitry Andric #include <cassert>
18eb11fae6SDimitry Andric
19eb11fae6SDimitry Andric using namespace llvm;
20eb11fae6SDimitry Andric
getAsString() const21eb11fae6SDimitry Andric std::string VersionTuple::getAsString() const {
22eb11fae6SDimitry Andric std::string Result;
23eb11fae6SDimitry Andric {
24eb11fae6SDimitry Andric llvm::raw_string_ostream Out(Result);
25eb11fae6SDimitry Andric Out << *this;
26eb11fae6SDimitry Andric }
27eb11fae6SDimitry Andric return Result;
28eb11fae6SDimitry Andric }
29eb11fae6SDimitry Andric
operator <<(raw_ostream & Out,const VersionTuple & V)30eb11fae6SDimitry Andric raw_ostream &llvm::operator<<(raw_ostream &Out, const VersionTuple &V) {
31eb11fae6SDimitry Andric Out << V.getMajor();
32e3b55780SDimitry Andric if (std::optional<unsigned> Minor = V.getMinor())
33eb11fae6SDimitry Andric Out << '.' << *Minor;
34e3b55780SDimitry Andric if (std::optional<unsigned> Subminor = V.getSubminor())
35eb11fae6SDimitry Andric Out << '.' << *Subminor;
36e3b55780SDimitry Andric if (std::optional<unsigned> Build = V.getBuild())
37eb11fae6SDimitry Andric Out << '.' << *Build;
38eb11fae6SDimitry Andric return Out;
39eb11fae6SDimitry Andric }
40eb11fae6SDimitry Andric
parseInt(StringRef & input,unsigned & value)41eb11fae6SDimitry Andric static bool parseInt(StringRef &input, unsigned &value) {
42eb11fae6SDimitry Andric assert(value == 0);
43eb11fae6SDimitry Andric if (input.empty())
44eb11fae6SDimitry Andric return true;
45eb11fae6SDimitry Andric
46eb11fae6SDimitry Andric char next = input[0];
47eb11fae6SDimitry Andric input = input.substr(1);
48eb11fae6SDimitry Andric if (next < '0' || next > '9')
49eb11fae6SDimitry Andric return true;
50eb11fae6SDimitry Andric value = (unsigned)(next - '0');
51eb11fae6SDimitry Andric
52eb11fae6SDimitry Andric while (!input.empty()) {
53eb11fae6SDimitry Andric next = input[0];
54eb11fae6SDimitry Andric if (next < '0' || next > '9')
55eb11fae6SDimitry Andric return false;
56eb11fae6SDimitry Andric input = input.substr(1);
57eb11fae6SDimitry Andric value = value * 10 + (unsigned)(next - '0');
58eb11fae6SDimitry Andric }
59eb11fae6SDimitry Andric
60eb11fae6SDimitry Andric return false;
61eb11fae6SDimitry Andric }
62eb11fae6SDimitry Andric
tryParse(StringRef input)63eb11fae6SDimitry Andric bool VersionTuple::tryParse(StringRef input) {
64eb11fae6SDimitry Andric unsigned major = 0, minor = 0, micro = 0, build = 0;
65eb11fae6SDimitry Andric
66eb11fae6SDimitry Andric // Parse the major version, [0-9]+
67eb11fae6SDimitry Andric if (parseInt(input, major))
68eb11fae6SDimitry Andric return true;
69eb11fae6SDimitry Andric
70eb11fae6SDimitry Andric if (input.empty()) {
71eb11fae6SDimitry Andric *this = VersionTuple(major);
72eb11fae6SDimitry Andric return false;
73eb11fae6SDimitry Andric }
74eb11fae6SDimitry Andric
75eb11fae6SDimitry Andric // If we're not done, parse the minor version, \.[0-9]+
76eb11fae6SDimitry Andric if (input[0] != '.')
77eb11fae6SDimitry Andric return true;
78eb11fae6SDimitry Andric input = input.substr(1);
79eb11fae6SDimitry Andric if (parseInt(input, minor))
80eb11fae6SDimitry Andric return true;
81eb11fae6SDimitry Andric
82eb11fae6SDimitry Andric if (input.empty()) {
83eb11fae6SDimitry Andric *this = VersionTuple(major, minor);
84eb11fae6SDimitry Andric return false;
85eb11fae6SDimitry Andric }
86eb11fae6SDimitry Andric
87eb11fae6SDimitry Andric // If we're not done, parse the micro version, \.[0-9]+
884df029ccSDimitry Andric if (!input.consume_front("."))
89eb11fae6SDimitry Andric return true;
90eb11fae6SDimitry Andric if (parseInt(input, micro))
91eb11fae6SDimitry Andric return true;
92eb11fae6SDimitry Andric
93eb11fae6SDimitry Andric if (input.empty()) {
94eb11fae6SDimitry Andric *this = VersionTuple(major, minor, micro);
95eb11fae6SDimitry Andric return false;
96eb11fae6SDimitry Andric }
97eb11fae6SDimitry Andric
98eb11fae6SDimitry Andric // If we're not done, parse the micro version, \.[0-9]+
994df029ccSDimitry Andric if (!input.consume_front("."))
100eb11fae6SDimitry Andric return true;
101eb11fae6SDimitry Andric if (parseInt(input, build))
102eb11fae6SDimitry Andric return true;
103eb11fae6SDimitry Andric
104eb11fae6SDimitry Andric // If we have characters left over, it's an error.
105eb11fae6SDimitry Andric if (!input.empty())
106eb11fae6SDimitry Andric return true;
107eb11fae6SDimitry Andric
108eb11fae6SDimitry Andric *this = VersionTuple(major, minor, micro, build);
109eb11fae6SDimitry Andric return false;
110eb11fae6SDimitry Andric }
111