changeset 57:dbc85d2608cc

lib: Export public API
author Lewin Bormann <lbo@spheniscida.de>
date Fri, 30 Aug 2019 15:55:21 +0200
parents cd03d1716725
children 7532e9000d89
files src/lib.rs src/state.rs
diffstat 2 files changed, 30 insertions(+), 0 deletions(-) [+]
line wrap: on
line diff
--- a/src/lib.rs	Fri Aug 30 15:54:37 2019 +0200
+++ b/src/lib.rs	Fri Aug 30 15:55:21 2019 +0200
@@ -9,6 +9,11 @@
 
 mod tests;
 
+
+fn parse(re: &str) -> Result<repr::Pattern, String> {
+    return parse::parse(re);
+}
+
 /// Compiles a parsed regular expression into the internal state graph and matches s against it.
 /// Returns whether the string matched as well as a list of submatches. The first submatch is the
 /// entire matched string. A submatch is a tuple of (start, end), where end is the index of the
@@ -16,3 +21,25 @@
 fn compile_and_match(re: &repr::Pattern, s: &str) -> (bool, Vec<(usize, usize)>) {
     matching::do_match(compile::start_compile(re), s)
 }
+
+/// Parse, compile, and match a regular expression. Not recommended for repeated use, as the
+/// regular expression will be compiled every time. Use `compile()` and `match_re()` to make this
+/// more efficient.
+pub fn match_re_str(re: &str, s: &str) -> Result<(bool, Vec<(usize, usize)>), String> {
+    return Ok(compile_and_match(&parse::parse(re)?, s));
+}
+
+/// 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(&parse(re)?))
+}
+
+/// Match a regular expression compiled with `compile()` against a string. Returns a tuple of a
+/// boolean (whether there was a match or partial match) and a vector of `(position, length)`
+/// 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)>) {
+    let re = re.clone();
+    matching::do_match(re, s)
+}
--- a/src/state.rs	Fri Aug 30 15:54:37 2019 +0200
+++ b/src/state.rs	Fri Aug 30 15:55:21 2019 +0200
@@ -34,6 +34,9 @@
 /// WrappedState is a shared pointer to a state node.
 pub type WrappedState = Rc<RefCell<State>>;
 
+/// CompiledRE is a compiled regular expression that can be used for matching.
+pub type CompiledRE = WrappedState;
+
 pub fn wrap_state(s: State) -> WrappedState {
     Rc::new(RefCell::new(s))
 }