forked from celeritas-project/celeritas
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Version.cc
79 lines (70 loc) · 2.37 KB
/
Version.cc
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
//----------------------------------*-C++-*----------------------------------//
// Copyright 2023 UT-Battelle, LLC, and other Celeritas developers.
// See the top-level COPYRIGHT file for details.
// SPDX-License-Identifier: (Apache-2.0 OR MIT)
//---------------------------------------------------------------------------//
//! \file corecel/sys/Version.cc
//---------------------------------------------------------------------------//
#include "Version.hh"
#include <cstdlib>
#include <iostream>
#include <regex>
#include "celeritas_version.h"
#include "corecel/Assert.hh"
#include "corecel/io/Join.hh"
namespace celeritas
{
//---------------------------------------------------------------------------//
/*!
* Construct from a string "1.2.3".
*/
Version Version::from_string(std::string_view sv)
{
static const std::regex version_regex{
R"re(^(\d+)(?:\.(\d+)(?:\.(\d+)(?:\.\d+)*)?)?(?:-.*)?)re"};
std::match_results<std::string_view::iterator> version_match;
bool matched
= std::regex_match(sv.begin(), sv.end(), version_match, version_regex);
CELER_VALIDATE(matched, << "failed to parse version '" << sv << "'");
auto match_to_int = [](auto const& submatch) {
if (submatch.length() == 0)
{
// No version component given
return size_type{0};
}
int result = std::atoi(&(*submatch.first));
return static_cast<size_type>(result);
};
return Version{match_to_int(version_match[1]),
match_to_int(version_match[2]),
match_to_int(version_match[3])};
}
//---------------------------------------------------------------------------//
/*!
* Write to a stream.
*/
std::ostream& operator<<(std::ostream& os, Version const& v)
{
os << join(v.value().begin(), v.value().end(), '.');
return os;
}
//---------------------------------------------------------------------------//
//! Save as a string
std::string to_string(Version const& v)
{
std::ostringstream os;
os << v;
return os.str();
}
//---------------------------------------------------------------------------//
/*!
* Get the Celeritas version.
*/
Version celer_version()
{
return {celeritas_version_major,
celeritas_version_minor,
celeritas_version_patch};
}
//---------------------------------------------------------------------------//
} // namespace celeritas