Skip to main content

Extractor

Struct Extractor 

Source
pub struct Extractor { /* private fields */ }
Expand description

The main IP address extractor.

An Extractor scans byte slices for IPv4 and/or IPv6 addresses, applying configurable filters to include or exclude certain address classes (private, loopback, broadcast).

Extractors are best created via ExtractorBuilder and are designed to be reused across many calls to find_iter for maximum efficiency.

§Bytes vs. Strings

This extractor works directly on byte slices rather than strings. This avoids UTF-8 validation overhead and enables zero-copy scanning of very large inputs.

§Performance

The extractor uses a compile-time DFA (Deterministic Finite Automaton) for O(n) scanning with minimal overhead. See the crate-level documentation for throughput benchmarks.

Implementations§

Source§

impl Extractor

Source

pub fn find_iter<'a>( &'a self, haystack: &'a [u8], ) -> impl Iterator<Item = Range<usize>> + 'a

Find all IP addresses in a byte slice.

Returns an iterator of byte ranges [start, end) pointing to each IP address found. Ranges are guaranteed to be valid indices into haystack.

For richer match information (IP version, direct string access), use match_iter instead.

§Example
use ip_extract::ExtractorBuilder;

let extractor = ExtractorBuilder::new().build()?;
let data = b"Connecting from 192.168.1.1";

for range in extractor.find_iter(data) {
    let ip = std::str::from_utf8(&data[range])?;
    println!("Found: {ip}");
}
Source

pub fn match_iter<'a>( &'a self, haystack: &'a [u8], ) -> impl Iterator<Item = IpMatch<'a>> + 'a

Find all IP addresses in a byte slice, yielding rich IpMatch values.

Like find_iter, but each match carries the matched bytes, their position in the haystack, and the IP version — eliminating the need to re-parse or guess the version at the call site.

§Example
use ip_extract::ExtractorBuilder;

let extractor = ExtractorBuilder::new().build()?;
let data = b"Log: 192.168.1.1 sent request to 2001:db8::1";

for m in extractor.match_iter(data) {
    println!("{} ({:?})", m.as_matched_str(), m.kind());
}
Source

pub fn replace_iter<W, F>( &self, haystack: &[u8], wtr: &mut W, replacer: F, ) -> Result<usize>
where W: Write, F: FnMut(&IpMatch<'_>, &mut W) -> Result<()>,

Scan haystack for IP addresses, writing non-IP text to wtr and calling replacer for each match.

This is the efficient single-pass decoration primitive: the caller never needs to track byte offsets or manage gap writes. The replacer writes the substitution directly to wtr — no intermediate allocation.

Returns the number of IP addresses found.

§Errors

Returns the first io::Error from either a gap write or the replacer.

§Example
use ip_extract::ExtractorBuilder;
use std::io::Write;

let extractor = ExtractorBuilder::new().build()?;
let data = b"Server 192.168.1.1 is up";
let mut out = Vec::new();

let count = extractor.replace_iter(data, &mut out, |m, w| {
    write!(w, "[{}]", m.as_matched_str())
})?;

assert_eq!(count, 1);
assert_eq!(out, b"Server [192.168.1.1] is up");

Trait Implementations§

Source§

impl Debug for Extractor

Source§

fn fmt(&self, f: &mut Formatter<'_>) -> Result

Formats the value using the given formatter. Read more

Auto Trait Implementations§

Blanket Implementations§

Source§

impl<T> Any for T
where T: 'static + ?Sized,

Source§

fn type_id(&self) -> TypeId

Gets the TypeId of self. Read more
Source§

impl<T> Borrow<T> for T
where T: ?Sized,

Source§

fn borrow(&self) -> &T

Immutably borrows from an owned value. Read more
Source§

impl<T> BorrowMut<T> for T
where T: ?Sized,

Source§

fn borrow_mut(&mut self) -> &mut T

Mutably borrows from an owned value. Read more
Source§

impl<T> From<T> for T

Source§

fn from(t: T) -> T

Returns the argument unchanged.

Source§

impl<T, U> Into<U> for T
where U: From<T>,

Source§

fn into(self) -> U

Calls U::from(self).

That is, this conversion is whatever the implementation of From<T> for U chooses to do.

Source§

impl<T, U> TryFrom<U> for T
where U: Into<T>,

Source§

type Error = Infallible

The type returned in the event of a conversion error.
Source§

fn try_from(value: U) -> Result<T, <T as TryFrom<U>>::Error>

Performs the conversion.
Source§

impl<T, U> TryInto<U> for T
where U: TryFrom<T>,

Source§

type Error = <U as TryFrom<T>>::Error

The type returned in the event of a conversion error.
Source§

fn try_into(self) -> Result<U, <U as TryFrom<T>>::Error>

Performs the conversion.