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
This commit is contained in:
Scott Wheeler 2008-02-12 03:20:30 +00:00
parent ee557e29d5
commit 12a884e122
4 changed files with 19 additions and 16 deletions

View File

@ -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();

View File

@ -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;
}

View File

@ -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 <class Vector>
@ -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<ByteVectorMirror>(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 <class T>

View File

@ -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));
}