diff options
author | Martin Fischer <martin@push-f.com> | 2023-08-29 07:24:55 +0200 |
---|---|---|
committer | Martin Fischer <martin@push-f.com> | 2023-09-28 10:36:08 +0200 |
commit | df5a2ae14c4976d404195e83e52b8db62be63988 (patch) | |
tree | 5260ba3196ad6c9dc164dbfe32bb992cb96423c8 /src/default_emitter.rs | |
parent | 394c52260e861e911e2d8706d4904136a920da87 (diff) |
break!: emit chars instead of strings
The HTML spec specifies that the tokenizer emits character tokens.
That html5gum always emitted strings instead was probably just done
to make the token consumption more convenient. When it comes to tree
construction character tokens are however actually more convenient
than string tokens since the spec defines that specific character
tokens should be ignored in specific states (and character tokens
let us avoid string manipulation for these conditions).
This should also make the DefaultEmitter more performant for cases
where you don't actually need the strings at all (or only a few)
since it avoids string allocations. Though I haven't benchmarked it.
Diffstat (limited to 'src/default_emitter.rs')
-rw-r--r-- | src/default_emitter.rs | 18 |
1 files changed, 2 insertions, 16 deletions
diff --git a/src/default_emitter.rs b/src/default_emitter.rs index e89fa5e..5edf848 100644 --- a/src/default_emitter.rs +++ b/src/default_emitter.rs @@ -1,7 +1,6 @@ use std::collections::btree_map::Entry; use std::collections::BTreeSet; use std::collections::VecDeque; -use std::mem; use std::ops::Range; use crate::offset::NoopOffset; @@ -12,7 +11,6 @@ use crate::Error; /// The default implementation of [`Emitter`], used to produce tokens. pub struct DefaultEmitter<O = NoopOffset> { - current_characters: String, current_token: Option<Token<O>>, current_attribute: Option<(String, crate::token::AttrInternal<O>)>, seen_attributes: BTreeSet<String>, @@ -24,7 +22,6 @@ pub struct DefaultEmitter<O = NoopOffset> { impl<O> Default for DefaultEmitter<O> { fn default() -> Self { DefaultEmitter { - current_characters: String::new(), current_token: None, current_attribute: None, seen_attributes: BTreeSet::new(), @@ -56,11 +53,10 @@ impl<O: Offset> Emitter<O> for DefaultEmitter<O> { } fn emit_eof(&mut self) { - self.flush_current_characters(); } - fn emit_string(&mut self, s: &str) { - self.current_characters.push_str(s); + fn emit_char(&mut self, c: char) { + self.emit_token(Token::Char(c)); } fn init_start_tag(&mut self, tag_offset: O, name_offset: O) { @@ -328,7 +324,6 @@ impl<O: Offset> Emitter<O> for DefaultEmitter<O> { impl<O> DefaultEmitter<O> { fn emit_token(&mut self, token: Token<O>) { - self.flush_current_characters(); self.emitted_tokens.push_front(token); } @@ -358,15 +353,6 @@ impl<O> DefaultEmitter<O> { } } } - - fn flush_current_characters(&mut self) { - if self.current_characters.is_empty() { - return; - } - - let s = mem::take(&mut self.current_characters); - self.emit_token(Token::String(s)); - } } /// The majority of our testing of the [`DefaultEmitter`] is done against the |