Changed API for match-finding functions - take in a Reg instead of start state and numGroups separately
This commit is contained in:
14
matching.go
14
matching.go
@@ -141,13 +141,14 @@ func pruneIndices(indices []Match) []Match {
|
|||||||
// findNthMatch finds the 'n'th match of the regex represented by the given start-state, with
|
// findNthMatch finds the 'n'th match of the regex represented by the given start-state, with
|
||||||
// the given string.
|
// the given string.
|
||||||
// It returns an error (!= nil) if there are fewer than 'n' matches in the string.
|
// It returns an error (!= nil) if there are fewer than 'n' matches in the string.
|
||||||
func findNthMatch(start *State, str []rune, numGroups int, n int) (Match, error) {
|
func findNthMatch(regex Reg, str string, n int) (Match, error) {
|
||||||
idx := 0
|
idx := 0
|
||||||
matchNum := 0
|
matchNum := 0
|
||||||
|
str_runes := []rune(str)
|
||||||
var matchFound bool
|
var matchFound bool
|
||||||
var matchIdx Match
|
var matchIdx Match
|
||||||
for idx <= len(str) {
|
for idx <= len(str_runes) {
|
||||||
matchFound, matchIdx, idx = findAllMatchesHelper(start, str, idx, numGroups)
|
matchFound, matchIdx, idx = findAllMatchesHelper(regex.start, str_runes, idx, regex.numGroups)
|
||||||
if matchFound {
|
if matchFound {
|
||||||
matchNum++
|
matchNum++
|
||||||
}
|
}
|
||||||
@@ -161,13 +162,14 @@ func findNthMatch(start *State, str []rune, numGroups int, n int) (Match, error)
|
|||||||
|
|
||||||
// findAllMatches tries to find all matches of the regex represented by given start-state, with
|
// findAllMatches tries to find all matches of the regex represented by given start-state, with
|
||||||
// the given string
|
// the given string
|
||||||
func findAllMatches(start *State, str []rune, numGroups int) []Match {
|
func findAllMatches(regex Reg, str string) []Match {
|
||||||
idx := 0
|
idx := 0
|
||||||
|
str_runes := []rune(str)
|
||||||
var matchFound bool
|
var matchFound bool
|
||||||
var matchIdx Match
|
var matchIdx Match
|
||||||
indices := make([]Match, 0)
|
indices := make([]Match, 0)
|
||||||
for idx <= len(str) {
|
for idx <= len(str_runes) {
|
||||||
matchFound, matchIdx, idx = findAllMatchesHelper(start, str, idx, numGroups)
|
matchFound, matchIdx, idx = findAllMatchesHelper(regex.start, str_runes, idx, regex.numGroups)
|
||||||
if matchFound {
|
if matchFound {
|
||||||
indices = append(indices, matchIdx)
|
indices = append(indices, matchIdx)
|
||||||
}
|
}
|
||||||
|
Reference in New Issue
Block a user