mirror of
https://github.com/QuantumNous/new-api.git
synced 2026-03-30 04:40:59 +00:00
34 lines
656 B
Go
34 lines
656 B
Go
package openaicompat
|
|
|
|
import (
|
|
"regexp"
|
|
"sync"
|
|
)
|
|
|
|
var compiledRegexCache sync.Map // map[string]*regexp.Regexp
|
|
|
|
func matchAnyRegex(patterns []string, s string) bool {
|
|
if len(patterns) == 0 || s == "" {
|
|
return false
|
|
}
|
|
for _, pattern := range patterns {
|
|
if pattern == "" {
|
|
continue
|
|
}
|
|
re, ok := compiledRegexCache.Load(pattern)
|
|
if !ok {
|
|
compiled, err := regexp.Compile(pattern)
|
|
if err != nil {
|
|
// Treat invalid patterns as non-matching to avoid breaking runtime traffic.
|
|
continue
|
|
}
|
|
re = compiled
|
|
compiledRegexCache.Store(pattern, re)
|
|
}
|
|
if re.(*regexp.Regexp).MatchString(s) {
|
|
return true
|
|
}
|
|
}
|
|
return false
|
|
}
|