goext/rext/wrapper.go

199 lines
5.3 KiB
Go
Raw Permalink Normal View History

2023-01-15 01:56:40 +01:00
package rext
2023-03-29 19:53:53 +02:00
import (
"gogs.mikescher.com/BlackForestBytes/goext/langext"
"regexp"
)
2023-01-15 01:56:40 +01:00
type Regex interface {
IsMatch(haystack string) bool
MatchFirst(haystack string) (RegexMatch, bool)
MatchAll(haystack string) []RegexMatch
ReplaceAll(haystack string, repl string, literal bool) string
ReplaceAllFunc(haystack string, repl func(string) string) string
2023-01-15 02:05:05 +01:00
RemoveAll(haystack string) string
2023-01-15 01:56:40 +01:00
GroupCount() int
2023-08-18 13:27:02 +02:00
String() string
2023-01-15 01:56:40 +01:00
}
type regexWrapper struct {
rex *regexp.Regexp
subnames []string
}
type RegexMatch struct {
haystack string
submatchesIndex []int
subnames []string
}
type RegexMatchGroup struct {
haystack string
start int
end int
}
2023-03-29 19:53:10 +02:00
type OptRegexMatchGroup struct {
v *RegexMatchGroup
}
2023-01-15 01:56:40 +01:00
func W(rex *regexp.Regexp) Regex {
return &regexWrapper{rex: rex, subnames: rex.SubexpNames()}
}
// ---------------------------------------------------------------------------------------------------------------------
2023-08-18 13:25:18 +02:00
// IsMatch reports whether the string s contains any match of the regular expression re.
2023-01-15 01:56:40 +01:00
func (w *regexWrapper) IsMatch(haystack string) bool {
return w.rex.MatchString(haystack)
}
func (w *regexWrapper) MatchFirst(haystack string) (RegexMatch, bool) {
res := w.rex.FindStringSubmatchIndex(haystack)
if res == nil {
return RegexMatch{}, false
}
return RegexMatch{haystack: haystack, submatchesIndex: res, subnames: w.subnames}, true
}
func (w *regexWrapper) MatchAll(haystack string) []RegexMatch {
resarr := w.rex.FindAllStringSubmatchIndex(haystack, -1)
matches := make([]RegexMatch, 0, len(resarr))
for _, res := range resarr {
matches = append(matches, RegexMatch{haystack: haystack, submatchesIndex: res, subnames: w.subnames})
}
return matches
}
func (w *regexWrapper) ReplaceAll(haystack string, repl string, literal bool) string {
if literal {
// do not expand placeholder aka $1, $2, ...
return w.rex.ReplaceAllLiteralString(haystack, repl)
} else {
return w.rex.ReplaceAllString(haystack, repl)
}
}
func (w *regexWrapper) ReplaceAllFunc(haystack string, repl func(string) string) string {
return w.rex.ReplaceAllStringFunc(haystack, repl)
}
2023-01-15 02:05:05 +01:00
func (w *regexWrapper) RemoveAll(haystack string) string {
return w.rex.ReplaceAllLiteralString(haystack, "")
}
2023-01-15 01:56:40 +01:00
// GroupCount returns the amount of groups in this match, does not count group-0 (whole match)
func (w *regexWrapper) GroupCount() int {
return len(w.subnames) - 1
}
2023-08-18 13:25:18 +02:00
// String returns the source text used to compile the regular expression.
func (w *regexWrapper) String() string {
return w.rex.String()
}
2023-01-15 01:56:40 +01:00
// ---------------------------------------------------------------------------------------------------------------------
func (m RegexMatch) FullMatch() RegexMatchGroup {
return RegexMatchGroup{haystack: m.haystack, start: m.submatchesIndex[0], end: m.submatchesIndex[1]}
}
// GroupCount returns the amount of groups in this match, does not count group-0 (whole match)
func (m RegexMatch) GroupCount() int {
return len(m.subnames) - 1
}
// GroupByIndex returns the value of a matched group (group 0 == whole match)
func (m RegexMatch) GroupByIndex(idx int) RegexMatchGroup {
return RegexMatchGroup{haystack: m.haystack, start: m.submatchesIndex[idx*2], end: m.submatchesIndex[idx*2+1]}
}
2023-03-29 19:53:10 +02:00
// GroupByName returns the value of a matched group (panics if not found!)
2023-01-15 01:56:40 +01:00
func (m RegexMatch) GroupByName(name string) RegexMatchGroup {
for idx, subname := range m.subnames {
if subname == name {
return RegexMatchGroup{haystack: m.haystack, start: m.submatchesIndex[idx*2], end: m.submatchesIndex[idx*2+1]}
}
}
panic("failed to find regex-group by name")
}
2023-03-29 19:53:10 +02:00
// GroupByName returns the value of a matched group (returns empty OptRegexMatchGroup if not found)
func (m RegexMatch) GroupByNameOrEmpty(name string) OptRegexMatchGroup {
for idx, subname := range m.subnames {
2023-03-31 13:33:06 +02:00
if subname == name && (m.submatchesIndex[idx*2] != -1 || m.submatchesIndex[idx*2+1] != -1) {
2023-03-29 19:53:10 +02:00
return OptRegexMatchGroup{&RegexMatchGroup{haystack: m.haystack, start: m.submatchesIndex[idx*2], end: m.submatchesIndex[idx*2+1]}}
}
}
return OptRegexMatchGroup{}
}
2023-01-15 01:56:40 +01:00
// ---------------------------------------------------------------------------------------------------------------------
func (g RegexMatchGroup) Value() string {
return g.haystack[g.start:g.end]
}
func (g RegexMatchGroup) Start() int {
return g.start
}
func (g RegexMatchGroup) End() int {
return g.end
}
func (g RegexMatchGroup) Range() (int, int) {
return g.start, g.end
}
func (g RegexMatchGroup) Length() int {
return g.end - g.start
}
2023-03-29 19:53:10 +02:00
// ---------------------------------------------------------------------------------------------------------------------
func (g OptRegexMatchGroup) Value() string {
return g.v.Value()
}
func (g OptRegexMatchGroup) ValueOrEmpty() string {
if g.v == nil {
return ""
}
return g.v.Value()
}
2023-03-29 19:53:53 +02:00
func (g OptRegexMatchGroup) ValueOrNil() *string {
if g.v == nil {
return nil
}
return langext.Ptr(g.v.Value())
}
2023-03-29 19:53:10 +02:00
func (g OptRegexMatchGroup) IsEmpty() bool {
return g.v == nil
}
func (g OptRegexMatchGroup) Exists() bool {
return g.v != nil
}
func (g OptRegexMatchGroup) Start() int {
return g.v.Start()
}
func (g OptRegexMatchGroup) End() int {
return g.v.End()
}
func (g OptRegexMatchGroup) Range() (int, int) {
return g.v.Range()
}
func (g OptRegexMatchGroup) Length() int {
return g.v.Length()
}