Last active
December 2, 2015 19:38
-
-
Save snoack/6e598775a83cc472ccfe to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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