refactor(Core/Misc): add braces and impove codestyle (#6402)

This commit is contained in:
Kargatum
2021-06-25 00:50:18 +07:00
committed by GitHub
parent 33c271cc7c
commit 3c24b511f2
72 changed files with 1486 additions and 401 deletions

View File

@@ -41,10 +41,14 @@ namespace Acore::Impl::StringConvertImpl
str.remove_prefix(2);
}
else
{
base = 10;
}
if (str.empty())
{
return std::nullopt;
}
}
char const* const start = str.data();
@@ -53,14 +57,18 @@ namespace Acore::Impl::StringConvertImpl
T val;
std::from_chars_result const res = std::from_chars(start, end, val, base);
if ((res.ptr == end) && (res.ec == std::errc()))
{
return val;
}
else
{
return std::nullopt;
}
}
static std::string ToString(T val)
{
std::string buf(20,'\0'); /* 2^64 is 20 decimal characters, -(2^63) is 20 including the sign */
std::string buf(20, '\0'); /* 2^64 is 20 decimal characters, -(2^63) is 20 including the sign */
char* const start = buf.data();
char* const end = (start + buf.length());
std::to_chars_result const res = std::to_chars(start, end, val);
@@ -84,13 +92,17 @@ namespace Acore::Impl::StringConvertImpl
static Optional<uint64> FromString(std::string_view str, int base = 10)
{
if (str.empty())
{
return std::nullopt;
}
try
{
size_t n;
uint64 val = std::stoull(std::string(str), &n, base);
if (n != str.length())
{
return std::nullopt;
}
return val;
}
catch (...) { return std::nullopt; }
@@ -107,13 +119,18 @@ namespace Acore::Impl::StringConvertImpl
{
static Optional<int64> FromString(std::string_view str, int base = 10)
{
try {
try
{
if (str.empty())
{
return std::nullopt;
}
size_t n;
int64 val = std::stoll(std::string(str), &n, base);
if (n != str.length())
{
return std::nullopt;
}
return val;
}
catch (...) { return std::nullopt; }
@@ -134,17 +151,25 @@ namespace Acore::Impl::StringConvertImpl
if (strict)
{
if (str == "1")
{
return true;
}
if (str == "0")
{
return false;
}
return std::nullopt;
}
else
{
if ((str == "1") || StringEqualI(str, "y") || StringEqualI(str, "on") || StringEqualI(str, "yes") || StringEqualI(str, "true"))
{
return true;
}
if ((str == "0") || StringEqualI(str, "n") || StringEqualI(str, "off") || StringEqualI(str, "no") || StringEqualI(str, "false"))
{
return false;
}
return std::nullopt;
}
}
@@ -162,7 +187,9 @@ namespace Acore::Impl::StringConvertImpl
static Optional<T> FromString(std::string_view str, std::chars_format fmt = std::chars_format())
{
if (str.empty())
{
return std::nullopt;
}
if (fmt == std::chars_format())
{
@@ -172,10 +199,14 @@ namespace Acore::Impl::StringConvertImpl
str.remove_prefix(2);
}
else
{
fmt = std::chars_format::general;
}
if (str.empty())
{
return std::nullopt;
}
}
char const* const start = str.data();
@@ -184,20 +215,30 @@ namespace Acore::Impl::StringConvertImpl
T val;
std::from_chars_result const res = std::from_chars(start, end, val, fmt);
if ((res.ptr == end) && (res.ec == std::errc()))
{
return val;
}
else
{
return std::nullopt;
}
}
// this allows generic converters for all numeric types (easier templating!)
static Optional<T> FromString(std::string_view str, int base)
{
if (base == 16)
{
return FromString(str, std::chars_format::hex);
}
else if (base == 10)
{
return FromString(str, std::chars_format::general);
}
else
{
return FromString(str, std::chars_format());
}
}
static std::string ToString(T val)
@@ -212,22 +253,31 @@ namespace Acore::Impl::StringConvertImpl
{
static Optional<T> FromString(std::string_view str, int base = 0)
{
try {
try
{
if (str.empty())
{
return std::nullopt;
}
if ((base == 10) && StringEqualI(str.substr(0, 2), "0x"))
{
return std::nullopt;
}
std::string tmp;
if (base == 16)
{
tmp.append("0x");
}
tmp.append(str);
size_t n;
T val = static_cast<T>(std::stold(tmp, &n));
if (n != tmp.length())
{
return std::nullopt;
}
return val;
}
catch (...) { return std::nullopt; }