Fix incomplete searches in some ZIM files

Former-commit-id: cb0cd14bd44a005d0a1fa784847ac7fe41bfca37 [formerly 17a97e25963e921a257b4de53b2e5c8df90e63ad]
Former-commit-id: f4666707e53b4478de6f0efeb0d8eba1e0e18da1
This commit is contained in:
Jaifroid 2018-11-26 17:34:26 +00:00
parent 6148e739f9
commit f5b95f4e28
2 changed files with 22 additions and 8 deletions

View File

@ -236,11 +236,16 @@ define(['q'], function (q) {
if (end <= begin)
return lowerBound ? begin : null;
var mid = Math.floor((begin + end) / 2);
return query(mid).then(function (decision) {
if (decision < 0)
return query(mid).then(function(decision)
{
if (decision == -1)
return binarySearch(begin, mid, query, lowerBound);
else if (decision > 0)
else if (decision == 1)
return binarySearch(mid + 1, end, query, lowerBound);
else if (decision == -2)
return binarySearch(begin, end - 1, query, lowerBound)
else if (decision == 2)
return binarySearch(begin + 1, end, query, lowerBound)
else
return mid;
});

View File

@ -180,13 +180,22 @@ define(['zimfile', 'zimDirEntry', 'util', 'utf8'],
*/
ZIMArchive.prototype.findDirEntriesWithPrefixCaseSensitive = function(prefix, resultSize, callback) {
var that = this;
// Vector is used to remember the search direction if we encounter a dirEntry with an empty title
var vector = -1;
util.binarySearch(0, this._file.articleCount, function(i) {
return that._file.dirEntryByTitleIndex(i).then(function(dirEntry) {
if (dirEntry.namespace < "A")
return 1;
else if (dirEntry.namespace > "A")
return -1;
return prefix <= dirEntry.title ? -1 : 1;
if (dirEntry.namespace < "A") vector = 1;
if (dirEntry.namespace > "A") vector = -1;
if (dirEntry.namespace !== "A") return vector;
// We should now be in namespace A
if (dirEntry.title) {
vector = prefix <= dirEntry.title ? -1 : 1;
return vector;
} else {
// Since there is no title, we must nudge the search algorith up or down
// We signal this to util.binarySearch by returning -2 or +2 instead of -1 or +1
return vector + vector;
}
});
}, true).then(function(firstIndex) {
var dirEntries = [];