filter

package
v0.97.0 Latest Latest
Warning

This package is not in the latest version of its module.

Go to latest
Published: Feb 10, 2026 License: MIT Imports: 4 Imported by: 0

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

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

func ValidateBankCard(s string) bool

ValidateBankCard checks if a string is a valid bank card number.

func ValidateEmail

func ValidateEmail(s string) bool

ValidateEmail checks if a string is a valid email address.

func ValidateIDCard

func ValidateIDCard(s string) bool

ValidateIDCard checks if a string is a valid Chinese ID card number.

func ValidateIP

func ValidateIP(s string) bool

ValidateIP checks if a string is a valid IPv4 address.

func ValidatePhone

func ValidatePhone(s string) bool

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

func (f *Filter) FilterText(text string) string

FilterText filters sensitive information from text.

func (*Filter) FilterWithOptions

func (f *Filter) FilterWithOptions(text string, keepFirst, keepLast int, maskChar rune) string

FilterWithOptions filters text with custom options.

func (*Filter) FindMatches

func (f *Filter) FindMatches(text string) []Match

FindMatches finds all sensitive information matches in text.

func (*Filter) GetStats

func (f *Filter) GetStats() *FilterStats

GetStats returns filter statistics.

func (*Filter) Validate

func (f *Filter) Validate(text string) bool

Validate checks if text contains any unfiltered sensitive information.

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.

Jump to

Keyboard shortcuts

? : This menu
/ : Search site
f or F : Jump to
y or Y : Canonical URL