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

@ -3,6 +3,8 @@
"use strict";
const shared = require("./shared");
const { addError, filterTokens, forEachLine, includesSorted, rangeFromRegExp,
trimRight } = shared;
const trailingSpaceRe = /\s+$/;
@ -20,24 +22,24 @@ module.exports = {
(listItemEmptyLines === undefined) ? false : !!listItemEmptyLines;
const listItemLineNumbers = [];
if (allowListItemEmptyLines) {
shared.filterTokens(params, "list_item_open", function forToken(token) {
filterTokens(params, "list_item_open", (token) => {
for (let i = token.map[0]; i < token.map[1]; i++) {
listItemLineNumbers.push(i + 1);
}
});
listItemLineNumbers.sort((a, b) => a - b);
}
const expected = (brSpaces < 2) ? 0 : brSpaces;
shared.forEachLine(function forLine(line, lineIndex) {
forEachLine((line, lineIndex) => {
const lineNumber = lineIndex + 1;
if (trailingSpaceRe.test(line) &&
(listItemLineNumbers.indexOf(lineNumber) === -1)) {
const actual = line.length - shared.trimRight(line).length;
!includesSorted(listItemLineNumbers, lineNumber)) {
const actual = line.length - trimRight(line).length;
if (expected !== actual) {
shared.addError(onError, lineNumber,
addError(onError, lineNumber,
"Expected: " + (expected === 0 ? "" : "0 or ") +
expected + "; Actual: " + actual,
null,
shared.rangeFromRegExp(line, trailingSpaceRe));
null, rangeFromRegExp(line, trailingSpaceRe));
}
}
});