mirror of
https://gitclone.com/github.com/MetaCubeX/Clash.Meta
synced 2024-11-16 06:01:21 +08:00
61 lines
1.5 KiB
Go
61 lines
1.5 KiB
Go
|
package strmatcher
|
||
|
|
||
|
import (
|
||
|
"regexp"
|
||
|
)
|
||
|
|
||
|
// Matcher is the interface to determine a string matches a pattern.
|
||
|
type Matcher interface {
|
||
|
// Match returns true if the given string matches a predefined pattern.
|
||
|
Match(string) bool
|
||
|
String() string
|
||
|
}
|
||
|
|
||
|
// Type is the type of the matcher.
|
||
|
type Type byte
|
||
|
|
||
|
const (
|
||
|
// Full is the type of matcher that the input string must exactly equal to the pattern.
|
||
|
Full Type = iota
|
||
|
// Substr is the type of matcher that the input string must contain the pattern as a sub-string.
|
||
|
Substr
|
||
|
// Domain is the type of matcher that the input string must be a sub-domain or itself of the pattern.
|
||
|
Domain
|
||
|
// Regex is the type of matcher that the input string must matches the regular-expression pattern.
|
||
|
Regex
|
||
|
)
|
||
|
|
||
|
// New creates a new Matcher based on the given pattern.
|
||
|
func (t Type) New(pattern string) (Matcher, error) {
|
||
|
// 1. regex matching is case-sensitive
|
||
|
switch t {
|
||
|
case Full:
|
||
|
return fullMatcher(pattern), nil
|
||
|
case Substr:
|
||
|
return substrMatcher(pattern), nil
|
||
|
case Domain:
|
||
|
return domainMatcher(pattern), nil
|
||
|
case Regex:
|
||
|
r, err := regexp.Compile(pattern)
|
||
|
if err != nil {
|
||
|
return nil, err
|
||
|
}
|
||
|
return ®exMatcher{
|
||
|
pattern: r,
|
||
|
}, nil
|
||
|
default:
|
||
|
panic("Unknown type")
|
||
|
}
|
||
|
}
|
||
|
|
||
|
// IndexMatcher is the interface for matching with a group of matchers.
|
||
|
type IndexMatcher interface {
|
||
|
// Match returns the index of a matcher that matches the input. It returns empty array if no such matcher exists.
|
||
|
Match(input string) []uint32
|
||
|
}
|
||
|
|
||
|
type matcherEntry struct {
|
||
|
m Matcher
|
||
|
id uint32
|
||
|
}
|