use monster_regex::{Flags, Regex};
use std::time::Instant;
fn run_profile(name: &str, pattern: &str, input: &str) {
println!("--- Profiling: {} ---", name);
println!("Pattern: \"{}\"", pattern);
let input_display = if input.len() > 50 {
format!("{}... (len={})", &input[..50], input.len())
} else {
format!("\"{}\"", input)
};
println!("Input: {}", input_display);
if let Ok(re) = Regex::new_linear(pattern, Flags::default()) {
println!("\n[Linear Engine]");
let start = Instant::now();
let count = re.find_all(input).count();
let dur = start.elapsed();
println!("Matches: {}", count);
println!("Time: {:.4}ms", dur.as_secs_f64() * 1000.0);
} else {
println!("\n[Linear Engine] Compile Failed");
}
if let Ok(re) = Regex::new(pattern, Flags::default()) {
println!("\n[Backtracking Engine]");
let start = Instant::now();
let count = re.find_all(input).count();
let dur = start.elapsed();
println!("Matches: {}", count);
println!("Time: {:.4}ms", dur.as_secs_f64() * 1000.0);
} else {
println!("\n[Backtracking Engine] Compile Failed");
}
println!("--------------------------------------------------\n");
}
fn generate_dna(len: usize) -> String {
let chars = ['A', 'C', 'G', 'T'];
(0..len).map(|i| chars[i % 4]).collect()
}
fn main() {
println!("=== Monster Regex Profiling Suite ===");
println!(
"To see internal metrics, run with: cargo run --features internal_metrics --bin profile_runner\n"
);
run_profile("Literal Short", "foo", "foo bar baz foo");
run_profile(
"Literal Long",
"supercalifragilistic",
&"supercalifragilisticexpialidocious".repeat(100),
);
run_profile("Class Digit", r"\d+", "Item 1, Item 20, Item 300");
run_profile("Class Custom", "[a-c]", "abracadabra");
run_profile("Anchor Start", "^Hello", "Hello World\nHello Rust");
run_profile("Anchor End", "End$", "Start to End\nJust End");
run_profile("Word Boundary", r"\bword\b", "word sword password word");
run_profile("Quantifier Star", "a*", "baaaab");
run_profile("Quantifier Plus", "a+", "baaaab");
run_profile("Quantifier Question", "a?", "baaaab");
run_profile("Quantifier Range", "a{2,4}", "aa aaa aaaa aaaaa");
run_profile("Alternation Simple", "cat|dog", "I have a cat and a dog.");
run_profile("Alternation Overlap", "cal|calendar", "calendar calculator");
run_profile("Group Capture", r"(a)b", "ab ac ad ab");
let dna = generate_dna(10_000);
run_profile("DNA Complex", r"[AC]+G+[TA]+", &dna);
run_profile("Pathological (a+)+$", r"(a+)+$", "aaaaaaaaaaaaaaaaaaaa");
}