Skip to content

Instantly share code, notes, and snippets.

@snoack
Last active December 2, 2015 19:38
Show Gist options
  • Save snoack/6e598775a83cc472ccfe to your computer and use it in GitHub Desktop.
Save snoack/6e598775a83cc472ccfe to your computer and use it in GitHub Desktop.
function findPositions(text, query, boundary) {
let lastBoundary = 0;
let numMatched = 0;
let found = false;
let positions = [];
for (let pos = 0; pos < text.length; pos++) {
let c = text[pos];
if (c == boundary) {
if (found)
positions.push({start: lastBoundary + 1, end: pos});
lastBoundary = pos;
numMatched = 0;
found = false;
} else if (!found) {
if (c != query[numMatched])
numMatched = 0;
else if (++numMatched == query.length)
found = true;
}
}
if (found)
positions.push({start: lastBoundary + 1, end: text.length});
return positions;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment