Made lookarounds a little more efficient by only matching from (or to, in the case of lookbehind) the current index

master
Aadhavan Srinivasan 2 weeks ago
parent 3fda07280e
commit 332c2fe5a2

@ -118,15 +118,21 @@ func (s State) checkAssertion(str []rune, idx int) bool {
if s.isLookaround() { if s.isLookaround() {
// The process here is simple: // The process here is simple:
// 1. Compile the regex stored in the state's contents. // 1. Compile the regex stored in the state's contents.
// 2. Run it on the test string. // 2. Run it on a subset of the test string, that ends after the current index in the string
// 3. Based on the kind of lookaround (and the indices we get), determine what action to take. // 3. Based on the kind of lookaround (and the indices we get), determine what action to take.
startState := s.lookaroundNFA startState := s.lookaroundNFA
matchIndices := findAllMatches(startState, str, startState.lookaroundNumCaptureGroups) var strToMatch []rune
if s.assert == PLA || s.assert == NLA {
strToMatch = str[idx:]
} else {
strToMatch = str[:idx]
}
matchIndices := findAllMatches(startState, strToMatch, startState.lookaroundNumCaptureGroups)
numMatchesFound := 0 numMatchesFound := 0
for _, matchIdx := range matchIndices { for _, matchIdx := range matchIndices {
if s.assert == PLA || s.assert == NLA { // Lookahead - return true (or false) if at least one match starts at the current index if s.assert == PLA || s.assert == NLA { // Lookahead - return true (or false) if at least one match starts at 0. Zero is used because the test-string _starts_ from idx.
if matchIdx[0].startIdx == idx { if matchIdx[0].startIdx == 0 {
numMatchesFound++ numMatchesFound++
} }
} }

Loading…
Cancel
Save