more examples - particularly multiple captures

This commit is contained in:
Steve Donovan 2017-11-30 15:39:08 +02:00
parent 7697b9ffb3
commit bd92afd0df
2 changed files with 35 additions and 0 deletions

26
examples/errors.rs Normal file
View 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);
}
}
}

View 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");
}
}