Skip to main content

parse_str

Function parse_str 

Source
pub fn parse_str(input: &str, opts: &ParseOptions) -> Result<Document>
Expand description

Parse input into an arena-allocated Document. Uses default ParseOptions.

Examples found in repository?
examples/profile_xpath_slow_unit.rs (line 65)
59fn main() {
60    let path = std::env::args().nth(1)
61        .expect("usage: profile_xpath_slow_unit <artifact-path>");
62    let bytes = std::fs::read(&path).expect("read artifact");
63    let full = std::str::from_utf8(&bytes).expect("artifact is UTF-8");
64
65    let doc = parse_str(FIXTURE, &ParseOptions::default()).expect("fixture parses");
66    let ctx = XPathContext::new(&doc);
67
68    // Report the predicate-nesting depth of the original artifact so we
69    // can pick a sensible MAX_PREDICATE_NESTING_DEPTH.
70    use sup_xml_core::xpath::ast::max_predicate_nesting;
71    use sup_xml_core::xpath::parse_xpath;
72    match parse_xpath(full) {
73        Ok(e)  => println!("# original artifact parses; predicate-nesting depth = {}", max_predicate_nesting(&e)),
74        Err(e) => println!("# original artifact parse-error: {}",
75                           e.message.chars().take(80).collect::<String>()),
76    }
77
78    println!("# best-of-3 timing of slow-unit reductions");
79    println!("{:>9}  {:>8}  {}", "time", "len", "expression  →  result");
80    println!("{}", "-".repeat(78));
81
82    // ── baseline ──────────────────────────────────────────────────────────
83    time_one(&ctx, "FULL artifact (1018 B)", full);
84
85    // ── strip from the right, halving each time ──────────────────────────
86    time_one(&ctx, "first 512 B",  &full[..full.len().min(512)]);
87    time_one(&ctx, "first 256 B",  &full[..full.len().min(256)]);
88    time_one(&ctx, "first 128 B",  &full[..full.len().min(128)]);
89    time_one(&ctx, "first  64 B",  &full[..full.len().min(64)]);
90
91    // ── isolate specific patterns ────────────────────────────────────────
92    time_one(&ctx, "just nested substring chains",
93        "r*substring(/*substring(/*substring(/*substring(/*substring(/*substring(/*substring(//book[author='Hu'])))))))");
94    time_one(&ctx, "deep //*/*/* descent chain",
95        "//*/*/*/*/*/*/*/*/*/*/*/*/*/*/*/*/*/*/*/*/*/*/*/*/*/*/*/*/*/*");
96    time_one(&ctx, "nested predicates (the worst case)",
97        "//*[//*[//*[//*[//*[//*[//*[.='x']]]]]]]");
98    time_one(&ctx, "nested //*/* in predicate",
99        "//*[/*//*//*//*//*//*//*//*//*//*]");
100    time_one(&ctx, "wildcard arithmetic chain",
101        "//*/*/*/* * * * * * * * * * //*/*/*");
102    time_one(&ctx, "many top-level *",
103        "* * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * *");
104
105    // ── single-step costs for comparison ─────────────────────────────────
106    time_one(&ctx, "trivial root",                                 "/");
107    time_one(&ctx, "trivial wildcard",                             "/*");
108    time_one(&ctx, "all elements",                                 "//*");
109    time_one(&ctx, "all elements with trivial predicate",          "//*[true()]");
110    time_one(&ctx, "all elements with attribute access",           "//*[@id]");
111    time_one(&ctx, "books filtered by author",                     "//book[author='Hunt']");
112    time_one(&ctx, "substring on string literal",                  "substring('hello', 2, 3)");
113    time_one(&ctx, "substring on node text",                       "substring(//title, 2, 3)");
114}