Documentation
¶
Index ¶
- type IPBinarySet
- func (s *IPBinarySet) AddAddr(addr netip.Addr)
- func (s *IPBinarySet) AddPrefix(prefix netip.Prefix)
- func (s *IPBinarySet) All() iter.Seq[netip.Prefix]
- func (s *IPBinarySet) ContainsAddr(addr netip.Addr) bool
- func (s *IPBinarySet) ContainsPrefix(prefix netip.Prefix) bool
- func (s *IPBinarySet) Copy() []netip.Prefix
- func (s *IPBinarySet) Len() int
- func (s *IPBinarySet) MarshalJSON() ([]byte, error)
- func (s *IPBinarySet) Remove(prefix netip.Prefix) bool
- func (s *IPBinarySet) String() string
- func (s *IPBinarySet) UnmarshalJSON(data []byte) error
- func (s *IPBinarySet) UnmarshalText(text []byte) error
- type ParseError
Constants ¶
This section is empty.
Variables ¶
This section is empty.
Functions ¶
This section is empty.
Types ¶
type IPBinarySet ¶
type IPBinarySet struct {
// contains filtered or unexported fields
}
IPBinarySet is a fast and memory-efficient data structure for storing and querying IP address ranges. It maintains a sorted, merged list of IPv4 and IPv6 CIDR prefixes, making it ideal for IP blocklists, allowlists, and other IP filtering applications.
The set uses binary search for O(log n) lookup performance and automatically merges overlapping or contained ranges to minimize memory usage and maintain optimal query speed. IPv4 and IPv6 addresses are stored separately in their respective optimized formats.
func (*IPBinarySet) AddAddr ¶
func (s *IPBinarySet) AddAddr(addr netip.Addr)
AddAddr adds a single IP address to the set.
func (*IPBinarySet) AddPrefix ¶
func (s *IPBinarySet) AddPrefix(prefix netip.Prefix)
AddPrefix adds a CIDR prefix to the set. Ranges that are contained by the new prefix are automatically removed. If the prefix is already contained by an existing range, it is not added.
func (*IPBinarySet) All ¶
func (s *IPBinarySet) All() iter.Seq[netip.Prefix]
All returns an iterator over all prefixes in the set.
func (*IPBinarySet) ContainsAddr ¶
func (s *IPBinarySet) ContainsAddr(addr netip.Addr) bool
ContainsAddr reports whether the set contains the given IP address.
func (*IPBinarySet) ContainsPrefix ¶
func (s *IPBinarySet) ContainsPrefix(prefix netip.Prefix) bool
ContainsPrefix reports whether the set fully contains the given prefix.
func (*IPBinarySet) Copy ¶
func (s *IPBinarySet) Copy() []netip.Prefix
Copy returns a copy of all prefixes in the set as a slice.
func (*IPBinarySet) Len ¶
func (s *IPBinarySet) Len() int
Len returns the number of prefixes in the set.
func (*IPBinarySet) MarshalJSON ¶
func (s *IPBinarySet) MarshalJSON() ([]byte, error)
MarshalJSON implements json.Marshaler.
func (*IPBinarySet) Remove ¶
func (s *IPBinarySet) Remove(prefix netip.Prefix) bool
Remove removes a prefix from the set if it exists. Returns true if the prefix was found and removed.
func (*IPBinarySet) String ¶
func (s *IPBinarySet) String() string
String returns a string representation of all prefixes in the set. Output is newline-delimited with a trailing newline.
func (*IPBinarySet) UnmarshalJSON ¶
func (s *IPBinarySet) UnmarshalJSON(data []byte) error
UnmarshalJSON implements json.Unmarshaler.
func (*IPBinarySet) UnmarshalText ¶
func (s *IPBinarySet) UnmarshalText(text []byte) error
UnmarshalText implements text.Unmarshaler.
It parses newline-delimited text as a list of IP prefixes and adds them to the set. It trims spaces, supports comments via `# comment`, ignores empty lines, and allows both single IP addresses and CIDR notation.
type ParseError ¶
ParseError represents an error that occurred while parsing an IP address or CIDR prefix.
func (*ParseError) Error ¶
func (e *ParseError) Error() string
Error returns a string representation of the error.