Skip to main content

StreamTokenizer

Struct StreamTokenizer 

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

A streaming tokenizer that processes input chunk by chunk.

Maintains internal state so that token boundaries that span chunk boundaries are handled correctly.

§Example

use fhp_tokenizer::streaming::StreamTokenizer;
use fhp_tokenizer::token::Token;

let mut tokenizer = StreamTokenizer::new();
let mut all_tokens: Vec<Token<'static>> = Vec::new();

let html = b"<div>hello</div>";
// Feed in small chunks.
let owned = tokenizer.feed(&html[..5]);
all_tokens.extend(owned);
let owned = tokenizer.feed(&html[5..]);
all_tokens.extend(owned);
let owned = tokenizer.finish();
all_tokens.extend(owned);

assert!(all_tokens.iter().any(|t| matches!(t, Token::OpenTag { .. })));

Implementations§

Source§

impl StreamTokenizer

Source

pub fn new() -> Self

Create a new streaming tokenizer.

Source

pub fn feed(&mut self, chunk: &[u8]) -> Vec<Token<'static>>

Feed a chunk of UTF-8 input and return any complete tokens.

Tokens are returned as owned ('static lifetime) since the chunk data may not live long enough. Text content is cloned into Cow::Owned.

Source

pub fn feed_str_with(&mut self, chunk: &str, on_token: impl FnMut(&Token<'_>))

Feed a UTF-8 chunk and process complete tokens via callback without cloning.

This path is intended for internal high-throughput consumers (e.g. tree building) that can consume tokens immediately.

Source

pub fn finish(&mut self) -> Vec<Token<'static>>

Signal end of input and flush any remaining buffered data.

Source

pub fn finish_with(&mut self, on_token: impl FnMut(&Token<'_>))

Signal end of input and flush buffered tokens via callback without cloning.

Trait Implementations§

Source§

impl Default for StreamTokenizer

Source§

fn default() -> Self

Returns the “default value” for a type. 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.