1
0
mirror of https://github.com/clementine-player/Clementine synced 2025-01-28 18:19:42 +01:00

Stop ASCII from voting, and add a failing test

This commit is contained in:
David Sansome 2010-06-03 15:16:15 +00:00
parent 3652ff30e0
commit f33604580c
3 changed files with 30 additions and 3 deletions

View File

@ -171,6 +171,7 @@ QTextCodec* UniversalEncodingHandler::Guess(const TagLib::FileRef& fileref) {
Guess(tag.album(), &usages);
Guess(tag.comment(), &usages);
Guess(tag.genre(), &usages);
if (TagLib::MPEG::File* file = dynamic_cast<TagLib::MPEG::File*>(fileref.file())) {
if (file->ID3v2Tag()) {
if (!file->ID3v2Tag()->frameListMap()["TCOM"].isEmpty())
@ -204,8 +205,12 @@ void UniversalEncodingHandler::Guess(const TagLib::String& input,
if (input.isEmpty()) {
return; // Empty strings don't vote.
}
QTextCodec* codec = Guess(input);
++(*usages)[codec]; // Qt automatically initialises ints to 0.
if (codec) {
// Ascii doesn't vote either
++(*usages)[codec]; // Qt automatically initialises ints to 0.
}
}
QTextCodec* UniversalEncodingHandler::Guess(const TagLib::String& input) {
@ -299,7 +304,7 @@ void Song::Init(const QString& title, const QString& artist, const QString& albu
d->length_ = length;
}
QString Song::Decode(const TagLib::String& tag, const QTextCodec* codec) const {
QString Song::Decode(const TagLib::String& tag, const QTextCodec* codec) {
if (codec) {
const std::string fixed = QString::fromUtf8(tag.toCString(true)).toStdString();
return codec->toUnicode(fixed.c_str()).trimmed();

View File

@ -116,7 +116,7 @@ class Song {
void InitFromLastFM(const lastfm::Track& track);
void MergeFromSimpleMetaBundle(const Engine::SimpleMetaBundle& bundle);
QString Decode(const TagLib::String& tag, const QTextCodec* codec) const;
static QString Decode(const TagLib::String& tag, const QTextCodec* codec);
// Save
void BindToQuery(QSqlQuery* query) const;

View File

@ -156,4 +156,26 @@ TEST_F(SongTest, TakesMajorityVote) {
EXPECT_EQ(QTextCodec::codecForName("windows-1251"), handler.Guess(ref));
}
TEST_F(SongTest, DecodesLatin1AsUtf8) {
const char utf8[] = { 0xe2, 0x80, 0x99, 0x00 };
TagLib::String str(utf8, TagLib::String::Latin1);
QTextCodec* codec = QTextCodec::codecForName("UTF-8");
QString fixed = Song::Decode(str, codec);
ASSERT_EQ(1, fixed.length());
EXPECT_EQ(QString::fromUtf8(""), fixed);
}
TEST_F(SongTest, DecodesUtf8AsUtf8) {
const char utf8[] = { 0xe2, 0x80, 0x99, 0x00 };
TagLib::String str(utf8, TagLib::String::UTF8);
QTextCodec* codec = QTextCodec::codecForName("UTF-8");
QString fixed = Song::Decode(str, codec);
ASSERT_EQ(1, fixed.length());
EXPECT_EQ(QString::fromUtf8(""), fixed);
}
} // namespace