From f33604580cda795ee0a9aa232cb572a0310ba752 Mon Sep 17 00:00:00 2001 From: David Sansome Date: Thu, 3 Jun 2010 15:16:15 +0000 Subject: [PATCH] Stop ASCII from voting, and add a failing test --- src/core/song.cpp | 9 +++++++-- src/core/song.h | 2 +- tests/song_test.cpp | 22 ++++++++++++++++++++++ 3 files changed, 30 insertions(+), 3 deletions(-) diff --git a/src/core/song.cpp b/src/core/song.cpp index 09e9f56da..c83adee1d 100644 --- a/src/core/song.cpp +++ b/src/core/song.cpp @@ -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(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(); diff --git a/src/core/song.h b/src/core/song.h index 7b1294568..17e2fb092 100644 --- a/src/core/song.h +++ b/src/core/song.h @@ -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; diff --git a/tests/song_test.cpp b/tests/song_test.cpp index 8d924e376..8435cf3bf 100644 --- a/tests/song_test.cpp +++ b/tests/song_test.cpp @@ -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