-
Notifications
You must be signed in to change notification settings - Fork 2
Expand file tree
/
Copy pathhelper.cpp
More file actions
89 lines (77 loc) · 1.98 KB
/
helper.cpp
File metadata and controls
89 lines (77 loc) · 1.98 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
#include "helper.h"
#include "../utils/string_utils.h"
#include <algorithm>
#include <sstream>
VersionHelper::VersionHelper(const size_t major, const size_t minor, const size_t revision) :
major{ major },
minor{ minor },
revision{ revision }
{
}
template<typename CharT>
struct Constants;
template<>
struct Constants<char>
{
static inline const char* V = "v";
static inline const char* DOT = ".";
static inline const char SPACE = ' ';
};
template<>
struct Constants<wchar_t>
{
static inline const wchar_t* V = L"v";
static inline const wchar_t* DOT = L".";
static inline const wchar_t SPACE = L' ';
};
template<typename CharT>
std::optional<VersionHelper> fromString(std::basic_string_view<CharT> str)
{
try
{
str = left_trim<CharT>(trim<CharT>(str), Constants<CharT>::V);
std::basic_string<CharT> spacedStr{ str };
replace_chars<CharT>(spacedStr, Constants<CharT>::DOT, Constants<CharT>::SPACE);
std::basic_istringstream<CharT> ss{ spacedStr };
VersionHelper result{ 0, 0, 0 };
ss >> result.major;
ss >> result.minor;
ss >> result.revision;
if (!ss.fail() && ss.eof())
{
return result;
}
}
catch (...)
{
}
return std::nullopt;
}
std::optional<VersionHelper> VersionHelper::fromString(std::string_view s)
{
return ::fromString(s);
}
std::optional<VersionHelper> VersionHelper::fromString(std::wstring_view s)
{
return ::fromString(s);
}
std::wstring VersionHelper::toWstring() const
{
std::wstring result{ L"v" };
result += std::to_wstring(major);
result += L'.';
result += std::to_wstring(minor);
result += L'.';
result += std::to_wstring(revision);
return result;
}
std::string VersionHelper::toString() const
{
std::string result{ "v" };
result += std::to_string(major);
result += '.';
result += std::to_string(minor);
result += '.';
result += std::to_string(revision);
return result;
}