-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathversionnumber.cpp
More file actions
150 lines (128 loc) · 4.93 KB
/
Copy pathversionnumber.cpp
File metadata and controls
150 lines (128 loc) · 4.93 KB
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
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
// SPDX-License-Identifier: MIT
#include "versionnumber.h"
#include <cstdlib>
#include <charconv>
#include "algorithms.h"
namespace stdc {
VersionNumber::VersionNumber() {
m_numbers[0] = 0;
m_numbers[1] = 0;
m_numbers[2] = 0;
m_numbers[3] = 0;
}
VersionNumber::VersionNumber(int major, int minor, int patch, int tweak) {
m_numbers[0] = major;
m_numbers[1] = minor;
m_numbers[2] = patch;
m_numbers[3] = tweak;
}
std::optional<VersionNumber> VersionNumber::fromString(const std::string_view &s) {
VersionNumber res;
size_t index = 0;
size_t start = 0;
for (;;) {
if (index == res.m_numbers.size()) {
// More components than there are places to put them, which is a version of
// something else rather than a longer version of this.
return std::nullopt;
}
auto stop = s.find('.', start);
auto segment = s.substr(start, stop == std::string_view::npos ? stop : stop - start);
// The same answer comes out of from_chars, which refuses an empty range. This is
// here so that a default constructed string_view, whose data() is null, is not the
// range handed to it.
if (segment.empty()) {
return std::nullopt;
}
// Checked here rather than left to from_chars, which reads a leading minus into an
// int quite happily and would make "1.-2" a version.
for (char c : segment) {
if (c < '0' || c > '9') {
return std::nullopt;
}
}
// Digits throughout, so the only thing left for from_chars to refuse is a number
// too big for an int, and there is nothing it can leave behind.
auto answer = std::from_chars(segment.data(), segment.data() + segment.size(),
res.m_numbers[index]);
if (answer.ec != std::errc{}) {
return std::nullopt;
}
index++;
if (stop == std::string_view::npos) {
return res;
}
start = stop + 1;
}
}
std::string VersionNumber::toString() const {
if (tweak() != 0) {
return std::to_string(major()) + "." + std::to_string(minor()) + "." +
std::to_string(patch()) + "." + std::to_string(tweak());
}
if (patch() != 0) {
return std::to_string(major()) + "." + std::to_string(minor()) + "." +
std::to_string(patch());
}
return std::to_string(major()) + "." + std::to_string(minor());
}
bool VersionNumber::isEmpty() const {
return major() == 0 && minor() == 0 && patch() == 0 && tweak() == 0;
}
bool VersionNumber::operator==(const VersionNumber &RHS) const {
return major() == RHS.major() && minor() == RHS.minor() && patch() == RHS.patch() &&
tweak() == RHS.tweak();
}
bool VersionNumber::operator!=(const VersionNumber &RHS) const {
return !(*this == RHS);
}
bool VersionNumber::operator<(const VersionNumber &RHS) const {
if (major() < RHS.major())
return true;
if (major() > RHS.major())
return false;
if (minor() < RHS.minor())
return true;
if (minor() > RHS.minor())
return false;
if (patch() < RHS.patch())
return true;
if (patch() > RHS.patch())
return false;
return tweak() < RHS.tweak();
}
bool VersionNumber::operator>(const VersionNumber &RHS) const {
if (major() > RHS.major())
return true;
if (major() < RHS.major())
return false;
if (minor() > RHS.minor())
return true;
if (minor() < RHS.minor())
return false;
if (patch() > RHS.patch())
return true;
if (patch() < RHS.patch())
return false;
return tweak() > RHS.tweak();
}
bool VersionNumber::operator<=(const VersionNumber &RHS) const {
return !(*this > RHS);
}
bool VersionNumber::operator>=(const VersionNumber &RHS) const {
return !(*this < RHS);
}
}
namespace std {
size_t hash<stdc::VersionNumber>::operator()(const stdc::VersionNumber &key) const {
// An arbitrary starting value, so that a version of all zeroes does not hash to zero.
// It used to be typeid(key).hash_code(), which for a type with no virtual functions is a
// constant the compiler already knows, so it bought nothing.
size_t seed = 0x9E3779B9u; // fits a 32 bit size_t too
seed = stdc::hash(key.major(), seed);
seed = stdc::hash(key.minor(), seed);
seed = stdc::hash(key.patch(), seed);
seed = stdc::hash(key.tweak(), seed);
return seed;
}
}