mirror of
https://github.com/go-i2p/go-connfilter.git
synced 2025-07-13 11:54:44 -04:00
fmt, implement missing IRC stuff
This commit is contained in:
@ -9,7 +9,7 @@ import (
|
||||
var ErrInvalidRegexFilter = errors.New("invalid regex filter")
|
||||
|
||||
type RegexConnFilter struct {
|
||||
net.Conn
|
||||
FunctionConnFilter
|
||||
match string
|
||||
}
|
||||
|
||||
@ -25,10 +25,44 @@ func (c *RegexConnFilter) Read(b []byte) (n int, err error) {
|
||||
return buffer.Len(), nil
|
||||
}
|
||||
|
||||
// NewRegexConnFilter creates a new RegexConnFilter that replaces occurrences of target regex with empty strings in the data read from the connection.
|
||||
// target regex is c.match
|
||||
func (c *RegexConnFilter) ReadFilter(b []byte) ([]byte, error) {
|
||||
if c.match == "" {
|
||||
return b, nil
|
||||
}
|
||||
|
||||
// Create a new bytes buffer
|
||||
var buffer bytes.Buffer
|
||||
|
||||
// Write the modified data to the buffer, with regex matches removed
|
||||
buffer.Write(bytes.ReplaceAll(b, []byte(c.match), []byte("")))
|
||||
|
||||
return buffer.Bytes(), nil
|
||||
}
|
||||
|
||||
// WriteFilter creates a new RegexConnFilter that replaces occurrences of target regex with empty strings in the data read from the connection.
|
||||
// target regex is c.match
|
||||
func (c *RegexConnFilter) WriteFilter(b []byte) ([]byte, error) {
|
||||
if c.match == "" {
|
||||
return b, nil
|
||||
}
|
||||
|
||||
// Create a new bytes buffer
|
||||
var buffer bytes.Buffer
|
||||
|
||||
// Write the modified data to the buffer, with regex matches removed
|
||||
buffer.Write(bytes.ReplaceAll(b, []byte(c.match), []byte("")))
|
||||
|
||||
return buffer.Bytes(), nil
|
||||
}
|
||||
|
||||
// NewRegexConnFilter creates a new RegexConnFilter that replaces occurrences of target regex with empty strings in the data read from the connection.
|
||||
// It returns an error if the lengths of target and replacement slices are not equal.
|
||||
func NewRegexConnFilter(parentConn net.Conn, regex string) (net.Conn, error) {
|
||||
return &RegexConnFilter{
|
||||
Conn: parentConn,
|
||||
FunctionConnFilter: FunctionConnFilter{
|
||||
Conn: parentConn,
|
||||
},
|
||||
}, nil
|
||||
}
|
||||
|
Reference in New Issue
Block a user