2020-12-15 15:16:09 +03:00
|
|
|
#include "helper.h"
|
2020-02-25 09:44:13 +01:00
|
|
|
|
2020-12-15 15:16:09 +03:00
|
|
|
#include "../utils/string_utils.h"
|
2020-08-26 15:03:53 +03:00
|
|
|
|
2020-02-25 09:44:13 +01:00
|
|
|
#include <algorithm>
|
|
|
|
|
#include <sstream>
|
|
|
|
|
|
2020-08-26 15:03:53 +03:00
|
|
|
VersionHelper::VersionHelper(const size_t major, const size_t minor, const size_t revision) :
|
|
|
|
|
major{ major },
|
|
|
|
|
minor{ minor },
|
|
|
|
|
revision{ revision }
|
2020-02-25 09:44:13 +01:00
|
|
|
{
|
|
|
|
|
}
|
2020-04-27 13:39:47 +03:00
|
|
|
|
2021-08-12 14:53:51 +03:00
|
|
|
template<typename CharT>
|
|
|
|
|
struct Constants;
|
|
|
|
|
|
|
|
|
|
template<>
|
|
|
|
|
struct Constants<char>
|
|
|
|
|
{
|
2024-07-18 12:37:01 +02:00
|
|
|
static inline const char* LOWER_V = "v";
|
|
|
|
|
static inline const char* UPPER_V = "V";
|
2021-08-12 14:53:51 +03:00
|
|
|
static inline const char* DOT = ".";
|
|
|
|
|
static inline const char SPACE = ' ';
|
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
template<>
|
|
|
|
|
struct Constants<wchar_t>
|
|
|
|
|
{
|
2024-07-18 12:37:01 +02:00
|
|
|
static inline const wchar_t* LOWER_V = L"v";
|
|
|
|
|
static inline const wchar_t* UPPER_V = L"V";
|
2021-08-12 14:53:51 +03:00
|
|
|
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
|
|
|
|
|
{
|
2024-07-18 12:37:01 +02:00
|
|
|
str = left_trim<CharT>(trim<CharT>(str), Constants<CharT>::LOWER_V);
|
|
|
|
|
str = left_trim<CharT>(trim<CharT>(str), Constants<CharT>::UPPER_V);
|
2021-08-12 14:53:51 +03:00
|
|
|
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);
|
|
|
|
|
}
|
|
|
|
|
|
2020-04-27 13:39:47 +03:00
|
|
|
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;
|
|
|
|
|
}
|
2021-03-17 16:49:07 +03:00
|
|
|
|
|
|
|
|
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;
|
|
|
|
|
}
|