Wrote new method to return 0-group of leftmost match; reorganized some functions for better clarity; made 'FindNthMatch' a method

master
Aadhavan Srinivasan 2 days ago
parent e9d4e857cf
commit 037ac75ea6

@ -57,6 +57,11 @@ func (g Group) isValid() bool {
return g.StartIdx >= 0 && g.EndIdx >= 0 return g.StartIdx >= 0 && g.EndIdx >= 0
} }
// Simple function, makes it easier to map this over a list of matches
func getZeroGroup(m Match) Group {
return m[0]
}
// takeZeroState takes the 0-state (if such a transition exists) for all states in the // takeZeroState takes the 0-state (if such a transition exists) for all states in the
// given slice. It returns the resulting states. If any of the resulting states is a 0-state, // given slice. It returns the resulting states. If any of the resulting states is a 0-state,
// the second ret val is true. // the second ret val is true.
@ -138,30 +143,51 @@ func pruneIndices(indices []Match) []Match {
return toRet return toRet
} }
// FindString returns a string containing the text of the leftmost match of the regex in the given string. // Find returns the 0-group of the leftmost match of the regex in the given string.
// An error value != nil indicates that no match was found.
func (regex Reg) Find(str string) (Group, error) {
match, err := regex.FindNthMatch(str, 1)
if err != nil {
return Group{}, nil
}
return getZeroGroup(match), nil
}
// FindAll returns a slice containing all the 0-groups of the regex in the given string.
// A 0-group represents the match without any submatches.
func (regex Reg) FindAll(str string) []Group {
indices := regex.FindAllSubmatch(str)
zeroGroups := funcMap(indices, getZeroGroup)
return zeroGroups
}
// FindString returns the text of the leftmost match of the regex in the given string.
// The return value will be an empty string in two situations: // The return value will be an empty string in two situations:
// 1. No match was found // 1. No match was found
// 2. The match was an empty string // 2. The match was an empty string
func (regex Reg) FindString(str string) string { func (regex Reg) FindString(str string) string {
match, err := FindNthMatch(regex, str, 1) match, err := regex.FindNthMatch(str, 1)
if err != nil { if err != nil {
return "" return ""
} }
return str[match[0].StartIdx:match[0].EndIdx] zeroGroup := getZeroGroup(match)
return str[zeroGroup.StartIdx:zeroGroup.EndIdx]
} }
// FindAllString is the 'all' version of FindString. // FindAllString is the 'all' version of FindString.
// It returns a slice of strings containing the text of all matches of // It returns a slice of strings containing the text of all matches of
// the regex in the given string. // the regex in the given string.
func (regex Reg) FindAllString(str string) []string { func (regex Reg) FindAllString(str string) []string {
return []string{} zerogroups := regex.FindAll(str)
// matches := FindAllMatches, str string) matchStrs := funcMap(zerogroups, func(g Group) string {
return str[g.StartIdx:g.EndIdx]
})
return matchStrs
} }
// FindNthMatch finds the 'n'th match of the regex represented by the given start-state, with // FindNthMatch return the 'n'th match of the regex in 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(regex Reg, str string, n int) (Match, error) { func (regex Reg) FindNthMatch(str string, n int) (Match, error) {
idx := 0 idx := 0
matchNum := 0 matchNum := 0
str_runes := []rune(str) str_runes := []rune(str)
@ -180,16 +206,6 @@ func FindNthMatch(regex Reg, str string, n int) (Match, error) {
return nil, fmt.Errorf("invalid match index - too few matches found") return nil, fmt.Errorf("invalid match index - too few matches found")
} }
// FindAll returns a slice containing all the 0-groups of the regex in the given string.
// A 0-group represents the match without any submatches.
func (regex Reg) FindAll(str string) []Group {
indices := regex.FindAllSubmatch(str)
zeroGroups := funcMap(indices, func(m Match) Group {
return m[0]
})
return zeroGroups
}
// FindAllSubmatch returns a slice of matches in the given string. // FindAllSubmatch returns a slice of matches in the given string.
func (regex Reg) FindAllSubmatch(str string) []Match { func (regex Reg) FindAllSubmatch(str string) []Match {
idx := 0 idx := 0

Loading…
Cancel
Save