Replaced call to 'FindAllMatches' with call to 'FindAll' or 'FindAllSubmatch' depending on whether I need submatches

master
Aadhavan Srinivasan 2 days ago
parent 45b6566b2c
commit 8eda5055ff

@ -156,17 +156,18 @@ func (s nfaState) checkAssertion(str []rune, idx int) bool {
strToMatch = string(runesToMatch) strToMatch = string(runesToMatch)
} }
matchIndices := FindAllMatches(Reg{startState, s.lookaroundNumCaptureGroups}, strToMatch) regComp := Reg{startState, s.lookaroundNumCaptureGroups}
matchIndices := regComp.FindAll(strToMatch)
numMatchesFound := 0 numMatchesFound := 0
for _, matchIdx := range matchIndices { for _, matchIdx := range matchIndices {
if s.assert == plaAssert || s.assert == nlaAssert { // Lookahead - return true (or false) if at least one match starts at 0. Zero is used because the test-string _starts_ from idx. if s.assert == plaAssert || s.assert == nlaAssert { // Lookahead - return true (or false) if at least one match starts at 0. Zero is used because the test-string _starts_ from idx.
if matchIdx[0].StartIdx == 0 { if matchIdx.StartIdx == 0 {
numMatchesFound++ numMatchesFound++
} }
} }
if s.assert == plbAssert || s.assert == nlbAssert { // Lookbehind - return true (or false) if at least one match _ends_ at the current index. if s.assert == plbAssert || s.assert == nlbAssert { // Lookbehind - return true (or false) if at least one match _ends_ at the current index.
if matchIdx[0].EndIdx == idx { if matchIdx.EndIdx == idx {
numMatchesFound++ numMatchesFound++
} }
} }

@ -682,13 +682,9 @@ func TestFindAllMatches(t *testing.T) {
panic(fmt.Errorf("Test Error: %v", err)) panic(fmt.Errorf("Test Error: %v", err))
} }
} else { } else {
matchIndices := FindAllMatches(regComp, test.str) matchIndices := regComp.FindAll(test.str)
zeroGroups := make([]Group, len(matchIndices)) if !slices.Equal(test.result, matchIndices) {
for i, m := range matchIndices { t.Errorf("Wanted %v Got %v\n", test.result, matchIndices)
zeroGroups[i] = m[0]
}
if !slices.Equal(test.result, zeroGroups) {
t.Errorf("Wanted %v Got %v\n", test.result, zeroGroups)
} }
} }
}) })
@ -720,6 +716,32 @@ func TestFindString(t *testing.T) {
} }
} }
func TestFindAllStrings(t *testing.T) {
t.Skip("Skipping finding all strings")
for _, test := range reTests {
t.Run(test.re+" "+test.str, func(t *testing.T) {
regComp, err := Compile(test.re, test.flags...)
if err != nil {
if test.result != nil {
panic(err)
}
} else {
foundStrings := regComp.FindAllString(test.str)
if len(test.result) != len(foundStrings) {
t.Errorf("Differing number of matches: Wanted %v matches Got %v matches\n", len(test.result), len(foundStrings))
} else {
for idx, group := range test.result {
groupStr := test.str[group.StartIdx:group.EndIdx]
if groupStr != foundStrings[idx] {
t.Errorf("Wanted %v Got %v\n", groupStr, foundStrings[idx])
}
}
}
}
})
}
}
func TestFindAllGroups(t *testing.T) { func TestFindAllGroups(t *testing.T) {
for _, test := range groupTests { for _, test := range groupTests {
t.Run(test.re+" "+test.str, func(t *testing.T) { t.Run(test.re+" "+test.str, func(t *testing.T) {
@ -729,7 +751,7 @@ func TestFindAllGroups(t *testing.T) {
panic(err) panic(err)
} }
} }
matchIndices := FindAllMatches(regComp, test.str) matchIndices := regComp.FindAllSubmatch(test.str)
for i := range matchIndices { for i := range matchIndices {
for j := range matchIndices[i] { for j := range matchIndices[i] {
if matchIndices[i][j].isValid() { if matchIndices[i][j].isValid() {

Loading…
Cancel
Save