use crate::cursor; use crate::extractor::boundary::{has_valid_boundaries, is_valid_before_boundary}; use crate::extractor::machine::{Machine, MachineState}; use crate::extractor::utility_machine::UtilityMachine; use crate::extractor::variant_machine::VariantMachine; use crate::extractor::Span; /// Extract full candidates including variants and utilities. #[derive(Debug, Default)] pub struct CandidateMachine { /// Start position of the candidate start_pos: usize, /// End position of the last variant (if any) last_variant_end_pos: Option, /// Position from which the input has no valid before boundary characters (if known). This /// is not reset, because it only depends on the input. no_boundary_from: Option, utility_machine: UtilityMachine, variant_machine: VariantMachine, } impl Machine for CandidateMachine { #[inline(always)] fn reset(&mut self) { self.start_pos = 0; self.last_variant_end_pos = None; } #[inline] fn next(&mut self, cursor: &mut cursor::Cursor<'_>) -> MachineState { self.next_with_checkpoints(cursor, |_| true) } } impl CandidateMachine { /// Same as `next`, but calls `checkpoint` with the cursor position each time the machine /// starts from a clean state. From such a position, the machine always finds the same /// candidates, no matter where it started. /// /// If `checkpoint` returns `false`, the machine stops and returns `MachineState::Idle`. #[inline] pub fn next_with_checkpoints( &mut self, cursor: &mut cursor::Cursor<'_>, mut checkpoint: impl FnMut(usize) -> bool, ) -> MachineState { let len = cursor.input.len(); while cursor.pos < len { if self.last_variant_end_pos.is_none() && !checkpoint(cursor.pos) { return MachineState::Idle; } // Skip ahead for known characters that will never be part of a candidate. No need to // run any sub-machines. if cursor.curr().is_ascii_whitespace() { self.reset(); cursor.advance(); continue; } // Candidates don't start with these characters, so we can skip ahead. if matches!(cursor.curr(), b':' | b'"' | b'\'' | b'`') { self.reset(); cursor.advance(); continue; } // Jump ahead if the character is known to be an invalid boundary and we should start // at the next boundary even though "valid" candidates can exist. // // E.g.: `
` // ^^^ Valid candidate // ^ But this character makes it invalid // ^ Therefore we jump here // // E.g.: `Some Class` // ^ ^ Invalid, we can jump ahead to the next boundary // // Once we know that the rest of the input has no boundary, we don't search it again. // Otherwise, an input with many of these characters and no boundary takes quadratic // time. if matches!(cursor.curr(), b'<' | b'A'..=b'Z') { let search_end = self.no_boundary_from.unwrap_or(len); if cursor.pos < search_end { if let Some(offset) = cursor.input[cursor.pos..search_end] .iter() .position(|&c| is_valid_before_boundary(&c)) { self.reset(); cursor.advance_by(offset + 1); continue; } self.no_boundary_from = Some(cursor.pos); } return self.restart(); } let mut variant_cursor = cursor.clone(); let variant_machine_state = self.variant_machine.next(&mut variant_cursor); let mut utility_cursor = cursor.clone(); let utility_machine_state = self.utility_machine.next(&mut utility_cursor); match (variant_machine_state, utility_machine_state) { // No variant, but the utility machine completed (MachineState::Idle, MachineState::Done(utility_span)) => { cursor.move_to(utility_cursor.pos + 1); let span = match self.last_variant_end_pos { Some(end_pos) => { // Verify that the utility is touching the last variant if end_pos + 1 != utility_span.start { return self.restart(); } Span::new(self.start_pos, utility_span.end) } None => utility_span, }; // Ensure the span has valid boundary characters before and after if !has_valid_boundaries(&span, cursor.input) { return self.restart(); } return self.done_span(span); } // Both variant and utility machines are done // E.g.: `hover:flex` // ^^^^^^ Variant // ^^^^^ Utility // (MachineState::Done(variant_span), MachineState::Done(utility_span)) => { cursor.move_to(variant_cursor.pos + 1); if let Some(end_pos) = self.last_variant_end_pos { // Verify variant is touching the last variant if end_pos + 1 != variant_span.start { return self.restart(); } } else { // We know that there is no variant before this one. // // Edge case: JavaScript keys should be considered utilities if they are // not preceded by another variant, and followed by any kind of whitespace // or the end of the line. // // E.g.: `{ underline: true }` // ^^^^^^^^^^ Variant // ^^^^^^^^^ Utility (followed by `: `) let after = cursor.input.get(utility_span.end + 2).unwrap_or(&b'\0'); if after.is_ascii_whitespace() || *after == b'\0' { cursor.move_to(utility_cursor.pos + 2); return self.done_span(utility_span); } self.start_pos = variant_span.start; } self.last_variant_end_pos = Some(variant_cursor.pos); } // Variant is done, utility is invalid (MachineState::Done(variant_span), MachineState::Idle) => { cursor.move_to(variant_cursor.pos + 1); if let Some(end_pos) = self.last_variant_end_pos { if end_pos + 1 > variant_span.start { self.reset(); return MachineState::Idle; } } else { self.start_pos = variant_span.start; } self.last_variant_end_pos = Some(variant_cursor.pos); } (MachineState::Idle, MachineState::Idle) => { // Skip main cursor to the next character after both machines. We already know // there is no candidate here. if variant_cursor.pos > cursor.pos || utility_cursor.pos > cursor.pos { cursor.move_to(variant_cursor.pos.max(utility_cursor.pos)); } self.reset(); cursor.advance(); } } } MachineState::Idle } #[inline(always)] fn done_span(&mut self, span: Span) -> MachineState { self.reset(); MachineState::Done(span) } } #[cfg(test)] mod tests { use super::CandidateMachine; use crate::extractor::machine::Machine; use pretty_assertions::assert_eq; #[test] #[ignore] fn test_candidate_machine_performance() { let n = 10_000; let input = include_str!("../fixtures/example.html"); // let input = &r#"