Limit to the first 4 digits when parsing years badly encoded as YYYYMMDD

This commit is contained in:
emeric
2024-02-02 22:58:37 +01:00
parent d3aa11714a
commit 5db91fc5b5
5 changed files with 78 additions and 4 deletions
+32
View File
@@ -64,6 +64,38 @@ namespace MetaData::Utils
return {};
}
std::optional<int> parseYear(std::string_view yearStr)
{
// limit to first 4 digit, accept leading '-'
if (yearStr.empty())
return std::nullopt;
int sign;
if (yearStr.front() == '-')
{
sign = -1;
yearStr.remove_prefix(1);
}
else
{
sign = 1;
}
if (yearStr.empty() || !std::isdigit(yearStr.front()))
return std::nullopt;
int result{};
for (std::size_t i{}; i < yearStr.size() && i < 4; ++i)
{
if (!std::isdigit(yearStr[i])) {
break;
}
result = result * 10 + (yearStr[i] - '0');
}
return result * sign;
}
std::string_view readStyleToString(ParserReadStyle readStyle)
{
switch (readStyle)