From df5a2ae14c4976d404195e83e52b8db62be63988 Mon Sep 17 00:00:00 2001 From: Martin Fischer Date: Tue, 29 Aug 2023 07:24:55 +0200 Subject: 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. --- src/default_emitter.rs | 18 ++---------------- src/emitter.rs | 4 ++-- src/token.rs | 6 ++++-- src/tokenizer/machine/utils.rs | 13 +++++++------ 4 files changed, 15 insertions(+), 26 deletions(-) (limited to 'src') 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 { - current_characters: String, current_token: Option>, current_attribute: Option<(String, crate::token::AttrInternal)>, seen_attributes: BTreeSet, @@ -24,7 +22,6 @@ pub struct DefaultEmitter { impl Default for DefaultEmitter { fn default() -> Self { DefaultEmitter { - current_characters: String::new(), current_token: None, current_attribute: None, seen_attributes: BTreeSet::new(), @@ -56,11 +53,10 @@ impl Emitter for DefaultEmitter { } 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 Emitter for DefaultEmitter { impl DefaultEmitter { fn emit_token(&mut self, token: Token) { - self.flush_current_characters(); self.emitted_tokens.push_front(token); } @@ -358,15 +353,6 @@ impl DefaultEmitter { } } } - - 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 diff --git a/src/emitter.rs b/src/emitter.rs index 2a47f40..7a567b4 100644 --- a/src/emitter.rs +++ b/src/emitter.rs @@ -30,8 +30,8 @@ pub trait Emitter { /// The state machine has reached the end of the file. fn emit_eof(&mut self); - /// Emit a bunch of plain characters as character tokens. - fn emit_string(&mut self, c: &str); + /// Emits the given character as a character token. + fn emit_char(&mut self, c: char); /// Set the _current token_ to a start tag. fn init_start_tag(&mut self, tag_offset: O, name_offset: O); diff --git a/src/token.rs b/src/token.rs index c599cd5..cb584ff 100644 --- a/src/token.rs +++ b/src/token.rs @@ -9,12 +9,14 @@ use crate::offset::Offset; /// A type for the tokens emitted by a WHATWG-compliant HTML tokenizer. #[derive(Debug, Eq, PartialEq)] pub enum Token { + /// A literal character, a resolved character reference, + /// or part of a resolved character reference (since some + /// character references resolve to two `char`s). + Char(char), /// An HTML start tag. StartTag(StartTag), /// An HTML end tag. EndTag(EndTag), - /// A literal string. Character references have been resolved. - String(String), /// An HTML comment. Comment(Comment), /// An HTML doctype declaration. diff --git a/src/tokenizer/machine/utils.rs b/src/tokenizer/machine/utils.rs index d96e50b..ea4d697 100644 --- a/src/tokenizer/machine/utils.rs +++ b/src/tokenizer/machine/utils.rs @@ -16,7 +16,7 @@ where /// Emits the given character as a character token. #[inline] pub(super) fn emit_char(&mut self, c: char) { - self.emitter.emit_string(ctostr!(c)); + self.emitter.emit_char(c); } /// Emits every byte of the given byte slice as a character token. @@ -25,10 +25,9 @@ where /// since [`str::chars`] isn't `const`.) #[inline] pub(super) fn emit_chars(&mut self, s: &[u8]) { - self.emitter.emit_string( - // this unsafe block is only temporary and will be removed in the next commit - unsafe { std::str::from_utf8_unchecked(s) }, - ); + for c in s { + self.emit_char(*c as char); + } } #[inline] @@ -204,7 +203,9 @@ where } pub(super) fn flush_buffer_characters(&mut self) { - self.emitter.emit_string(&self.temporary_buffer); + for c in self.temporary_buffer.chars() { + self.emitter.emit_char(c); + } self.temporary_buffer.clear(); } } -- cgit v1.2.3