Implement PCRE Matching (prefer left-branch) #2
31
regex/nfa.go
31
regex/nfa.go
@@ -47,7 +47,6 @@ type nfaState struct {
|
|||||||
// The following properties depend on the current match - I should think about resetting them for every match.
|
// The following properties depend on the current match - I should think about resetting them for every match.
|
||||||
zeroMatchFound bool // Whether or not the state has been used for a zero-length match - only relevant for zero states
|
zeroMatchFound bool // Whether or not the state has been used for a zero-length match - only relevant for zero states
|
||||||
threadGroups []Group // Assuming that a state is part of a 'thread' in the matching process, this array stores the indices of capturing groups in the current thread. As matches are found for this state, its groups will be copied over.
|
threadGroups []Group // Assuming that a state is part of a 'thread' in the matching process, this array stores the indices of capturing groups in the current thread. As matches are found for this state, its groups will be copied over.
|
||||||
threadSP int // The string pointer of the thread - where it is in the input string
|
|
||||||
}
|
}
|
||||||
|
|
||||||
// Clones the NFA starting from the given state.
|
// Clones the NFA starting from the given state.
|
||||||
@@ -123,7 +122,6 @@ func resetThreadsHelper(state *nfaState, visitedMap map[*nfaState]bool) {
|
|||||||
}
|
}
|
||||||
// Assuming it hasn't been visited
|
// Assuming it hasn't been visited
|
||||||
state.threadGroups = nil
|
state.threadGroups = nil
|
||||||
state.threadSP = 0
|
|
||||||
visitedMap[state] = true
|
visitedMap[state] = true
|
||||||
if state.isAlternation {
|
if state.isAlternation {
|
||||||
resetThreadsHelper(state.next, visitedMap)
|
resetThreadsHelper(state.next, visitedMap)
|
||||||
@@ -408,3 +406,32 @@ func zeroLengthMatchState() nfaState {
|
|||||||
start.assert = alwaysTrueAssert
|
start.assert = alwaysTrueAssert
|
||||||
return start
|
return start
|
||||||
}
|
}
|
||||||
|
|
||||||
|
func (s nfaState) equals(other nfaState) bool {
|
||||||
|
return slices.Equal(s.content, other.content) &&
|
||||||
|
s.isEmpty == other.isEmpty &&
|
||||||
|
s.isLast == other.isLast &&
|
||||||
|
slices.Equal(s.output, other.output) &&
|
||||||
|
s.next == other.next &&
|
||||||
|
s.isKleene == other.isKleene &&
|
||||||
|
s.isQuestion == other.isQuestion &&
|
||||||
|
s.isAlternation == other.isAlternation &&
|
||||||
|
s.splitState == other.splitState &&
|
||||||
|
s.assert == other.assert &&
|
||||||
|
s.allChars == other.allChars &&
|
||||||
|
slices.Equal(s.except, other.except) &&
|
||||||
|
s.lookaroundNFA == other.lookaroundNFA &&
|
||||||
|
s.groupBegin == other.groupBegin &&
|
||||||
|
s.groupEnd == other.groupEnd &&
|
||||||
|
s.groupNum == other.groupNum &&
|
||||||
|
slices.Equal(s.threadGroups, other.threadGroups)
|
||||||
|
}
|
||||||
|
|
||||||
|
func stateExists(list []nfaState, s nfaState) bool {
|
||||||
|
for i := range list {
|
||||||
|
if list[i].equals(s) {
|
||||||
|
return true
|
||||||
|
}
|
||||||
|
}
|
||||||
|
return false
|
||||||
|
}
|
||||||
|
Reference in New Issue
Block a user