Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add patched regex-automata #18

Merged
merged 2 commits into from
Jul 28, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
40 changes: 40 additions & 0 deletions Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

5 changes: 5 additions & 0 deletions Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -4,3 +4,8 @@ version = "0.1.0"
edition = "2021"

[dependencies]
anyhow = "1.0.86"
regex-automata = "0.4.7"

[patch.crates-io]
regex-automata = { git = 'https://github.com/f-forcher/regex', branch = 'expose-state-iter' }
35 changes: 33 additions & 2 deletions src/main.rs
Original file line number Diff line number Diff line change
@@ -1,3 +1,34 @@
fn main() {
println!("Hello, world!");
use anyhow::Result;
use regex_automata::{
dfa::{dense, Automaton},
Anchored, Input,
};

fn main() -> Result<()> {
let dfa = dense::DFA::new(r"[0-9]*\.?[0-9]*")?;
let haystack = "1.34";

// The start state is determined by inspecting the position and the
// initial bytes of the haystack.
let mut state = dfa.start_state_forward(&Input::new(haystack).anchored(Anchored::Yes))?;
// Walk all the bytes in the haystack.
for &b in haystack.as_bytes().iter() {
state = dfa.next_state(state, b);
}

let states_num = dfa.tt.len();

println!("There are {} states in this dfa", states_num);

let states_ids: Vec<_> = dfa.tt.states().map(|state| state.id()).collect();

println!("Their IDs are {:?}", states_ids);

// DFAs in this crate require an explicit
// end-of-input transition if a search reaches
// the end of a haystack.
state = dfa.next_eoi_state(state);
assert!(dfa.is_match_state(state));

Ok(())
}