From f5b95f4e28488292231bc3184ff49d0de343b317 Mon Sep 17 00:00:00 2001 From: Jaifroid Date: Mon, 26 Nov 2018 17:34:26 +0000 Subject: [PATCH] Fix incomplete searches in some ZIM files Former-commit-id: cb0cd14bd44a005d0a1fa784847ac7fe41bfca37 [formerly 17a97e25963e921a257b4de53b2e5c8df90e63ad] Former-commit-id: f4666707e53b4478de6f0efeb0d8eba1e0e18da1 --- www/js/lib/util.js | 11 ++++++++--- www/js/lib/zimArchive.js | 19 ++++++++++++++----- 2 files changed, 22 insertions(+), 8 deletions(-) diff --git a/www/js/lib/util.js b/www/js/lib/util.js index 3154dd60..3e2ddb4e 100644 --- a/www/js/lib/util.js +++ b/www/js/lib/util.js @@ -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; }); diff --git a/www/js/lib/zimArchive.js b/www/js/lib/zimArchive.js index ed9d613a..74bd9836 100644 --- a/www/js/lib/zimArchive.js +++ b/www/js/lib/zimArchive.js @@ -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 = [];