Mind encoding in TagValue::toStandardGenreIndex()

This commit is contained in:
Martchus 2017-06-03 20:59:17 +02:00
parent 8ae3dd6f6f
commit 4e59ed2742
2 changed files with 15 additions and 7 deletions

View File

@ -205,17 +205,22 @@ int TagValue::toStandardGenreIndex() const
int index = 0;
switch(m_type) {
case TagDataType::Text: {
string s(m_ptr.get(), m_size);
const string s(toString());
try {
index = ConversionUtilities::stringToNumber<int32>(s);
} catch (ConversionException &) {
index = Id3Genres::indexFromString(s);
index = toInteger();
} catch (const ConversionException &) {
TagTextEncoding encoding = TagTextEncoding::Utf8;
if(m_encoding == TagTextEncoding::Latin1) {
// no need to convert Latin-1 to UTF-8 (makes no difference in case of genre strings)
encoding = TagTextEncoding::Unspecified;
}
index = Id3Genres::indexFromString(toString(encoding));
}
break;
} case TagDataType::StandardGenreIndex:
case TagDataType::Integer:
if(m_size == sizeof(int)) {
index = *reinterpret_cast<int *>(m_ptr.get());
if(m_size == sizeof(int32)) {
index = static_cast<int>(*reinterpret_cast<int32 *>(m_ptr.get()));
} else {
throw ConversionException("The assigned data is of unappropriate size.");
}

View File

@ -160,12 +160,15 @@ void TagValueTests::testString()
CPPUNIT_ASSERT_EQUAL_MESSAGE("conversion to int", 15, TagValue("\0\x31\0\x35", 4, TagTextEncoding::Utf16BigEndian).toInteger());
CPPUNIT_ASSERT_THROW_MESSAGE("failing conversion to int", TagValue("15ä", 4, TagTextEncoding::Utf8).toInteger(), ConversionException);
CPPUNIT_ASSERT_EQUAL_MESSAGE("conversion to pos", PositionInSet(4, 15), TagValue("4 / 15", 6, TagTextEncoding::Utf8).toPositionInSet());
CPPUNIT_ASSERT_EQUAL_MESSAGE("conversion to int", PositionInSet(15), TagValue("\0\x31\0\x35", 4, TagTextEncoding::Utf16BigEndian).toPositionInSet());
CPPUNIT_ASSERT_EQUAL_MESSAGE("conversion to pos", PositionInSet(15), TagValue("\0\x31\0\x35", 4, TagTextEncoding::Utf16BigEndian).toPositionInSet());
CPPUNIT_ASSERT_THROW_MESSAGE("failing conversion pos", TagValue("a4 / 15", 7, TagTextEncoding::Utf8).toPositionInSet(), ConversionException);
CPPUNIT_ASSERT_EQUAL_MESSAGE("conversion to date", DateTime::fromDate(2004, 4, 15), TagValue("2004-04-15", 10, TagTextEncoding::Utf8).toDateTime());
CPPUNIT_ASSERT_THROW_MESSAGE("failing conversion to date", TagValue("_", 1, TagTextEncoding::Utf8).toDateTime(), ConversionException);
CPPUNIT_ASSERT_EQUAL_MESSAGE("conversion to time span", TimeSpan::fromHours(1.5), TagValue("01:30:00", 10, TagTextEncoding::Utf8).toTimeSpan());
CPPUNIT_ASSERT_THROW_MESSAGE("failing conversion to time span", TagValue("_", 1, TagTextEncoding::Utf8).toTimeSpan(), ConversionException);
CPPUNIT_ASSERT_EQUAL_MESSAGE("conversion to genre from index", 15, TagValue("\0\x31\0\x35", 4, TagTextEncoding::Utf16BigEndian).toStandardGenreIndex());
CPPUNIT_ASSERT_EQUAL_MESSAGE("conversion to genre from name", 2, TagValue("Country", 7, TagTextEncoding::Latin1).toStandardGenreIndex());
CPPUNIT_ASSERT_THROW_MESSAGE("failing conversion to genre", TagValue("Kountry", 7, TagTextEncoding::Latin1).toStandardGenreIndex(), ConversionException);
}
void TagValueTests::testEqualityOperator()