// Increment until we hit a character matching the start state (assuming not 0-state)
// Increment until we hit a character matching the start state (assuming not 0-state)
ifstart.isEmpty==false{
// if start.isEmpty == false {
fori<len(str)&&!start.contentContains(str,i){
// for i < len(str) && !start.contentContains(str, i) {
i++
// i++
}
// }
startIdx=i
// startIdx = i
startingFrom=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
// 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
// Check if the start state begins a group - if so, add the start index to our list
ifstart.groupBegin{
//if start.groupBegin {
start.threadGroups[start.groupNum].StartIdx=i
// start.threadGroups[start.groupNum].StartIdx = i
// tempIndices[start.groupNum].startIdx = i
// tempIndices[start.groupNum].startIdx = i
}
//}
start.threadSP=i
start.threadSP=i
heap.Push(currentStates,newPriorQueueItem(start))
currentStates=append(currentStates,start)
varfoundMatchbool
// Main loop
// Main loop
forcurrentStates.Len()>0{
forlen(currentStates)>0{
currentState:=heap.Pop(currentStates)
currentState,_:=pop(¤tStates)
foundPath=false
idx:=currentState.threadSP
foundMatch=false
zeroStates:=make([]*nfaState,0)
// Keep taking zero-states, until there are no more left to take
ifcurrentState.threadGroups==nil{
// 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
heap.Push(currentStates,newPriorQueueItem(state))
currentState.threadGroups[0].EndIdx=idx+1
}
returntrue,currentState.threadGroups,idx+1
tempStates=nil
// 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{
returnfalse,[]Group{},i+1
assertionFailed=true
// zeroStates := make([]*nfaState, 0)
}
// // Keep taking zero-states, until there are no more left to take
ifstate.isLast{
// // 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{
// // Check if we can find a state in our list that is:
returntrue,tempIndices,tempIndices[0].EndIdx
// // a. A last-state
}
// // b. Empty
}
// // c. Doesn't assert anything
returnfalse,[]Group{},startIdx
// for _, stateItem := range *currentStates {
}
// s := stateItem.state
currentStates=&priorityQueue{}
// if s.isLast && s.isEmpty && s.assert == noneAssert {
slices.Reverse(tempStates)
// lastStatePtr = s
for_,state:=rangetempStates{
// lastStateInList = true
heap.Push(currentStates,newPriorQueueItem(state))
// }
}
// }
tempStates=nil
// 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++ {
i++
// tempIndices[j] = lastStatePtr.threadGroups[j]
}
// }
// endIdx = 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.
// tempIndices[0] = Group{startIdx, endIdx}
// This is the exact same algorithm used inside the loop, so I should probably put it in a function.
// if tempIndices[0].StartIdx == tempIndices[0].EndIdx {
ifnum_appended==0{// Break if we haven't appended any more unique values
// return item.state
break
// })
}
// if ok := zeroMatchPossible(str, i, numGroups, currentStatesList...); ok {
}
// if tempIndices[0].IsValid() == false {
}
// tempIndices[0] = Group{startIdx, startIdx}
// }
for_,state:=rangetempStates{
// }
heap.Push(currentStates,newPriorQueueItem(state))
// // 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.
tempStates=nil
// // if i == startingFrom {
// startIdx++
for_,stateItem:=range*currentStates{
// // i++
state:=stateItem.state
// // }
// Only add the match if the start index is in bounds. If the state has an assertion,
// if tempIndices.numValidGroups() > 0 && tempIndices[0].IsValid() {
// make sure the assertion checks out.
// 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.
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.
// tempStates = nil
returntrue,tempIndices,tempIndices[0].EndIdx+1
//
}else{
// i++
returntrue,tempIndices,tempIndices[0].EndIdx
// }
}
//
}
// // 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.
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.
// // This is the exact same algorithm used inside the loop, so I should probably put it in a function.
// // 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.