update Duration::Parse()

This commit is contained in:
Nomango 2019-09-14 11:51:52 +08:00
parent ab9c650b0a
commit 5ef219ad89
1 changed files with 5 additions and 20 deletions

View File

@ -97,7 +97,7 @@ namespace kiwano
namespace namespace
{ {
const auto duration_regex = std::wregex(LR"([-+]?([0-9]*(\.[0-9]*)?[a-z]+)+)"); const auto duration_regex = std::wregex(LR"(^[-+]?([0-9]*(\.[0-9]*)?(h|m|s|ms)+)+$)");
typedef std::unordered_map<String, Duration> UnitMap; typedef std::unordered_map<String, Duration> UnitMap;
const auto unit_map = UnitMap const auto unit_map = UnitMap
@ -172,14 +172,7 @@ namespace kiwano
if (ms != 0) if (ms != 0)
{ {
auto float_to_str = [](Float32 val) -> String result.append(String::parse(static_cast<Float32>(sec) + static_cast<Float32>(ms) / 1000.f))
{
WChar buf[10] = {};
::swprintf_s(buf, L"%g", val);
return String(buf);
};
result.append(float_to_str(static_cast<Float32>(sec) + static_cast<Float32>(ms) / 1000.f))
.append(L"s"); .append(L"s");
} }
else if (sec != 0) else if (sec != 0)
@ -400,11 +393,7 @@ namespace kiwano
String num_str = str.substr(pos, i - pos); String num_str = str.substr(pos, i - pos);
pos = i; pos = i;
if (num_str.empty() || num_str == L".") KGE_ASSERT(!(num_str.empty() || num_str == L".") && "Duration::Parse failed, invalid duration");
{
KGE_ERROR_LOG(L"Duration::Parse failed, invalid duration");
return Duration();
}
// µ¥Î» // µ¥Î»
for (; i < len; ++i) for (; i < len; ++i)
@ -419,11 +408,7 @@ namespace kiwano
String unit_str = str.substr(pos, i - pos); String unit_str = str.substr(pos, i - pos);
pos = i; pos = i;
if (unit_map.find(unit_str) == unit_map.end()) KGE_ASSERT(unit_map.find(unit_str) != unit_map.end() && "Duration::Parse failed, invalid duration");
{
KGE_ERROR_LOG(L"Duration::Parse failed, invalid duration");
return Duration();
}
Float64 num = std::wcstod(num_str.c_str(), nullptr); Float64 num = std::wcstod(num_str.c_str(), nullptr);
Duration unit = unit_map.at(unit_str); Duration unit = unit_map.at(unit_str);
@ -437,4 +422,4 @@ namespace kiwano
return d; return d;
} }
} }
} }