Merge pull request #484 from TsudaKageyu/fix-rfind

Fix File::rfind() for small files.
This commit is contained in:
Stephen F. Booth
2015-05-21 22:44:10 -04:00
3 changed files with 127 additions and 15 deletions

View File

@ -278,7 +278,7 @@ long File::find(const ByteVector &pattern, long fromOffset, const ByteVector &be
// (2) The search pattern is wholly contained within the current buffer.
//
// (3) The current buffer ends with a partial match of the pattern. We will
// note this for use in the next itteration, where we will check for the rest
// note this for use in the next iteration, where we will check for the rest
// of the pattern.
//
// All three of these are done in two steps. First we check for the pattern
@ -363,25 +363,34 @@ long File::rfind(const ByteVector &pattern, long fromOffset, const ByteVector &b
// Start the search at the offset.
long bufferOffset;
if(fromOffset == 0) {
seek(-1 * int(bufferSize()), End);
bufferOffset = tell();
}
else {
seek(fromOffset + -1 * int(bufferSize()), Beginning);
bufferOffset = tell();
}
if(fromOffset == 0)
fromOffset = length();
long bufferLength = bufferSize();
long bufferOffset = fromOffset + pattern.size();
// See the notes in find() for an explanation of this algorithm.
for(buffer = readBlock(bufferSize()); buffer.size() > 0; buffer = readBlock(bufferSize())) {
while(true) {
if(bufferOffset > bufferLength) {
bufferOffset -= bufferLength;
}
else {
bufferLength = bufferOffset;
bufferOffset = 0;
}
seek(bufferOffset);
buffer = readBlock(bufferLength);
if(buffer.isEmpty())
break;
// TODO: (1) previous partial match
// (2) pattern contained in current buffer
long location = buffer.rfind(pattern);
const long location = buffer.rfind(pattern);
if(location >= 0) {
seek(originalPosition);
return bufferOffset + location;
@ -393,9 +402,6 @@ long File::rfind(const ByteVector &pattern, long fromOffset, const ByteVector &b
}
// TODO: (3) partial match
bufferOffset -= bufferSize();
seek(bufferOffset);
}
// Since we hit the end of the file, reset the status before continuing.