From 7697b9ffb3b4c50a6a7db017931c53852e338b3b Mon Sep 17 00:00:00 2001 From: Steve Donovan Date: Tue, 14 Nov 2017 14:22:04 +0200 Subject: [PATCH] 2- and 3-capture versions of capture_mayble --- src/lib.rs | 45 +++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 45 insertions(+) diff --git a/src/lib.rs b/src/lib.rs index fbad099..4ffb6c2 100644 --- a/src/lib.rs +++ b/src/lib.rs @@ -174,6 +174,43 @@ impl <'a> LuaPattern<'a> { None } } + + /// Match a string, returning first two explicit captures if successful + /// + /// ``` + /// let mut p = lua_patterns::LuaPattern::new("%s*(%d+)%s+(%S+)"); + /// let (int,rest) = p.match_maybe_2(" 233 hello dolly").unwrap(); + /// assert_eq!(int,"233"); + /// assert_eq!(rest,"hello"); + /// ``` + pub fn match_maybe_2<'t>(&mut self, text: &'t str) -> Option<(&'t str,&'t str)> { + if self.matches(text) { + let cc = self.match_captures(text); + if cc.num_matches() != 3 { return None; } + Some((cc.get(1),cc.get(2))) + } else { + None + } + } + + /// Match a string, returning first three explicit captures if successful + /// + /// ``` + /// let mut p = lua_patterns::LuaPattern::new("(%d+)/(%d+)/(%d+)"); + /// let (y,m,d) = p.match_maybe_3("2017/11/10").unwrap(); + /// assert_eq!(y,"2017"); + /// assert_eq!(m,"11"); + /// assert_eq!(d,"10"); + /// ``` + pub fn match_maybe_3<'t>(&mut self, text: &'t str) -> Option<(&'t str,&'t str,&'t str)> { + if self.matches(text) { + let cc = self.match_captures(text); + if cc.num_matches() != 4 { return None; } + Some((cc.get(1),cc.get(2),cc.get(3))) + } else { + None + } + } /// Match and collect all captures as a vector of string slices /// @@ -706,6 +743,14 @@ mod tests { assert_eq!(cc[2], "bonzo dog"); } + + #[test] + fn multiple_captures() { + let mut p = LuaPattern::new("%s*(%d+)%s+(%S+)"); + let (int,rest) = p.match_maybe_2(" 233 hello dolly").unwrap(); + assert_eq!(int,"233"); + assert_eq!(rest,"hello"); + } #[test] fn gmatch() {