Documentation
¶
Overview ¶
Package filter provides optimized regex patterns for sensitive data detection.
Package filter provides sensitive information filtering for AI outputs. It implements Issue #101: Sensitive information filtering with <1ms performance.
Index ¶
- func CompositePattern(types []FilterType) (*regexp.Regexp, error)
- func FastMatch(text string, types []FilterType) bool
- func GetPattern(ft FilterType) *regexp.Regexp
- func ValidateBankCard(s string) bool
- func ValidateEmail(s string) bool
- func ValidateIDCard(s string) bool
- func ValidateIP(s string) bool
- func ValidatePhone(s string) bool
- type FastScanner
- type Filter
- type FilterConfig
- type FilterStats
- type FilterType
- type Match
- type PatternSet
Constants ¶
This section is empty.
Variables ¶
This section is empty.
Functions ¶
func CompositePattern ¶
func CompositePattern(types []FilterType) (*regexp.Regexp, error)
CompositePattern creates a composite regex pattern from multiple filter types. This is more efficient for bulk scanning than running each pattern separately.
func FastMatch ¶
func FastMatch(text string, types []FilterType) bool
FastMatch performs a fast match check using precompiled patterns. Returns true if any sensitive data pattern matches the input text.
func GetPattern ¶
func GetPattern(ft FilterType) *regexp.Regexp
GetPattern returns a precompiled regex pattern for the given filter type.
func ValidateBankCard ¶
ValidateBankCard checks if a string is a valid bank card number.
func ValidateEmail ¶
ValidateEmail checks if a string is a valid email address.
func ValidateIDCard ¶
ValidateIDCard checks if a string is a valid Chinese ID card number.
func ValidateIP ¶
ValidateIP checks if a string is a valid IPv4 address.
func ValidatePhone ¶
ValidatePhone checks if a string is a valid Chinese mobile phone number.
Types ¶
type FastScanner ¶
type FastScanner struct {
// contains filtered or unexported fields
}
FastScanner provides high-performance scanning for sensitive data. It uses a composite pattern for single-pass scanning.
func NewFastScanner ¶
func NewFastScanner(types []FilterType) (*FastScanner, error)
NewFastScanner creates a new fast scanner for the given types.
func (*FastScanner) HasAny ¶
func (fs *FastScanner) HasAny(text string) bool
HasAny returns true if text contains any sensitive data.
func (*FastScanner) Scan ¶
func (fs *FastScanner) Scan(text string) []Match
Scan scans text for sensitive data matches.
type Filter ¶
type Filter struct {
// contains filtered or unexported fields
}
Filter filters sensitive information from text.
func DefaultFilter ¶
func DefaultFilter() *Filter
DefaultFilter creates a filter with default configuration.
func NewFilter ¶
func NewFilter(cfg FilterConfig) *Filter
NewFilter creates a new sensitive information filter.
func (*Filter) FilterText ¶
FilterText filters sensitive information from text.
func (*Filter) FilterWithOptions ¶
FilterWithOptions filters text with custom options.
func (*Filter) FindMatches ¶
FindMatches finds all sensitive information matches in text.
func (*Filter) GetStats ¶
func (f *Filter) GetStats() *FilterStats
GetStats returns filter statistics.
type FilterConfig ¶
type FilterConfig struct {
// Enabled filter types.
Enabled []FilterType
// MaskChar is the character used for masking.
MaskChar rune
// PreserveLength determines whether to preserve original length.
PreserveLength bool
// KeepFirstN keeps first N characters unmasked.
KeepFirstN int
// KeepLastN keeps last N characters unmasked.
KeepLastN int
}
FilterConfig configures the sensitive information filter.
func DefaultConfig ¶
func DefaultConfig() FilterConfig
DefaultConfig returns default filter configuration.
type FilterStats ¶
type FilterStats struct {
TotalFiltered int64
TotalMatches int64
PhoneMatches int64
IDCardMatches int64
EmailMatches int64
BankCardMatches int64
IPMatches int64
AverageLatency time.Duration
}
FilterStats contains filter statistics.
type FilterType ¶
type FilterType int
FilterType defines the type of sensitive information to filter.
const ( // Phone filters mobile phone numbers. Phone FilterType = iota // IDCard filters Chinese ID card numbers. IDCard // Email filters email addresses. Email // BankCard filters bank card numbers. BankCard // IP filters IP addresses. IP // All filters all known sensitive types. All )
type Match ¶
type Match struct {
Type FilterType
Start int
End int
Original string
Replaced string
}
Match represents a single match found in text.
func FindAllMatches ¶
func FindAllMatches(text string, types []FilterType) []Match
FindAllMatches finds all matches for the given filter types.
type PatternSet ¶
type PatternSet struct {
// contains filtered or unexported fields
}
PatternSet represents a set of compiled patterns for efficient matching.
func NewPatternSet ¶
func NewPatternSet(types []FilterType) *PatternSet
NewPatternSet creates a new pattern set with the given filter types.
func (*PatternSet) Add ¶
func (ps *PatternSet) Add(ft FilterType)
Add adds a new pattern type to the set.
func (*PatternSet) FindAll ¶
func (ps *PatternSet) FindAll(text string) []Match
FindAll finds all matches for patterns in the set.
func (*PatternSet) Has ¶
func (ps *PatternSet) Has(ft FilterType) bool
Has returns true if the pattern set contains the given type.
func (*PatternSet) Match ¶
func (ps *PatternSet) Match(text string) bool
Match checks if the text matches any pattern in the set.
func (*PatternSet) Remove ¶
func (ps *PatternSet) Remove(ft FilterType)
Remove removes a pattern type from the set.
func (*PatternSet) Types ¶
func (ps *PatternSet) Types() []FilterType
Types returns all filter types in the set.