diff --git a/examples/iter.rs b/examples/iter.rs index ac66220..74072aa 100644 --- a/examples/iter.rs +++ b/examples/iter.rs @@ -4,9 +4,9 @@ extern crate lua_patterns as lp; fn main() { - let mut m = lp::LuaPattern::new("hello%"); - m.matches("hello"); - println!("ok"); + //~ let mut m = lp::LuaPattern::new("hello%"); + //~ m.matches("hello"); + //~ println!("ok"); ///* let mut m = lp::LuaPattern::new("(%a+)"); @@ -16,8 +16,8 @@ fn main() { assert_eq!(iter.next(), Some("three")); assert_eq!(iter.next(), None); - let mut m = lp::LuaPattern::new("(%a+)"); - let split: Vec<_> = m.gmatch("dog cat leopard wolf").collect(); + let mut m = lp::LuaPattern::new("%S+"); + let split: Vec<_> = m.gmatch("dog cat leopard wolf").collect(); assert_eq!(split,&["dog","cat","leopard","wolf"]); let mut m = lp::LuaPattern::new("(%S+)%s*=%s*(.+)"); @@ -37,8 +37,12 @@ fn main() { |cc| cc.get(1).to_uppercase() ); assert_eq!(res,"hello DOLLY you're so FINE"); - //*/ + let mut m = lp::LuaPattern::new("(%S+)%s*=%s*([^;]+);"); + let res = m.gsub("alpha=bonzo; beta=felix;", + |cc| format!("{}:'{}',", cc.get(1), cc.get(2)) + ); + assert_eq!(res, "alpha:'bonzo', beta:'felix',"); diff --git a/examples/range.rs b/examples/range.rs index f1a4d35..6134d42 100644 --- a/examples/range.rs +++ b/examples/range.rs @@ -7,4 +7,18 @@ fn main() { assert!(m.matches(text)); assert_eq!(m.capture(1),1..6); assert_eq!(m.capture(0),1..10); + + let v = m.captures(text); + assert_eq!(v, &["hello one","hello"]); + + let mut v = Vec::new(); + assert!(m.capture_into(text,&mut v)); + assert_eq!(v, &["hello one","hello"]); + + let patt = &[0xDE,0x00,b'+',0xBE]; + let bytes = &[0xFF,0xEE,0x0,0xDE,0x0,0x0,0xBE,0x0,0x0]; + + let mut m = LuaPattern::from_bytes(patt); + assert!(m.matches_bytes(bytes)); + assert_eq!(&bytes[m.capture(0)], &[0xDE,0x00,0x00,0xBE]); } diff --git a/examples/strings.rs b/examples/strings.rs new file mode 100644 index 0000000..5de9f9f --- /dev/null +++ b/examples/strings.rs @@ -0,0 +1,19 @@ +extern crate lua_patterns; +use lua_patterns::LuaPattern; + +use std::env; +use std::fs::File; +use std::io::prelude::*; + +fn main() { + let file = env::args().skip(1).next().expect("provide a binary file"); + let mut f = File::open(&file).expect("can't open file"); + let mut buf = Vec::new(); + f.read_to_end(&mut buf).expect("can't read file"); + + let mut words = LuaPattern::new("%a%a+"); + for w in words.gmatch_bytes(&buf) { + println!("{}",std::str::from_utf8(w).unwrap()); + } + +}