10b57cec5SDimitry Andric //===- VersionTuple.cpp - Version Number Handling ---------------*- C++ -*-===// 20b57cec5SDimitry Andric // 30b57cec5SDimitry Andric // Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions. 40b57cec5SDimitry Andric // See https://llvm.org/LICENSE.txt for license information. 50b57cec5SDimitry Andric // SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception 60b57cec5SDimitry Andric // 70b57cec5SDimitry Andric //===----------------------------------------------------------------------===// 80b57cec5SDimitry Andric // 90b57cec5SDimitry Andric // This file implements the VersionTuple class, which represents a version in 100b57cec5SDimitry Andric // the form major[.minor[.subminor]]. 110b57cec5SDimitry Andric // 120b57cec5SDimitry Andric //===----------------------------------------------------------------------===// 135ffd83dbSDimitry Andric 140b57cec5SDimitry Andric #include "llvm/Support/VersionTuple.h" 155ffd83dbSDimitry Andric #include "llvm/ADT/StringRef.h" 160b57cec5SDimitry Andric #include "llvm/Support/raw_ostream.h" 175ffd83dbSDimitry Andric #include <cassert> 180b57cec5SDimitry Andric 190b57cec5SDimitry Andric using namespace llvm; 200b57cec5SDimitry Andric 210b57cec5SDimitry Andric std::string VersionTuple::getAsString() const { 220b57cec5SDimitry Andric std::string Result; 230b57cec5SDimitry Andric { 240b57cec5SDimitry Andric llvm::raw_string_ostream Out(Result); 250b57cec5SDimitry Andric Out << *this; 260b57cec5SDimitry Andric } 270b57cec5SDimitry Andric return Result; 280b57cec5SDimitry Andric } 290b57cec5SDimitry Andric 300b57cec5SDimitry Andric raw_ostream &llvm::operator<<(raw_ostream &Out, const VersionTuple &V) { 310b57cec5SDimitry Andric Out << V.getMajor(); 32bdd1243dSDimitry Andric if (std::optional<unsigned> Minor = V.getMinor()) 330b57cec5SDimitry Andric Out << '.' << *Minor; 34bdd1243dSDimitry Andric if (std::optional<unsigned> Subminor = V.getSubminor()) 350b57cec5SDimitry Andric Out << '.' << *Subminor; 36bdd1243dSDimitry Andric if (std::optional<unsigned> Build = V.getBuild()) 370b57cec5SDimitry Andric Out << '.' << *Build; 380b57cec5SDimitry Andric return Out; 390b57cec5SDimitry Andric } 400b57cec5SDimitry Andric 410b57cec5SDimitry Andric static bool parseInt(StringRef &input, unsigned &value) { 420b57cec5SDimitry Andric assert(value == 0); 430b57cec5SDimitry Andric if (input.empty()) 440b57cec5SDimitry Andric return true; 450b57cec5SDimitry Andric 460b57cec5SDimitry Andric char next = input[0]; 470b57cec5SDimitry Andric input = input.substr(1); 480b57cec5SDimitry Andric if (next < '0' || next > '9') 490b57cec5SDimitry Andric return true; 500b57cec5SDimitry Andric value = (unsigned)(next - '0'); 510b57cec5SDimitry Andric 520b57cec5SDimitry Andric while (!input.empty()) { 530b57cec5SDimitry Andric next = input[0]; 540b57cec5SDimitry Andric if (next < '0' || next > '9') 550b57cec5SDimitry Andric return false; 560b57cec5SDimitry Andric input = input.substr(1); 570b57cec5SDimitry Andric value = value * 10 + (unsigned)(next - '0'); 580b57cec5SDimitry Andric } 590b57cec5SDimitry Andric 600b57cec5SDimitry Andric return false; 610b57cec5SDimitry Andric } 620b57cec5SDimitry Andric 630b57cec5SDimitry Andric bool VersionTuple::tryParse(StringRef input) { 640b57cec5SDimitry Andric unsigned major = 0, minor = 0, micro = 0, build = 0; 650b57cec5SDimitry Andric 660b57cec5SDimitry Andric // Parse the major version, [0-9]+ 670b57cec5SDimitry Andric if (parseInt(input, major)) 680b57cec5SDimitry Andric return true; 690b57cec5SDimitry Andric 700b57cec5SDimitry Andric if (input.empty()) { 710b57cec5SDimitry Andric *this = VersionTuple(major); 720b57cec5SDimitry Andric return false; 730b57cec5SDimitry Andric } 740b57cec5SDimitry Andric 750b57cec5SDimitry Andric // If we're not done, parse the minor version, \.[0-9]+ 760b57cec5SDimitry Andric if (input[0] != '.') 770b57cec5SDimitry Andric return true; 780b57cec5SDimitry Andric input = input.substr(1); 790b57cec5SDimitry Andric if (parseInt(input, minor)) 800b57cec5SDimitry Andric return true; 810b57cec5SDimitry Andric 820b57cec5SDimitry Andric if (input.empty()) { 830b57cec5SDimitry Andric *this = VersionTuple(major, minor); 840b57cec5SDimitry Andric return false; 850b57cec5SDimitry Andric } 860b57cec5SDimitry Andric 870b57cec5SDimitry Andric // If we're not done, parse the micro version, \.[0-9]+ 88*7a6dacacSDimitry Andric if (!input.consume_front(".")) 890b57cec5SDimitry Andric return true; 900b57cec5SDimitry Andric if (parseInt(input, micro)) 910b57cec5SDimitry Andric return true; 920b57cec5SDimitry Andric 930b57cec5SDimitry Andric if (input.empty()) { 940b57cec5SDimitry Andric *this = VersionTuple(major, minor, micro); 950b57cec5SDimitry Andric return false; 960b57cec5SDimitry Andric } 970b57cec5SDimitry Andric 980b57cec5SDimitry Andric // If we're not done, parse the micro version, \.[0-9]+ 99*7a6dacacSDimitry Andric if (!input.consume_front(".")) 1000b57cec5SDimitry Andric return true; 1010b57cec5SDimitry Andric if (parseInt(input, build)) 1020b57cec5SDimitry Andric return true; 1030b57cec5SDimitry Andric 1040b57cec5SDimitry Andric // If we have characters left over, it's an error. 1050b57cec5SDimitry Andric if (!input.empty()) 1060b57cec5SDimitry Andric return true; 1070b57cec5SDimitry Andric 1080b57cec5SDimitry Andric *this = VersionTuple(major, minor, micro, build); 1090b57cec5SDimitry Andric return false; 1100b57cec5SDimitry Andric } 111