// 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.
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{
}else{
returntrue,tempIndices,tempIndices[0].EndIdx
m.threadSP=idx
}
}
}
}
returnfalse,[]Group{},startIdx
currentStates=append(currentStates,allMatches...)
}
currentStates=&priorityQueue{}
slices.Reverse(tempStates)
for_,state:=rangetempStates{
heap.Push(currentStates,newPriorQueueItem(state))
}
}
tempStates=nil
i++
ifcurrentState.isLast&&foundMatch{// Last state reached
}
currentState.threadGroups[0].EndIdx=idx+1
returntrue,currentState.threadGroups,idx+1
// 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.
ifnum_appended==0{// Break if we haven't appended any more unique values
break
}
}
}
}
}
returnfalse,[]Group{},i+1
for_,state:=rangetempStates{
// zeroStates := make([]*nfaState, 0)
heap.Push(currentStates,newPriorQueueItem(state))
// // 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
// tempStates = nil
}else{
//
returntrue,tempIndices,tempIndices[0].EndIdx
// // 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
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.
// lastStateInList := false // Whether or not a last state was in our list of states
startIdx++
// var lastStatePtr *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
returnfalse,[]Group{},startIdx
// for numStatesMatched == 0 && lastStateInList == false {
// 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.