From 332c2fe5a253a95834152dbae2eb0c70abf3a258 Mon Sep 17 00:00:00 2001 From: Aadhavan Srinivasan Date: Wed, 11 Dec 2024 00:31:08 -0500 Subject: [PATCH] Made lookarounds a little more efficient by only matching from (or to, in the case of lookbehind) the current index --- nfa.go | 14 ++++++++++---- 1 file changed, 10 insertions(+), 4 deletions(-) diff --git a/nfa.go b/nfa.go index 4ba6141..0acae90 100644 --- a/nfa.go +++ b/nfa.go @@ -118,15 +118,21 @@ func (s State) checkAssertion(str []rune, idx int) bool { if s.isLookaround() { // The process here is simple: // 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. 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 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 matchIdx[0].startIdx == idx { + 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 == 0 { numMatchesFound++ } }