This commit is contained in:
Mike Schwörer 2023-03-29 19:53:10 +02:00
parent 8e40deae6a
commit dda2418255
Signed by: Mikescher
GPG Key ID: D3C7172E0A70F8CF
2 changed files with 53 additions and 2 deletions

View File

@ -21,7 +21,7 @@ if [ "$( git rev-parse --abbrev-ref HEAD )" != "master" ]; then
exit 1 exit 1
fi fi
git pull --rebase git pull --ff
curr_vers=$(git describe --tags --abbrev=0 | sed 's/v//g') curr_vers=$(git describe --tags --abbrev=0 | sed 's/v//g')

View File

@ -29,6 +29,10 @@ type RegexMatchGroup struct {
end int end int
} }
type OptRegexMatchGroup struct {
v *RegexMatchGroup
}
func W(rex *regexp.Regexp) Regex { func W(rex *regexp.Regexp) Regex {
return &regexWrapper{rex: rex, subnames: rex.SubexpNames()} return &regexWrapper{rex: rex, subnames: rex.SubexpNames()}
} }
@ -97,7 +101,7 @@ func (m RegexMatch) GroupByIndex(idx int) RegexMatchGroup {
return RegexMatchGroup{haystack: m.haystack, start: m.submatchesIndex[idx*2], end: m.submatchesIndex[idx*2+1]} return RegexMatchGroup{haystack: m.haystack, start: m.submatchesIndex[idx*2], end: m.submatchesIndex[idx*2+1]}
} }
// GroupByName returns the value of a matched group (group 0 == whole match) // GroupByName returns the value of a matched group (panics if not found!)
func (m RegexMatch) GroupByName(name string) RegexMatchGroup { func (m RegexMatch) GroupByName(name string) RegexMatchGroup {
for idx, subname := range m.subnames { for idx, subname := range m.subnames {
if subname == name { if subname == name {
@ -107,6 +111,16 @@ func (m RegexMatch) GroupByName(name string) RegexMatchGroup {
panic("failed to find regex-group by name") panic("failed to find regex-group by name")
} }
// 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 {
if subname == name {
return OptRegexMatchGroup{&RegexMatchGroup{haystack: m.haystack, start: m.submatchesIndex[idx*2], end: m.submatchesIndex[idx*2+1]}}
}
}
return OptRegexMatchGroup{}
}
// --------------------------------------------------------------------------------------------------------------------- // ---------------------------------------------------------------------------------------------------------------------
func (g RegexMatchGroup) Value() string { func (g RegexMatchGroup) Value() string {
@ -128,3 +142,40 @@ func (g RegexMatchGroup) Range() (int, int) {
func (g RegexMatchGroup) Length() int { func (g RegexMatchGroup) Length() int {
return g.end - g.start return g.end - g.start
} }
// ---------------------------------------------------------------------------------------------------------------------
func (g OptRegexMatchGroup) Value() string {
return g.v.Value()
}
func (g OptRegexMatchGroup) ValueOrEmpty() string {
if g.v == nil {
return ""
}
return g.v.Value()
}
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()
}