Add includesSorted function, use for faster searches of sorted arrays.

This commit is contained in:
David Anson 2019-03-28 22:06:42 -07:00
parent d7c0d195d7
commit 9b9532e163
8 changed files with 86 additions and 50 deletions

View file

@ -71,6 +71,23 @@ module.exports.isBlankLine = function isBlankLine(line) {
return !line || !line.trim() || !line.replace(blankLineRe, "").trim();
};
// Returns true iff the sorted array contains the specified element
module.exports.includesSorted = function includesSorted(array, element) {
let left = 0;
let right = array.length - 1;
while (left <= right) {
const mid = (left + right) >> 1;
if (array[mid] < element) {
left = mid + 1;
} else if (array[mid] > element) {
right = mid - 1;
} else {
return true;
}
}
return false;
};
// Replaces the text of all properly-formatted HTML comments with whitespace
// This preserves the line/column information for the rest of the document
// Trailing whitespace is avoided with a '\' character in the last column
@ -89,7 +106,7 @@ module.exports.clearHtmlCommentText = function clearHtmlCommentText(text) {
if ((comment.length > 0) &&
(comment[0] !== ">") &&
(comment[comment.length - 1] !== "-") &&
(comment.indexOf("--") === -1) &&
!comment.includes("--") &&
(text.slice(i, j + htmlCommentEnd.length)
.search(inlineCommentRe) === -1)) {
const blanks = comment