Limit to the first 4 digits when parsing years badly encoded as YYYYMMDD
This commit is contained in:
@@ -336,19 +336,19 @@ namespace MetaData
|
|||||||
if (const Wt::WDate date{ Utils::parseDate(value) }; date.isValid())
|
if (const Wt::WDate date{ Utils::parseDate(value) }; date.isValid())
|
||||||
track.date = date;
|
track.date = date;
|
||||||
else if (!track.year)
|
else if (!track.year)
|
||||||
track.year = StringUtils::readAs<int>(value);
|
track.year = Utils::parseYear(value);
|
||||||
}
|
}
|
||||||
else if (tag == "YEAR")
|
else if (tag == "YEAR")
|
||||||
track.year = StringUtils::readAs<int>(value);
|
track.year = Utils::parseYear(value);
|
||||||
else if (tag == "ORIGINALDATE")
|
else if (tag == "ORIGINALDATE")
|
||||||
{
|
{
|
||||||
if (const Wt::WDate date{ Utils::parseDate(value) }; date.isValid())
|
if (const Wt::WDate date{ Utils::parseDate(value) }; date.isValid())
|
||||||
track.originalDate = date;
|
track.originalDate = date;
|
||||||
else if (!track.originalYear)
|
else if (!track.originalYear)
|
||||||
track.originalYear = StringUtils::readAs<int>(value);
|
track.originalYear = Utils::parseYear(value);
|
||||||
}
|
}
|
||||||
else if (tag == "ORIGINALYEAR")
|
else if (tag == "ORIGINALYEAR")
|
||||||
track.originalYear = StringUtils::readAs<int>(value);
|
track.originalYear = Utils::parseYear(value);
|
||||||
else if (tag == "METADATA_BLOCK_PICTURE")
|
else if (tag == "METADATA_BLOCK_PICTURE")
|
||||||
track.hasCover = true;
|
track.hasCover = true;
|
||||||
else if (tag == "COPYRIGHT")
|
else if (tag == "COPYRIGHT")
|
||||||
|
|||||||
@@ -64,6 +64,38 @@ namespace MetaData::Utils
|
|||||||
return {};
|
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)
|
std::string_view readStyleToString(ParserReadStyle readStyle)
|
||||||
{
|
{
|
||||||
switch (readStyle)
|
switch (readStyle)
|
||||||
|
|||||||
@@ -20,6 +20,7 @@
|
|||||||
|
|
||||||
#pragma once
|
#pragma once
|
||||||
|
|
||||||
|
#include <optional>
|
||||||
#include <string_view>
|
#include <string_view>
|
||||||
#include <Wt/WDate.h>
|
#include <Wt/WDate.h>
|
||||||
|
|
||||||
@@ -28,6 +29,7 @@
|
|||||||
namespace MetaData::Utils
|
namespace MetaData::Utils
|
||||||
{
|
{
|
||||||
Wt::WDate parseDate(std::string_view dateStr);
|
Wt::WDate parseDate(std::string_view dateStr);
|
||||||
|
std::optional<int> parseYear(std::string_view yearStr);
|
||||||
std::string_view readStyleToString(ParserReadStyle readStyle);
|
std::string_view readStyleToString(ParserReadStyle readStyle);
|
||||||
|
|
||||||
struct PerformerArtist
|
struct PerformerArtist
|
||||||
|
|||||||
@@ -76,6 +76,43 @@ TEST(MetaData, parseDate)
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
TEST(MetaData, parseYear)
|
||||||
|
{
|
||||||
|
using namespace MetaData::Utils;
|
||||||
|
|
||||||
|
struct TestCase
|
||||||
|
{
|
||||||
|
std::string str;
|
||||||
|
std::optional<int> result;
|
||||||
|
} testCases[]
|
||||||
|
{
|
||||||
|
{ "1995-05-09", 1995 },
|
||||||
|
{ "1995", 1995 },
|
||||||
|
{ "-0", 0 },
|
||||||
|
{ "0", 0 },
|
||||||
|
{ "00", 0 },
|
||||||
|
{ "05", 5 },
|
||||||
|
{ "050", 50 },
|
||||||
|
{ "00005", 0 },
|
||||||
|
{ "-50", -50 },
|
||||||
|
{ "-", std::nullopt },
|
||||||
|
{ "", std::nullopt },
|
||||||
|
{ "a", std::nullopt },
|
||||||
|
{ "1a", 1 },
|
||||||
|
{ "12a", 12 },
|
||||||
|
{ "123a", 123 },
|
||||||
|
{ "1234a", 1234 },
|
||||||
|
{ "19951123", 1995 },
|
||||||
|
{ "199511", 1995 },
|
||||||
|
};
|
||||||
|
|
||||||
|
for (const TestCase& testCase : testCases)
|
||||||
|
{
|
||||||
|
const std::optional<int> parsed{ parseYear(testCase.str) };
|
||||||
|
EXPECT_EQ(parsed, testCase.result) << " str was '" << testCase.str << "'";
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
TEST(MetaData, extractPerformerAndRole)
|
TEST(MetaData, extractPerformerAndRole)
|
||||||
{
|
{
|
||||||
using namespace MetaData::Utils;
|
using namespace MetaData::Utils;
|
||||||
|
|||||||
@@ -173,9 +173,12 @@ TEST(StringUtils, readAs_int)
|
|||||||
EXPECT_EQ(StringUtils::readAs<int>("-1"), -1);
|
EXPECT_EQ(StringUtils::readAs<int>("-1"), -1);
|
||||||
EXPECT_EQ(StringUtils::readAs<int>(""), std::nullopt);
|
EXPECT_EQ(StringUtils::readAs<int>(""), std::nullopt);
|
||||||
EXPECT_EQ(StringUtils::readAs<int>("a"), std::nullopt);
|
EXPECT_EQ(StringUtils::readAs<int>("a"), std::nullopt);
|
||||||
|
EXPECT_EQ(StringUtils::readAs<int>("-"), std::nullopt);
|
||||||
EXPECT_EQ(StringUtils::readAs<int>("1024-1"), 1024);
|
EXPECT_EQ(StringUtils::readAs<int>("1024-1"), 1024);
|
||||||
EXPECT_EQ(StringUtils::readAs<int>("1024-"), 1024);
|
EXPECT_EQ(StringUtils::readAs<int>("1024-"), 1024);
|
||||||
EXPECT_EQ(StringUtils::readAs<int>("1024/5"), 1024);
|
EXPECT_EQ(StringUtils::readAs<int>("1024/5"), 1024);
|
||||||
|
EXPECT_EQ(StringUtils::readAs<int>("1024a"), 1024);
|
||||||
|
EXPECT_EQ(StringUtils::readAs<int>("a1024a"), std::nullopt);
|
||||||
}
|
}
|
||||||
|
|
||||||
TEST(StringUtils, capitalize)
|
TEST(StringUtils, capitalize)
|
||||||
|
|||||||
Reference in New Issue
Block a user