mirror of
https://github.com/kiwix/kiwix-js.git
synced 2025-09-22 03:52:21 -04:00
Fix for the title search algorithm (little regression introduced by the pull request 66)
+ some cosmetic changes
This commit is contained in:
parent
84f5ab62a3
commit
26f0f5fbf8
@ -346,7 +346,7 @@ define(function(require) {
|
||||
var normalize = this.getNormalizeFunction();
|
||||
var normalizedTitleName = normalize(titleName);
|
||||
|
||||
titleIterators.FindPrefixOffset(this.titleFile, titleName, normalize).then(function(offset) {
|
||||
titleIterators.findPrefixOffset(this.titleFile, titleName, normalize).then(function(offset) {
|
||||
var iterator = new titleIterators.SequentialTitleIterator(that, offset);
|
||||
function check(title) {
|
||||
if (title == null || normalize(title.name) !== normalizedTitleName) {
|
||||
@ -390,7 +390,7 @@ define(function(require) {
|
||||
var normalize = this.getNormalizeFunction();
|
||||
prefix = normalize(prefix);
|
||||
|
||||
titleIterators.FindPrefixOffset(this.titleFile, prefix, normalize).then(function(offset) {
|
||||
titleIterators.findPrefixOffset(this.titleFile, prefix, normalize).then(function(offset) {
|
||||
var iterator = new titleIterators.SequentialTitleIterator(that, offset);
|
||||
function addNext() {
|
||||
if (titles.length >= maxSize) {
|
||||
|
@ -72,12 +72,12 @@ define(['utf8', 'title', 'util', 'jquery'], function(utf8, evopediaTitle, util,
|
||||
* @param normalize function to be applied to every title before comparison
|
||||
* @returns jQuery promise giving the offset
|
||||
*/
|
||||
function FindPrefixOffset(titleFile, prefix, normalize) {
|
||||
function findPrefixOffset(titleFile, prefix, normalize) {
|
||||
prefix = normalize(prefix);
|
||||
var lo = 0;
|
||||
var hi = titleFile.size;
|
||||
var iterate = function() {
|
||||
if (lo >= hi) {
|
||||
if (lo >= hi - 1) {
|
||||
if (lo > 0)
|
||||
lo += 2; // Let lo point to the start of an entry
|
||||
return jQuery.when(lo);
|
||||
@ -121,6 +121,6 @@ define(['utf8', 'title', 'util', 'jquery'], function(utf8, evopediaTitle, util,
|
||||
*/
|
||||
return {
|
||||
SequentialTitleIterator : SequentialTitleIterator,
|
||||
FindPrefixOffset : FindPrefixOffset
|
||||
findPrefixOffset : findPrefixOffset
|
||||
};
|
||||
});
|
||||
|
@ -1,148 +1,151 @@
|
||||
/**
|
||||
* util.js : Utility functions
|
||||
*
|
||||
* Copyright 2013 Mossroy
|
||||
* License GPL v3:
|
||||
*
|
||||
* This file is part of Evopedia.
|
||||
*
|
||||
* Evopedia is free software: you can redistribute it and/or modify
|
||||
* it under the terms of the GNU General Public License as published by
|
||||
* the Free Software Foundation, either version 3 of the License, or
|
||||
* (at your option) any later version.
|
||||
*
|
||||
* Evopedia is distributed in the hope that it will be useful,
|
||||
* but WITHOUT ANY WARRANTY; without even the implied warranty of
|
||||
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
||||
* GNU General Public License for more details.
|
||||
*
|
||||
* You should have received a copy of the GNU General Public License
|
||||
* along with Evopedia (file LICENSE-GPLv3.txt). If not, see <http://www.gnu.org/licenses/>
|
||||
*/
|
||||
define(function(require) {
|
||||
var jQuery = require('jquery');
|
||||
|
||||
/**
|
||||
* Utility function : return true if the given string ends with the suffix
|
||||
* @param str
|
||||
* @param suffix
|
||||
* @returns {Boolean}
|
||||
*/
|
||||
function endsWith(str, suffix) {
|
||||
return str.indexOf(suffix, str.length - suffix.length) !== -1;
|
||||
}
|
||||
|
||||
/**
|
||||
* Read an integer encoded in 4 bytes, little endian
|
||||
* @param {type} byteArray
|
||||
* @param {type} firstIndex
|
||||
* @returns {Number}
|
||||
*/
|
||||
function readIntegerFrom4Bytes(byteArray, firstIndex) {
|
||||
var dataView = new DataView(byteArray.buffer, firstIndex, 4);
|
||||
var integer = dataView.getUint32(0, true);
|
||||
return integer;
|
||||
}
|
||||
|
||||
/**
|
||||
* Read an integer encoded in 2 bytes, little endian
|
||||
* @param {type} byteArray
|
||||
* @param {type} firstIndex
|
||||
* @returns {Number}
|
||||
*/
|
||||
function readIntegerFrom2Bytes(byteArray, firstIndex) {
|
||||
var dataView = new DataView(byteArray.buffer, firstIndex, 2);
|
||||
var integer = dataView.getUint16(0, true);
|
||||
return integer;
|
||||
}
|
||||
|
||||
/**
|
||||
* Read a float encoded in 2 bytes
|
||||
* @param {type} byteArray
|
||||
* @param {type} firstIndex
|
||||
* @param {bool} littleEndian (optional)
|
||||
* @returns {Number}
|
||||
*/
|
||||
function readFloatFrom4Bytes(byteArray, firstIndex, littleEndian) {
|
||||
var dataView = new DataView(byteArray.buffer, firstIndex, 4);
|
||||
var float = dataView.getFloat32(0, littleEndian);
|
||||
return float;
|
||||
}
|
||||
|
||||
/**
|
||||
* Convert a Uint8Array to a lowercase hex string
|
||||
* @param {type} byteArray
|
||||
* @returns {String}
|
||||
*/
|
||||
function uint8ArrayToHex(byteArray) {
|
||||
var s = '';
|
||||
var hexDigits = '0123456789abcdef';
|
||||
for (var i = 0; i < byteArray.length; i++) {
|
||||
var v = byteArray[i];
|
||||
s += hexDigits[(v & 0xff) >> 4];
|
||||
s += hexDigits[v & 0xf];
|
||||
}
|
||||
return s;
|
||||
}
|
||||
|
||||
/**
|
||||
* Convert a Uint8Array to base64
|
||||
* TODO : might be replaced by btoa() built-in function? https://developer.mozilla.org/en-US/docs/Web/API/window.btoa
|
||||
* @param {type} byteArray
|
||||
* @returns {String}
|
||||
*/
|
||||
function uint8ArrayToBase64(byteArray) {
|
||||
var b64 = "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789+/=";
|
||||
var bits, h1, h2, h3, h4, i = 0;
|
||||
var enc = "";
|
||||
|
||||
for (var i = 0; i < byteArray.length; ) {
|
||||
bits = byteArray[i++] << 16;
|
||||
bits |= byteArray[i++] << 8;
|
||||
bits |= byteArray[i++];
|
||||
|
||||
h1 = bits >> 18 & 0x3f;
|
||||
h2 = bits >> 12 & 0x3f;
|
||||
h3 = bits >> 6 & 0x3f;
|
||||
h4 = bits & 0x3f;
|
||||
|
||||
enc += b64[h1] + b64[h2] + b64[h3] + b64[h4];
|
||||
}
|
||||
|
||||
var r = byteArray.length % 3;
|
||||
|
||||
return (r > 0 ? enc.slice(0, r - 3) : enc) + '==='.slice(r || 3);
|
||||
}
|
||||
|
||||
/**
|
||||
* Reads a Uint8Array from the given file starting at byte offset begin and
|
||||
* not including byte offset end.
|
||||
* @returns jQuery promise
|
||||
*/
|
||||
function readFileSlice(file, begin, end) {
|
||||
var deferred = jQuery.Deferred();
|
||||
var reader = new FileReader();
|
||||
reader.onload = function(e) {
|
||||
deferred.resolve(new Uint8Array(e.target.result));
|
||||
}
|
||||
reader.onerror = reader.onabort = function(e) {
|
||||
deferred.reject(e);
|
||||
}
|
||||
reader.readAsArrayBuffer(file.slice(begin, end));
|
||||
return deferred.promise();
|
||||
}
|
||||
|
||||
|
||||
/**
|
||||
* Functions and classes exposed by this module
|
||||
*/
|
||||
return {
|
||||
endsWith: endsWith,
|
||||
readIntegerFrom4Bytes: readIntegerFrom4Bytes,
|
||||
readIntegerFrom2Bytes : readIntegerFrom2Bytes,
|
||||
readFloatFrom4Bytes : readFloatFrom4Bytes,
|
||||
uint8ArrayToHex : uint8ArrayToHex,
|
||||
uint8ArrayToBase64 : uint8ArrayToBase64,
|
||||
readFileSlice : readFileSlice
|
||||
};
|
||||
});
|
||||
/**
|
||||
* util.js : Utility functions
|
||||
*
|
||||
* Copyright 2013 Mossroy
|
||||
* License GPL v3:
|
||||
*
|
||||
* This file is part of Evopedia.
|
||||
*
|
||||
* Evopedia is free software: you can redistribute it and/or modify
|
||||
* it under the terms of the GNU General Public License as published by
|
||||
* the Free Software Foundation, either version 3 of the License, or
|
||||
* (at your option) any later version.
|
||||
*
|
||||
* Evopedia is distributed in the hope that it will be useful,
|
||||
* but WITHOUT ANY WARRANTY; without even the implied warranty of
|
||||
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
||||
* GNU General Public License for more details.
|
||||
*
|
||||
* You should have received a copy of the GNU General Public License
|
||||
* along with Evopedia (file LICENSE-GPLv3.txt). If not, see <http://www.gnu.org/licenses/>
|
||||
*/
|
||||
define(function(require) {
|
||||
var jQuery = require('jquery');
|
||||
|
||||
/**
|
||||
* Utility function : return true if the given string ends with the suffix
|
||||
* @param str
|
||||
* @param suffix
|
||||
* @returns {Boolean}
|
||||
*/
|
||||
function endsWith(str, suffix) {
|
||||
return str.indexOf(suffix, str.length - suffix.length) !== -1;
|
||||
}
|
||||
|
||||
/**
|
||||
* Read an integer encoded in 4 bytes, little endian
|
||||
* @param {type} byteArray
|
||||
* @param {type} firstIndex
|
||||
* @returns {Number}
|
||||
*/
|
||||
function readIntegerFrom4Bytes(byteArray, firstIndex) {
|
||||
var dataView = new DataView(byteArray.buffer, firstIndex, 4);
|
||||
var integer = dataView.getUint32(0, true);
|
||||
return integer;
|
||||
}
|
||||
|
||||
/**
|
||||
* Read an integer encoded in 2 bytes, little endian
|
||||
* @param {type} byteArray
|
||||
* @param {type} firstIndex
|
||||
* @returns {Number}
|
||||
*/
|
||||
function readIntegerFrom2Bytes(byteArray, firstIndex) {
|
||||
var dataView = new DataView(byteArray.buffer, firstIndex, 2);
|
||||
var integer = dataView.getUint16(0, true);
|
||||
return integer;
|
||||
}
|
||||
|
||||
/**
|
||||
* Read a float encoded in 2 bytes
|
||||
* @param {type} byteArray
|
||||
* @param {type} firstIndex
|
||||
* @param {bool} littleEndian (optional)
|
||||
* @returns {Number}
|
||||
*/
|
||||
function readFloatFrom4Bytes(byteArray, firstIndex, littleEndian) {
|
||||
var dataView = new DataView(byteArray.buffer, firstIndex, 4);
|
||||
var float = dataView.getFloat32(0, littleEndian);
|
||||
return float;
|
||||
}
|
||||
|
||||
/**
|
||||
* Convert a Uint8Array to a lowercase hex string
|
||||
* @param {type} byteArray
|
||||
* @returns {String}
|
||||
*/
|
||||
function uint8ArrayToHex(byteArray) {
|
||||
var s = '';
|
||||
var hexDigits = '0123456789abcdef';
|
||||
for (var i = 0; i < byteArray.length; i++) {
|
||||
var v = byteArray[i];
|
||||
s += hexDigits[(v & 0xff) >> 4];
|
||||
s += hexDigits[v & 0xf];
|
||||
}
|
||||
return s;
|
||||
}
|
||||
|
||||
/**
|
||||
* Convert a Uint8Array to base64
|
||||
* TODO : might be replaced by btoa() built-in function? https://developer.mozilla.org/en-US/docs/Web/API/window.btoa
|
||||
* @param {type} byteArray
|
||||
* @returns {String}
|
||||
*/
|
||||
function uint8ArrayToBase64(byteArray) {
|
||||
var b64 = "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789+/=";
|
||||
var bits, h1, h2, h3, h4, i = 0;
|
||||
var enc = "";
|
||||
|
||||
for (var i = 0; i < byteArray.length; ) {
|
||||
bits = byteArray[i++] << 16;
|
||||
bits |= byteArray[i++] << 8;
|
||||
bits |= byteArray[i++];
|
||||
|
||||
h1 = bits >> 18 & 0x3f;
|
||||
h2 = bits >> 12 & 0x3f;
|
||||
h3 = bits >> 6 & 0x3f;
|
||||
h4 = bits & 0x3f;
|
||||
|
||||
enc += b64[h1] + b64[h2] + b64[h3] + b64[h4];
|
||||
}
|
||||
|
||||
var r = byteArray.length % 3;
|
||||
|
||||
return (r > 0 ? enc.slice(0, r - 3) : enc) + '==='.slice(r || 3);
|
||||
}
|
||||
|
||||
/**
|
||||
* Reads a Uint8Array from the given file starting at byte offset begin and
|
||||
* not including byte offset end.
|
||||
* @param file
|
||||
* @param begin
|
||||
* @param end
|
||||
* @returns jQuery promise
|
||||
*/
|
||||
function readFileSlice(file, begin, end) {
|
||||
var deferred = jQuery.Deferred();
|
||||
var reader = new FileReader();
|
||||
reader.onload = function(e) {
|
||||
deferred.resolve(new Uint8Array(e.target.result));
|
||||
};
|
||||
reader.onerror = reader.onabort = function(e) {
|
||||
deferred.reject(e);
|
||||
};
|
||||
reader.readAsArrayBuffer(file.slice(begin, end));
|
||||
return deferred.promise();
|
||||
}
|
||||
|
||||
|
||||
/**
|
||||
* Functions and classes exposed by this module
|
||||
*/
|
||||
return {
|
||||
endsWith: endsWith,
|
||||
readIntegerFrom4Bytes: readIntegerFrom4Bytes,
|
||||
readIntegerFrom2Bytes : readIntegerFrom2Bytes,
|
||||
readFloatFrom4Bytes : readFloatFrom4Bytes,
|
||||
uint8ArrayToHex : uint8ArrayToHex,
|
||||
uint8ArrayToBase64 : uint8ArrayToBase64,
|
||||
readFileSlice : readFileSlice
|
||||
};
|
||||
});
|
||||
|
Loading…
x
Reference in New Issue
Block a user