changeset 83:715c12404500

Add utility function substring()
author Lewin Bormann <lbo@spheniscida.de>
date Sun, 22 Mar 2020 16:31:11 +0100
parents 7de8c3011ce3
children dae8f2b0874a
files src/lib.rs src/state.rs
diffstat 2 files changed, 11 insertions(+), 4 deletions(-) [+]
line wrap: on
line diff
--- a/src/lib.rs	Wed Mar 18 11:31:56 2020 +0100
+++ b/src/lib.rs	Sun Mar 22 16:31:11 2020 +0100
@@ -9,6 +9,13 @@
 
 mod tests;
 
+use std::iter::FromIterator;
+
+/// Easily take a substring from a match tuple.
+pub fn substring(s: &str, (from, len): (usize, usize)) -> String {
+    String::from_iter(s.chars().skip(from).take(len))
+}
+
 /// Render the state machine generated from `re` as graphviz `dot` input. The result can be pasted
 /// into `visualize.sh`, which renders a PNG image from it.
 pub fn render_graph(re: &str) -> String {
@@ -46,9 +53,9 @@
 /// Optimize and compile a regular expression into a representation that can be directly used for
 /// matching with `match_re()`.
 pub fn compile(re: &str) -> Result<state::CompiledRE, String> {
-    Ok(compile::start_compile(&repr::optimize::optimize(parse(
+    Ok(state::CompiledRE(compile::start_compile(&repr::optimize::optimize(parse(
         re,
-    )?)))
+    )?))))
 }
 
 /// Match a regular expression compiled with `compile()` against a string. Returns a tuple of a
@@ -56,5 +63,5 @@
 /// tuples for all submatches, where the first element describes the match by the whole regular
 /// expression.
 pub fn match_re(re: &state::CompiledRE, s: &str) -> (bool, Vec<(usize, usize)>) {
-    matching::do_match(re, s)
+    matching::do_match(&re.0, s)
 }
--- a/src/state.rs	Wed Mar 18 11:31:56 2020 +0100
+++ b/src/state.rs	Sun Mar 22 16:31:11 2020 +0100
@@ -21,7 +21,7 @@
 pub type StateRef = usize;
 
 /// CompiledRE is a compiled regular expression that can be used for matching.
-pub type CompiledRE = StateGraph;
+pub struct CompiledRE (pub(crate) StateGraph);
 
 /// State is a single state that the evaluation can be in. It contains several output states as
 /// well as a matcher.