diff --git a/regex/matching.go b/regex/matching.go index ad7d15b..1263e37 100644 --- a/regex/matching.go +++ b/regex/matching.go @@ -314,13 +314,36 @@ func findAllSubmatchHelper(start *nfaState, str []rune, offset int, numGroups in currentState.threadGroups = newMatch(numGroups + 1) currentState.threadGroups[0].StartIdx = idx } + if currentState.groupBegin { currentState.threadGroups[currentState.groupNum].StartIdx = idx - } else if currentState.groupEnd { + } + + if currentState.groupEnd { currentState.threadGroups[currentState.groupNum].EndIdx = idx - } else if currentState.isKleene { - // Append the - } else if currentState.isAlternation { + } + + // if currentState.isKleene { + // // Append the next-state (after the kleene), then append the kleene state + // allMatches := make([]*nfaState, 0) + // for _, v := range currentState.transitions { + // allMatches = append(allMatches, v...) + // } + // slices.Reverse(allMatches) + // for _, m := range allMatches { + // m.threadGroups = currentState.threadGroups + // m.threadSP = idx + // } + // currentStates = append(currentStates, allMatches...) + // + // // kleeneState := currentState.kleeneState + // // kleeneState.threadGroups = currentState.threadGroups + // // kleeneState.threadSP = currentState.threadSP + // // currentStates = append(currentStates, kleeneState) + // continue + // } + + if currentState.isAlternation { rightState := currentState.rightState rightState.threadGroups = currentState.threadGroups rightState.threadSP = currentState.threadSP @@ -330,7 +353,22 @@ func findAllSubmatchHelper(start *nfaState, str []rune, offset int, numGroups in leftState.threadSP = currentState.threadSP currentStates = append(currentStates, currentState.leftState) continue - } else if currentState.contentContains(str, idx) { + } + + if currentState.isEmpty && currentState.assert == noneAssert { + allMatches := make([]*nfaState, 0) + for _, v := range currentState.transitions { + allMatches = append(allMatches, v...) + } + slices.Reverse(allMatches) + for _, m := range allMatches { + m.threadGroups = currentState.threadGroups + m.threadSP = idx + } + currentStates = append(currentStates, allMatches...) + } + + if currentState.contentContains(str, idx) { foundMatch = true allMatches := make([]*nfaState, 0) for _, v := range currentState.transitions { @@ -348,9 +386,17 @@ func findAllSubmatchHelper(start *nfaState, str []rune, offset int, numGroups in currentStates = append(currentStates, allMatches...) } - if currentState.isLast && foundMatch { // Last state reached - currentState.threadGroups[0].EndIdx = idx + 1 - return true, currentState.threadGroups, idx + 1 + if currentState.isLast { // Last state reached + if foundMatch { + currentState.threadGroups[0].EndIdx = idx + 1 + return true, currentState.threadGroups, idx + 1 + } else if currentState.isEmpty && currentState.assert == noneAssert { + currentState.threadGroups[0].EndIdx = idx + if idx == currentState.threadGroups[0].StartIdx { + idx++ + } + return true, currentState.threadGroups, idx + } } }