Sourcemeta Core 0.0.0
Loading...
Searching...
No Matches
semver.h
1#ifndef SOURCEMETA_CORE_SEMVER_H_
2#define SOURCEMETA_CORE_SEMVER_H_
3
4#ifndef SOURCEMETA_CORE_SEMVER_EXPORT
5#include <sourcemeta/core/semver_export.h>
6#endif
7
8// NOLINTBEGIN(misc-include-cleaner)
9#include <sourcemeta/core/semver_error.h>
10// NOLINTEND(misc-include-cleaner)
11
12#include <sourcemeta/core/preprocessor.h>
13
14#include <cstdint> // std::uint64_t
15#include <optional> // std::optional
16#include <string> // std::string
17#include <string_view> // std::string_view
18
27
28namespace sourcemeta::core {
29
33class SOURCEMETA_CORE_SEMVER_EXPORT SemVer {
34public:
36 enum class Mode : std::uint8_t {
39
40 // Permits the following deviations on the version core only:
41 // - Optional "v" or "V" prefix (e.g. "v1.2.3")
42 // - Missing patch, defaulting to 0 (e.g. "1.2")
43 // - Missing minor and patch, defaulting to 0 (e.g. "1")
44 // - Combinations of the above (e.g. "v1", "v1.2")
46 Loose
47 };
48
50 SemVer(std::string_view input, Mode mode = Mode::Strict);
51
53 [[nodiscard]] static auto from(std::string_view input,
54 Mode mode = Mode::Strict) noexcept
55 -> std::optional<SemVer>;
56
58 [[nodiscard]] SOURCEMETA_FORCEINLINE inline auto major() const noexcept
59 -> std::uint64_t {
60 return this->major_;
61 }
62
64 [[nodiscard]] SOURCEMETA_FORCEINLINE inline auto minor() const noexcept
65 -> std::uint64_t {
66 return this->minor_;
67 }
68
70 [[nodiscard]] SOURCEMETA_FORCEINLINE inline auto patch() const noexcept
71 -> std::uint64_t {
72 return this->patch_;
73 }
74
76 [[nodiscard]] SOURCEMETA_FORCEINLINE inline auto pre_release() const noexcept
77 -> std::string_view {
78 return this->pre_release_;
79 }
80
82 [[nodiscard]] SOURCEMETA_FORCEINLINE inline auto build() const noexcept
83 -> std::string_view {
84 return this->build_;
85 }
86
87 // Build metadata is included in equality because equality is not
88 // precedence. The spec says build metadata must be ignored when
89 // determining version *precedence*, but two versions with different build
90 // metadata are distinct versions.
91 // See https://semver.org/spec/v2.0.0.html#spec-item-10
92 SOURCEMETA_FORCEINLINE inline auto
93 operator==(const SemVer &other) const noexcept -> bool {
94 return this->major_ == other.major_ && this->minor_ == other.minor_ &&
95 this->patch_ == other.patch_ &&
96 this->pre_release_ == other.pre_release_ &&
97 this->build_ == other.build_;
98 }
99
100 auto operator<(const SemVer &other) const noexcept -> bool;
101
102 SOURCEMETA_FORCEINLINE inline auto
103 operator!=(const SemVer &other) const noexcept -> bool {
104 return !(*this == other);
105 }
106
107 SOURCEMETA_FORCEINLINE inline auto
108 operator>(const SemVer &other) const noexcept -> bool {
109 return other < *this;
110 }
111
112 SOURCEMETA_FORCEINLINE inline auto
113 operator<=(const SemVer &other) const noexcept -> bool {
114 return !(other < *this);
115 }
116
117 SOURCEMETA_FORCEINLINE inline auto
118 operator>=(const SemVer &other) const noexcept -> bool {
119 return !(*this < other);
120 }
121
123 [[nodiscard]] auto to_string() const -> std::string;
124
125private:
126 SemVer() = default;
127 std::uint64_t major_{0};
128 std::uint64_t minor_{0};
129 std::uint64_t patch_{0};
130#if defined(_MSC_VER)
131#pragma warning(disable : 4251)
132#endif
133 std::string_view pre_release_;
134 std::string_view build_;
135#if defined(_MSC_VER)
136#pragma warning(default : 4251)
137#endif
138};
139
140} // namespace sourcemeta::core
141
142#endif
@ Strict
The cookie is withheld from every cross-site request.
Definition http.h:269
SOURCEMETA_FORCEINLINE auto minor() const noexcept -> std::uint64_t
The minor version number.
Definition semver.h:64
SOURCEMETA_FORCEINLINE auto pre_release() const noexcept -> std::string_view
The pre-release identifier, or empty if none is present.
Definition semver.h:76
SOURCEMETA_FORCEINLINE auto major() const noexcept -> std::uint64_t
The major version number.
Definition semver.h:58
auto to_string() const -> std::string
Serialise the version back into its string representation.
SOURCEMETA_FORCEINLINE auto build() const noexcept -> std::string_view
The build metadata, or empty if none is present.
Definition semver.h:82
Mode
The level of strictness applied when parsing a version string.
Definition semver.h:36
@ Strict
Require a fully compliant version string.
Definition semver.h:38
SOURCEMETA_FORCEINLINE auto patch() const noexcept -> std::uint64_t
The patch version number.
Definition semver.h:70
SemVer(std::string_view input, Mode mode=Mode::Strict)
Construct a version by parsing the given string, throwing on failure.
static auto from(std::string_view input, Mode mode=Mode::Strict) noexcept -> std::optional< SemVer >
Parse a version from the given string, returning no value on failure.
Definition semver.h:33