updated examples
This commit is contained in:
parent
9ba61944b0
commit
edb376c69e
|
@ -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,7 +16,7 @@ fn main() {
|
|||
assert_eq!(iter.next(), Some("three"));
|
||||
assert_eq!(iter.next(), None);
|
||||
|
||||
let mut m = lp::LuaPattern::new("(%a+)");
|
||||
let mut m = lp::LuaPattern::new("%S+");
|
||||
let split: Vec<_> = m.gmatch("dog cat leopard wolf").collect();
|
||||
assert_eq!(split,&["dog","cat","leopard","wolf"]);
|
||||
|
||||
|
@ -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',");
|
||||
|
||||
|
||||
|
||||
|
|
|
@ -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]);
|
||||
}
|
||||
|
|
19
examples/strings.rs
Normal file
19
examples/strings.rs
Normal file
|
@ -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());
|
||||
}
|
||||
|
||||
}
|
Loading…
Reference in a new issue