initrd/regexp.go

41 lines
707 B
Go
Raw Normal View History

package main
import (
"regexp"
2024-01-20 15:41:54 +00:00
"github.com/rs/zerolog/log"
)
func regexpSelectN(n int, regexps []string, names []string) (matches []string) {
if n <= 0 {
matches = make([]string, 0)
} else {
matches = make([]string, 0, n)
}
res := make([]*regexp.Regexp, 0, len(regexps))
for _, reStr := range regexps {
re, err := regexp.Compile(reStr)
if err != nil {
2024-01-20 15:41:54 +00:00
log.Warn().Err(err).Str("regexp", reStr).Msg("invalid regexp, ignored")
continue
}
res = append(res, re)
}
namesLoop:
for _, name := range names {
if len(matches) == n {
break
}
for _, re := range res {
if re.MatchString(name) {
matches = append(matches, name)
continue namesLoop
}
}
}
return
}