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