rust-lang / regex

An implementation of regular expressions for Rust. This implementation uses finite automata and guarantees linear time matching on all inputs.

Home Page:https://docs.rs/regex

Geek Repo:Geek Repo

Github PK Tool:Github PK Tool

Onepass DFA always has empty captures (user error)

Crypto-Spartan opened this issue · comments

What version of regex are you using?

regex-automata = "0.4.6"

Describe the bug:

With the following code:

use regex_automata::{
    dfa::{onepass, Automaton},
    nfa::thompson,
    util::syntax,
    Anchored, HalfMatch, Input, Match, MatchKind
};

fn main() {
    let test_nfa_conf = thompson::NFA::config()
        .which_captures(thompson::WhichCaptures::None)
        .utf8(false)
        .shrink(true)
        .reverse(false);
    let test_dfa = onepass::DFA::builder()
        .syntax(syntax::Config::new().unicode(false).utf8(false))
        .thompson(test_nfa_conf)
        .build(r"^dead(beef)$")
        .unwrap();
    
    let haystack = "deadbeef";
    let input = Input::new(haystack).anchored(Anchored::Yes);
    
    let (mut cache, mut caps) = (test_dfa.create_cache(), test_dfa.create_captures());
    test_dfa.try_search(&mut cache, &input, &mut caps).unwrap();
    
    dbg!(&cache);
    dbg!(&caps);
    dbg!(&caps.get_match());
}

I get the following output:

image

I would expect to see a capture on beef, but the captures are empty. If I'm doing something wrong, I'm terribly sorry for wasting your time. However, I've played around with this for almost 2 hours in various formats and I still can't get it to work.

Based on the docs for onepass::DFA, this code seemed correct, but I'm not sure what's going on.

You're explicitly disabling captures in the NFA with thompson::WhichCaptures::None.

🤦

I'm so sorry. This is what happens when messing with stuff too late into the night. Next time I'll try to remember to just go to bed.