From 8eda5055ff5222124d255829332a9851090fae98 Mon Sep 17 00:00:00 2001 From: Aadhavan Srinivasan Date: Fri, 31 Jan 2025 09:55:36 -0500 Subject: [PATCH] Replaced call to 'FindAllMatches' with call to 'FindAll' or 'FindAllSubmatch' depending on whether I need submatches --- regex/nfa.go | 7 ++++--- regex/re_test.go | 38 ++++++++++++++++++++++++++++++-------- 2 files changed, 34 insertions(+), 11 deletions(-) diff --git a/regex/nfa.go b/regex/nfa.go index 0dc4e2a..908db67 100644 --- a/regex/nfa.go +++ b/regex/nfa.go @@ -156,17 +156,18 @@ func (s nfaState) checkAssertion(str []rune, idx int) bool { strToMatch = string(runesToMatch) } - matchIndices := FindAllMatches(Reg{startState, s.lookaroundNumCaptureGroups}, strToMatch) + regComp := Reg{startState, s.lookaroundNumCaptureGroups} + matchIndices := regComp.FindAll(strToMatch) numMatchesFound := 0 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 matchIdx[0].StartIdx == 0 { + if matchIdx.StartIdx == 0 { numMatchesFound++ } } 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++ } } diff --git a/regex/re_test.go b/regex/re_test.go index e319b0e..7ac2d80 100644 --- a/regex/re_test.go +++ b/regex/re_test.go @@ -682,13 +682,9 @@ func TestFindAllMatches(t *testing.T) { panic(fmt.Errorf("Test Error: %v", err)) } } else { - matchIndices := FindAllMatches(regComp, test.str) - zeroGroups := make([]Group, len(matchIndices)) - for i, m := range matchIndices { - zeroGroups[i] = m[0] - } - if !slices.Equal(test.result, zeroGroups) { - t.Errorf("Wanted %v Got %v\n", test.result, zeroGroups) + matchIndices := regComp.FindAll(test.str) + if !slices.Equal(test.result, matchIndices) { + t.Errorf("Wanted %v Got %v\n", test.result, matchIndices) } } }) @@ -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) { for _, test := range groupTests { t.Run(test.re+" "+test.str, func(t *testing.T) { @@ -729,7 +751,7 @@ func TestFindAllGroups(t *testing.T) { panic(err) } } - matchIndices := FindAllMatches(regComp, test.str) + matchIndices := regComp.FindAllSubmatch(test.str) for i := range matchIndices { for j := range matchIndices[i] { if matchIndices[i][j].isValid() {