1
1
mirror of https://github.com/walles/moar.git synced 2024-09-11 12:15:43 +03:00
moar/m/matchRanges.go

44 lines
725 B
Go

package m
import "regexp"
// MatchRanges collects match indices
type MatchRanges struct {
Matches [][]int
}
// GetMatchRanges locates a regexp in a string
func GetMatchRanges(String *string, Pattern *regexp.Regexp) *MatchRanges {
if Pattern == nil {
return nil
}
return &MatchRanges{
Matches: Pattern.FindAllStringIndex(*String, -1),
}
}
// InRange says true if the index is part of a regexp match
func (mr *MatchRanges) InRange(index int) bool {
if mr == nil {
return false
}
for _, match := range mr.Matches {
matchFirstIndex := match[0]
matchLastIndex := match[1] - 1
if index < matchFirstIndex {
continue
}
if index > matchLastIndex {
continue
}
return true
}
return false
}