// Increment until we hit a character matching the start state (assuming not 0-state)
ifstart.isEmpty==false{
fori<len(str)&&!start.contentContains(str,i){
i++
}
startIdx=i
startingFrom=i
i++// Advance to next character (if we aren't at a 0-state, which doesn't match anything), so that we can check for transitions. If we advance at a 0-state, we will never get a chance to match the first character
}
// if start.isEmpty == false {
// for i < len(str) && !start.contentContains(str, i) {
// i++
// }
// startIdx = i
// startingFrom = i
// i++ // Advance to next character (if we aren't at a 0-state, which doesn't match anything), so that we can check for transitions. If we advance at a 0-state, we will never get a chance to match the first character
// }
start.threadGroups=newMatch(numGroups+1)
// start.threadGroups = newMatch(numGroups + 1)
// Check if the start state begins a group - if so, add the start index to our list
ifstart.groupBegin{
start.threadGroups[start.groupNum].StartIdx=i
//if start.groupBegin {
// start.threadGroups[start.groupNum].StartIdx = i
// tempIndices[start.groupNum].startIdx = i
}
//}
start.threadSP=i
heap.Push(currentStates,newPriorQueueItem(start))
currentStates=append(currentStates,start)
varfoundMatchbool
// Main loop
forcurrentStates.Len()>0{
currentState:=heap.Pop(currentStates)
foundPath=false
zeroStates:=make([]*nfaState,0)
// Keep taking zero-states, until there are no more left to take
// Objective: If any of our current states have transitions to 0-states, replace them with the 0-state. Do this until there are no more transitions to 0-states, or there are no more unique 0-states to take.
ifcurrentState.isLast&&foundMatch{// Last state reached
currentState.threadGroups[0].EndIdx=idx+1
returntrue,currentState.threadGroups,idx+1
// Take any transitions corresponding to current character
numStatesMatched:=0// The number of states which had at least 1 match for this round
assertionFailed:=false// Whether or not an assertion failed for this round
lastStateInList:=false// Whether or not a last state was in our list of states
varlastStatePtr*nfaState=nil// Pointer to the last-state, if it was found
lastLookaroundInList:=false// Whether or not a last state (that is a lookaround) was in our list of states
fornumStatesMatched==0&&lastStateInList==false{
ifcurrentStates.Len()==0{
break
}
stateItem:=heap.Pop(currentStates)
state:=stateItem.(*priorQueueItem).state
matches,numMatches:=state.matchesFor(str,i)
ifnumMatches>0{
numStatesMatched++
tempStates=append([]*nfaState(nil),matches...)
foundPath=true
for_,m:=rangematches{
ifm.threadGroups==nil{
m.threadGroups=newMatch(numGroups+1)
}
m.threadSP=state.threadSP+1
copy(m.threadGroups,state.threadGroups)
}
}
ifnumMatches<0{
assertionFailed=true
}
ifstate.isLast{
ifstate.isLookaround(){
lastLookaroundInList=true
}
lastStateInList=true
lastStatePtr=state
}
}
ifassertionFailed&&numStatesMatched==0{// Nothing has matched and an assertion has failed
// If I'm being completely honest, I'm not sure why I have to check specifically for a _lookaround_
// state. The explanation below is my attempt to explain this behavior.
// If you replace 'lastLookaroundInList' with 'lastStateInList', one of the test cases fails.
returnfalse,[]Group{},i+1
// zeroStates := make([]*nfaState, 0)
// // Keep taking zero-states, until there are no more left to take
// // Objective: If any of our current states have transitions to 0-states, replace them with the 0-state. Do this until there are no more transitions to 0-states, or there are no more unique 0-states to take.
iftempIndices[0].StartIdx==tempIndices[0].EndIdx{// If we have a zero-length match, we have to shift the index at which we start. Otherwise we keep looking at the same paert of the string over and over.
returntrue,tempIndices,tempIndices[0].EndIdx+1
}else{
returntrue,tempIndices,tempIndices[0].EndIdx
}
}
returnfalse,[]Group{},startIdx
}
currentStates=&priorityQueue{}
slices.Reverse(tempStates)
for_,state:=rangetempStates{
heap.Push(currentStates,newPriorQueueItem(state))
}
tempStates=nil
i++
}
// End-of-string reached. Go to any 0-states, until there are no more 0-states to go to. Then check if any of our states are in the end position.
// This is the exact same algorithm used inside the loop, so I should probably put it in a function.
iftempIndices[0].StartIdx==tempIndices[0].EndIdx{// If we have a zero-length match, we have to shift the index at which we start. Otherwise we keep looking at the same paert of the string over and over.
returntrue,tempIndices,tempIndices[0].EndIdx+1
}else{
returntrue,tempIndices,tempIndices[0].EndIdx
}
}
ifstartIdx==startingFrom{// Increment starting index if we haven't moved in the string. Prevents us from matching the same part of the string over and over.
startIdx++
}
returnfalse,[]Group{},startIdx
// return false, []Group{}, i
// }
// }
// // Check if we can find a state in our list that is:
// // a. A last-state
// // b. Empty
// // c. Doesn't assert anything
// for _, stateItem := range *currentStates {
// s := stateItem.state
// if s.isLast && s.isEmpty && s.assert == noneAssert {
// lastStatePtr = s
// lastStateInList = true
// }
// }
// if lastStateInList && numStatesMatched == 0 { // A last-state was in the list of states. add the matchIndex to our MatchIndex list
// for j := 1; j < numGroups+1; j++ {
// tempIndices[j] = lastStatePtr.threadGroups[j]
// }
// endIdx = i
// tempIndices[0] = Group{startIdx, endIdx}
// if tempIndices[0].StartIdx == tempIndices[0].EndIdx {
// if ok := zeroMatchPossible(str, i, numGroups, currentStatesList...); ok {
// if tempIndices[0].IsValid() == false {
// tempIndices[0] = Group{startIdx, startIdx}
// }
// }
// // If we haven't moved in the string, increment the counter by 1
// // to ensure we don't keep trying the same string over and over.
// // if i == startingFrom {
// startIdx++
// // i++
// // }
// if tempIndices.numValidGroups() > 0 && tempIndices[0].IsValid() {
// if tempIndices[0].StartIdx == tempIndices[0].EndIdx { // If we have a zero-length match, we have to shift the index at which we start. Otherwise we keep looking at the same paert of the string over and over.
// // Only add the match if the start index is in bounds. If the state has an assertion,
// // make sure the assertion checks out.
// if state.isLast && i <= len(str) {
// if state.assert == noneAssert || state.checkAssertion(str, i) {
// for j := 1; j < numGroups+1; j++ {
// tempIndices[j] = state.threadGroups[j]
// }
// endIdx = i
// tempIndices[0] = Group{startIdx, endIdx}
// }
// }
// }
//
// if tempIndices.numValidGroups() > 0 {
// if tempIndices[0].StartIdx == tempIndices[0].EndIdx { // If we have a zero-length match, we have to shift the index at which we start. Otherwise we keep looking at the same paert of the string over and over.
// if startIdx == startingFrom { // Increment starting index if we haven't moved in the string. Prevents us from matching the same part of the string over and over.