mirror of
https://github.com/microsoft/PowerToys.git
synced 2026-04-04 02:06:36 +02:00
[Updating] Add versioning to UpdateState.json (#12744)
- fix "Check for updates" hang if UpdateState.json was deleted while the page is open
This commit is contained in:
@@ -5,23 +5,6 @@
|
||||
#include <algorithm>
|
||||
#include <sstream>
|
||||
|
||||
VersionHelper::VersionHelper(std::string str)
|
||||
{
|
||||
// Remove whitespaces chars and a leading 'v'
|
||||
str = left_trim<char>(trim<char>(str), "v");
|
||||
// Replace '.' with spaces
|
||||
replace_chars(str, ".", ' ');
|
||||
|
||||
std::istringstream ss{ str };
|
||||
ss >> major;
|
||||
ss >> minor;
|
||||
ss >> revision;
|
||||
if (ss.fail() || !ss.eof())
|
||||
{
|
||||
throw std::logic_error("VersionHelper: couldn't parse the supplied version string");
|
||||
}
|
||||
}
|
||||
|
||||
VersionHelper::VersionHelper(const size_t major, const size_t minor, const size_t revision) :
|
||||
major{ major },
|
||||
minor{ minor },
|
||||
@@ -29,6 +12,60 @@ VersionHelper::VersionHelper(const size_t major, const size_t minor, const size_
|
||||
{
|
||||
}
|
||||
|
||||
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" };
|
||||
|
||||
@@ -1,14 +1,17 @@
|
||||
#pragma once
|
||||
|
||||
#include <string>
|
||||
#include <optional>
|
||||
#include <compare>
|
||||
|
||||
struct VersionHelper
|
||||
{
|
||||
VersionHelper(std::string str);
|
||||
VersionHelper(const size_t major, const size_t minor, const size_t revision);
|
||||
|
||||
auto operator<=>(const VersionHelper&) const = default;
|
||||
|
||||
static std::optional<VersionHelper> fromString(std::string_view s);
|
||||
static std::optional<VersionHelper> fromString(std::wstring_view s);
|
||||
|
||||
size_t major;
|
||||
size_t minor;
|
||||
|
||||
Reference in New Issue
Block a user