forked from janhq/cortex.cpp
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathsemantic_version_utils.h
More file actions
61 lines (52 loc) · 1.21 KB
/
semantic_version_utils.h
File metadata and controls
61 lines (52 loc) · 1.21 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
#include <trantor/utils/Logger.h>
#include <sstream>
namespace semantic_version_utils {
struct SemVer {
int major;
int minor;
int patch;
};
inline SemVer SplitVersion(const std::string& version) {
if (version.empty()) {
LOG_WARN << "Passed in version is empty!";
}
SemVer semVer = {0, 0, 0}; // default value
std::stringstream ss(version);
std::string part;
int index = 0;
while (std::getline(ss, part, '.') && index < 3) {
int value = std::stoi(part);
switch (index) {
case 0:
semVer.major = value;
break;
case 1:
semVer.minor = value;
break;
case 2:
semVer.patch = value;
break;
}
++index;
}
return semVer;
}
inline int CompareSemanticVersion(const std::string& version1,
const std::string& version2) {
SemVer v1 = SplitVersion(version1);
SemVer v2 = SplitVersion(version2);
if (v1.major < v2.major)
return -1;
if (v1.major > v2.major)
return 1;
if (v1.minor < v2.minor)
return -1;
if (v1.minor > v2.minor)
return 1;
if (v1.patch < v2.patch)
return -1;
if (v1.patch > v2.patch)
return 1;
return 0;
}
} // namespace semantic_version_utils