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
impl StreamTokenizer
Sourcepub fn feed(&mut self, chunk: &[u8]) -> Vec<Token<'static>>
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.
Sourcepub fn feed_str_with(&mut self, chunk: &str, on_token: impl FnMut(&Token<'_>))
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.
Sourcepub fn finish(&mut self) -> Vec<Token<'static>>
pub fn finish(&mut self) -> Vec<Token<'static>>
Signal end of input and flush any remaining buffered data.
Sourcepub fn finish_with(&mut self, on_token: impl FnMut(&Token<'_>))
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§
Auto Trait Implementations§
impl Freeze for StreamTokenizer
impl RefUnwindSafe for StreamTokenizer
impl Send for StreamTokenizer
impl Sync for StreamTokenizer
impl Unpin for StreamTokenizer
impl UnsafeUnpin for StreamTokenizer
impl UnwindSafe for StreamTokenizer
Blanket Implementations§
Source§impl<T> BorrowMut<T> for Twhere
T: ?Sized,
impl<T> BorrowMut<T> for Twhere
T: ?Sized,
Source§fn borrow_mut(&mut self) -> &mut T
fn borrow_mut(&mut self) -> &mut T
Mutably borrows from an owned value. Read more