Implement PCRE Matching (prefer left-branch) #2
@@ -152,7 +152,6 @@ func pruneIndices(indices []Match) []Match {
|
|||||||
}
|
}
|
||||||
|
|
||||||
func copyThread(to *nfaState, from nfaState) {
|
func copyThread(to *nfaState, from nfaState) {
|
||||||
to.threadSP = from.threadSP
|
|
||||||
to.threadGroups = append([]Group{}, from.threadGroups...)
|
to.threadGroups = append([]Group{}, from.threadGroups...)
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -253,6 +252,35 @@ func (regex Reg) FindAllSubmatch(str string) []Match {
|
|||||||
return indices
|
return indices
|
||||||
}
|
}
|
||||||
|
|
||||||
|
func addStateToList(idx int, list []nfaState, state nfaState) []nfaState {
|
||||||
|
if stateExists(list, state) {
|
||||||
|
return list
|
||||||
|
}
|
||||||
|
if state.isAlternation {
|
||||||
|
copyThread(state.next, state)
|
||||||
|
list = append(list, addStateToList(idx, list, *state.next)...)
|
||||||
|
copyThread(state.splitState, state)
|
||||||
|
list = append(list, addStateToList(idx, list, *state.splitState)...)
|
||||||
|
return list
|
||||||
|
}
|
||||||
|
if state.isKleene {
|
||||||
|
copyThread(state.splitState, state)
|
||||||
|
list = append(list, addStateToList(idx, list, *state.splitState)...)
|
||||||
|
copyThread(state.next, state)
|
||||||
|
list = append(list, addStateToList(idx, list, *state.next)...)
|
||||||
|
return list
|
||||||
|
}
|
||||||
|
if state.groupBegin {
|
||||||
|
state.threadGroups[state.groupNum].StartIdx = idx
|
||||||
|
}
|
||||||
|
if state.groupEnd {
|
||||||
|
state.threadGroups[state.groupNum].StartIdx = idx
|
||||||
|
}
|
||||||
|
copyThread(state.next, state)
|
||||||
|
return append(list, *state.next)
|
||||||
|
|
||||||
|
}
|
||||||
|
|
||||||
// Helper for FindAllMatches. Returns whether it found a match, the
|
// Helper for FindAllMatches. Returns whether it found a match, the
|
||||||
// first Match it finds, and how far it got into the string ie. where
|
// first Match it finds, and how far it got into the string ie. where
|
||||||
// the next search should start from.
|
// the next search should start from.
|
||||||
@@ -307,7 +335,6 @@ func findAllSubmatchHelper(start *nfaState, str []rune, offset int, numGroups in
|
|||||||
// tempIndices[start.groupNum].startIdx = i
|
// tempIndices[start.groupNum].startIdx = i
|
||||||
//}
|
//}
|
||||||
|
|
||||||
start.threadSP = i
|
|
||||||
currentStates = append(currentStates, *start)
|
currentStates = append(currentStates, *start)
|
||||||
var foundMatch bool
|
var foundMatch bool
|
||||||
var isEmptyAndNoAssertion bool
|
var isEmptyAndNoAssertion bool
|
||||||
@@ -404,23 +431,28 @@ func findAllSubmatchHelper(start *nfaState, str []rune, offset int, numGroups in
|
|||||||
}
|
}
|
||||||
|
|
||||||
if isEmptyAndNoAssertion || foundMatch {
|
if isEmptyAndNoAssertion || foundMatch {
|
||||||
allMatches := make([]nfaState, 0)
|
nextMatch := *(currentState.next)
|
||||||
allMatches = append(allMatches, *(currentState.next))
|
copyThread(&nextMatch, currentState)
|
||||||
slices.Reverse(allMatches)
|
|
||||||
for i := range allMatches {
|
|
||||||
copyThread(&allMatches[i], currentState)
|
|
||||||
if foundMatch && currentState.assert == noneAssert {
|
|
||||||
allMatches[i].threadSP += 1
|
|
||||||
}
|
|
||||||
}
|
|
||||||
if currentState.groupBegin {
|
if currentState.groupBegin {
|
||||||
currentStates = slices.Insert(currentStates, currentStateIdx+1, allMatches...)
|
// if !stateExists(currentStates, nextMatch) {
|
||||||
|
currentStates = slices.Insert(currentStates, currentStateIdx+1, nextMatch)
|
||||||
|
//}
|
||||||
} else if currentState.groupEnd {
|
} else if currentState.groupEnd {
|
||||||
currentStates = append(currentStates, allMatches...)
|
if !stateExists(currentStates, nextMatch) {
|
||||||
|
currentStates = slices.Insert(currentStates, currentStateIdx+1, nextMatch) // append(currentStates, nextMatch)
|
||||||
|
}
|
||||||
} else if currentState.assert != noneAssert {
|
} else if currentState.assert != noneAssert {
|
||||||
currentStates = append(currentStates, allMatches...)
|
if !stateExists(currentStates, nextMatch) {
|
||||||
|
currentStates = append(currentStates, nextMatch)
|
||||||
|
}
|
||||||
|
} else if currentState.isEmpty && !currentState.groupBegin && !currentState.groupEnd {
|
||||||
|
if !stateExists(currentStates, nextMatch) {
|
||||||
|
currentStates = append(currentStates, nextMatch)
|
||||||
|
}
|
||||||
} else {
|
} else {
|
||||||
nextStates = append(nextStates, allMatches...)
|
if !stateExists(nextStates, nextMatch) {
|
||||||
|
nextStates = append(nextStates, nextMatch)
|
||||||
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
Reference in New Issue
Block a user