Merge branch 'master' of github.com:stevedonovan/lua-patterns
This commit is contained in:
commit
027e2166d0
26
examples/errors.rs
Normal file
26
examples/errors.rs
Normal file
|
@ -0,0 +1,26 @@
|
||||||
|
extern crate lua_patterns;
|
||||||
|
|
||||||
|
fn main() {
|
||||||
|
let bad = [
|
||||||
|
("bonzo %","malformed pattern (ends with '%')"),
|
||||||
|
("bonzo (dog%(","unfinished capture"),
|
||||||
|
("alles [%a%[","malformed pattern (missing ']')"),
|
||||||
|
("bonzo (dog (cat)","unfinished capture"),
|
||||||
|
("frodo %f[%A","malformed pattern (missing ']')"),
|
||||||
|
("frodo (1) (2(3)%2)%1","invalid capture index %2"),
|
||||||
|
];
|
||||||
|
|
||||||
|
fn error(s: &str) -> lua_patterns::PatternError {
|
||||||
|
lua_patterns::PatternError(s.into())
|
||||||
|
}
|
||||||
|
|
||||||
|
for p in bad.iter() {
|
||||||
|
let res = lua_patterns::LuaPattern::new_try(p.0);
|
||||||
|
if let Err(e) = res {
|
||||||
|
assert_eq!(e, error(p.1));
|
||||||
|
} else {
|
||||||
|
println!("'{}' was fine",p.0);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
}
|
9
examples/multiple_captures.rs
Normal file
9
examples/multiple_captures.rs
Normal file
|
@ -0,0 +1,9 @@
|
||||||
|
extern crate lua_patterns as lp;
|
||||||
|
|
||||||
|
fn main() {
|
||||||
|
let mut p = lp::LuaPattern::new("%s*(%d+)%s+(%S+)");
|
||||||
|
if let Some((int,rest)) = p.match_maybe_2(" 233 hello dolly") {
|
||||||
|
assert_eq!(int,"233");
|
||||||
|
assert_eq!(rest,"hello");
|
||||||
|
}
|
||||||
|
}
|
45
src/lib.rs
45
src/lib.rs
|
@ -175,6 +175,43 @@ impl <'a> LuaPattern<'a> {
|
||||||
None
|
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
|
/// Match and collect all captures as a vector of string slices
|
||||||
///
|
///
|
||||||
|
@ -707,6 +744,14 @@ mod tests {
|
||||||
assert_eq!(cc[2], "bonzo dog");
|
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]
|
#[test]
|
||||||
fn gmatch() {
|
fn gmatch() {
|
||||||
|
|
Loading…
Reference in a new issue