From 12a884e1228378de89913d3ffcf603f6d24a6eb7 Mon Sep 17 00:00:00 2001 From: Scott Wheeler Date: Tue, 12 Feb 2008 03:20:30 +0000 Subject: [PATCH] Messed with things until the algorithm is correct. rfind now passes all unit tests. git-svn-id: svn://anonsvn.kde.org/home/kde/trunk/kdesupport/taglib@773913 283d02a7-25f6-0310-bc7c-ecb5cbfe19da --- taglib/ape/apetag.cpp | 2 +- taglib/mpeg/mpegfile.cpp | 13 ++++++------- taglib/toolkit/tbytevector.cpp | 16 ++++++++++------ tests/test_bytevector.cpp | 4 ++-- 4 files changed, 19 insertions(+), 16 deletions(-) diff --git a/taglib/ape/apetag.cpp b/taglib/ape/apetag.cpp index e6703ed4..2d628f92 100644 --- a/taglib/ape/apetag.cpp +++ b/taglib/ape/apetag.cpp @@ -244,7 +244,7 @@ ByteVector APE::Tag::render() const } d->footer.setItemCount(itemCount); - d->footer.setTagSize(data.size()+Footer::size()); + d->footer.setTagSize(data.size() + Footer::size()); d->footer.setHeaderPresent(true); return d->footer.renderHeader() + data + d->footer.renderFooter(); diff --git a/taglib/mpeg/mpegfile.cpp b/taglib/mpeg/mpegfile.cpp index 704a5fba..f69fd20f 100644 --- a/taglib/mpeg/mpegfile.cpp +++ b/taglib/mpeg/mpegfile.cpp @@ -212,7 +212,8 @@ bool MPEG::File::save(int tags, bool stripOthers) if(d->hasAPE) insert(APETag()->render(), d->APELocation, d->APEOriginalSize); else { - if(d->hasID3v1) { + if(d->hasID3v1) { + debug("inserting ape tag before id3v1 tag"); insert(APETag()->render(), d->ID3v1Location, 0); d->APEOriginalSize = APETag()->footer()->completeTagSize(); d->hasAPE = true; @@ -561,15 +562,13 @@ long MPEG::File::findID3v1() long MPEG::File::findAPE() { if(isValid()) { - if (d->hasID3v1) - seek(-160, End); - else - seek(-32, End); - + seek(d->hasID3v1 ? -160 : -32, End); long p = tell(); - if(readBlock(8) == APE::Tag::fileIdentifier()) + if(readBlock(8) == APE::Tag::fileIdentifier()) { + debug("found ape at " + String::number(int(p))); return p; + } } return -1; } diff --git a/taglib/toolkit/tbytevector.cpp b/taglib/toolkit/tbytevector.cpp index e180e908..59da5192 100644 --- a/taglib/toolkit/tbytevector.cpp +++ b/taglib/toolkit/tbytevector.cpp @@ -89,7 +89,7 @@ namespace TagLib { }; /*! - * A templatized find that works both with a ByteVector and a ByteVectorMirror. + * A templatized KMP find that works both with a ByteVector and a ByteVectorMirror. */ template @@ -146,6 +146,7 @@ namespace TagLib { { public: ByteVectorMirror(const ByteVector &source) : v(source) {} + const char operator[](int index) const { return v[v.size() - index - 1]; @@ -170,6 +171,12 @@ namespace TagLib { { ByteVectorMirror v(*this); + if(offset > 0) { + offset = size() - offset - pattern.size(); + if(offset >= size()) + offset = 0; + } + const int pos = vectorFind(v, pattern, offset, byteAlign); // If the offset is zero then we need to adjust the location in the search @@ -184,14 +191,11 @@ namespace TagLib { if(pos == -1) return -1; - if(offset == 0) - return size() - pos - pattern.size(); - else - return pos - offset; + return size() - pos - pattern.size(); } private: - const ByteVector v; + const ByteVector &v; }; template diff --git a/tests/test_bytevector.cpp b/tests/test_bytevector.cpp index 0a343e31..f92fce01 100644 --- a/tests/test_bytevector.cpp +++ b/tests/test_bytevector.cpp @@ -139,7 +139,7 @@ public: void testRfind1() { - CPPUNIT_ASSERT_EQUAL(-1, ByteVector(".OggS....").rfind("OggS", 0)); + CPPUNIT_ASSERT_EQUAL(1, ByteVector(".OggS....").rfind("OggS", 0)); CPPUNIT_ASSERT_EQUAL(1, ByteVector(".OggS....").rfind("OggS", 1)); CPPUNIT_ASSERT_EQUAL(1, ByteVector(".OggS....").rfind("OggS", 2)); CPPUNIT_ASSERT_EQUAL(1, ByteVector(".OggS....").rfind("OggS", 3)); @@ -168,7 +168,7 @@ public: CPPUNIT_ASSERT_EQUAL(0, r3.find("OggS")); CPPUNIT_ASSERT_EQUAL(10, r3.rfind("OggS")); CPPUNIT_ASSERT_EQUAL(10, r4.rfind("OggS")); - CPPUNIT_ASSERT_EQUAL(0, r4.rfind("OggS", 0)); + CPPUNIT_ASSERT_EQUAL(10, r4.rfind("OggS", 0)); CPPUNIT_ASSERT_EQUAL(5, r4.rfind("OggS", 7)); CPPUNIT_ASSERT_EQUAL(10, r4.rfind("OggS", 12)); }