2022-03-08 10:45:56 +00:00
|
|
|
package main
|
|
|
|
|
|
|
|
import (
|
|
|
|
"regexp"
|
2024-01-20 15:41:54 +00:00
|
|
|
|
|
|
|
"github.com/rs/zerolog/log"
|
2022-03-08 10:45:56 +00:00
|
|
|
)
|
|
|
|
|
|
|
|
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")
|
2022-03-08 10:45:56 +00:00
|
|
|
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
|
|
|
|
}
|