Struct lua_patterns::LuaPattern
source · pub struct LuaPattern<'a> { /* private fields */ }
Expand description
Represents a Lua string pattern and the results of a match
Implementations§
source§impl<'a> LuaPattern<'a>
impl<'a> LuaPattern<'a>
sourcepub fn from_bytes_try(bytes: &'a [u8]) -> Result<LuaPattern<'a>, PatternError>
pub fn from_bytes_try(bytes: &'a [u8]) -> Result<LuaPattern<'a>, PatternError>
Maybe create a new Lua pattern from a slice of bytes
sourcepub fn new_try(patt: &'a str) -> Result<LuaPattern<'a>, PatternError>
pub fn new_try(patt: &'a str) -> Result<LuaPattern<'a>, PatternError>
Maybe create a new Lua pattern from a string
sourcepub fn new(patt: &'a str) -> LuaPattern<'a>
pub fn new(patt: &'a str) -> LuaPattern<'a>
Create a new Lua pattern from a string, panicking if bad
sourcepub fn from_bytes(bytes: &'a [u8]) -> LuaPattern<'a>
pub fn from_bytes(bytes: &'a [u8]) -> LuaPattern<'a>
Create a new Lua pattern from a slice of bytes, panicking if bad
sourcepub fn matches_bytes(&mut self, s: &[u8]) -> bool
pub fn matches_bytes(&mut self, s: &[u8]) -> bool
Match a slice of bytes with a pattern
let patt = &[0xFE,0xEE,b'+',0xED];
let mut m = lua_patterns::LuaPattern::from_bytes(patt);
let bytes = &[0x00,0x01,0xFE,0xEE,0xEE,0xED,0xEF];
assert!(m.matches_bytes(bytes));
assert_eq!(&bytes[m.range()], &[0xFE,0xEE,0xEE,0xED]);
sourcepub fn matches(&mut self, text: &str) -> bool
pub fn matches(&mut self, text: &str) -> bool
Match a string with a pattern
let mut m = lua_patterns::LuaPattern::new("(%a+) one");
let text = " hello one two";
assert!(m.matches(text));
sourcepub fn match_maybe<'t>(&mut self, text: &'t str) -> Option<&'t str>
pub fn match_maybe<'t>(&mut self, text: &'t str) -> Option<&'t str>
Match a string, returning first capture if successful
let mut m = lua_patterns::LuaPattern::new("OK%s+(%d+)");
let res = m.match_maybe("and that's OK 400 to you");
assert_eq!(res, Some("400"));
sourcepub fn match_maybe_2<'t>(&mut self, text: &'t str) -> Option<(&'t str, &'t str)>
pub fn match_maybe_2<'t>(&mut self, text: &'t str) -> Option<(&'t str, &'t str)>
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");
sourcepub fn match_maybe_3<'t>(
&mut self,
text: &'t str
) -> Option<(&'t str, &'t str, &'t str)>
pub fn match_maybe_3<'t>(
&mut self,
text: &'t str
) -> Option<(&'t str, &'t str, &'t str)>
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");
sourcepub fn captures<'b>(&mut self, text: &'b str) -> Vec<&'b str>
pub fn captures<'b>(&mut self, text: &'b str) -> Vec<&'b str>
Match and collect all captures as a vector of string slices
let mut m = lua_patterns::LuaPattern::new("(one).+");
assert_eq!(m.captures(" one two"), &["one two","one"]);
sourcepub fn match_captures<'b, 'c>(&'c self, text: &'b str) -> Captures<'a, 'b, 'c>
pub fn match_captures<'b, 'c>(&'c self, text: &'b str) -> Captures<'a, 'b, 'c>
A convenient way to access the captures with no allocation
let text = " hello one";
let mut m = lua_patterns::LuaPattern::new("(%S+) one");
if m.matches(text) {
let cc = m.match_captures(text);
assert_eq!(cc.get(0), "hello one");
assert_eq!(cc.get(1), "hello");
}
sourcepub fn capture_into<'b>(&mut self, text: &'b str, vec: &mut Vec<&'b str>) -> bool
pub fn capture_into<'b>(&mut self, text: &'b str, vec: &mut Vec<&'b str>) -> bool
Match and collect all captures into the provided vector.
let text = " hello one";
let mut m = lua_patterns::LuaPattern::new("(%S+) one");
let mut v = Vec::new();
if m.capture_into(text,&mut v) {
assert_eq!(v, &["hello one","hello"]);
}
sourcepub fn capture(&self, i: usize) -> Range<usize>
pub fn capture(&self, i: usize) -> Range<usize>
Get the nth capture of the match.
let mut m = lua_patterns::LuaPattern::new("(%a+) one");
let text = " hello one two";
assert!(m.matches(text));
assert_eq!(m.capture(0),1..10);
assert_eq!(m.capture(1),1..6);
sourcepub fn first_capture(&self) -> Range<usize>
pub fn first_capture(&self) -> Range<usize>
Get the ‘first’ capture of the match
If there are no matches, this is the same as range
,
otherwise it’s capture(1)
sourcepub fn gmatch<'b, 'c>(&'c mut self, text: &'b str) -> GMatch<'a, 'b, 'c> ⓘ
pub fn gmatch<'b, 'c>(&'c mut self, text: &'b str) -> GMatch<'a, 'b, 'c> ⓘ
An iterator over all matches in a string.
The matches are returned as string slices; if there are no captures the full match is used, otherwise the first capture. That is, this example will also work with the pattern “(%S+)”.
let mut m = lua_patterns::LuaPattern::new("%S+");
let split: Vec<_> = m.gmatch("dog cat leopard wolf").collect();
assert_eq!(split,&["dog","cat","leopard","wolf"]);
sourcepub fn gmatch_captures<'b, 'c>(
&'c mut self,
text: &'b str
) -> GMatchCaptures<'a, 'b, 'c> ⓘ
pub fn gmatch_captures<'b, 'c>(
&'c mut self,
text: &'b str
) -> GMatchCaptures<'a, 'b, 'c> ⓘ
An iterator over all captures in a string.
The matches are returned as captures; this is a streaming
iterator, so don’t try to collect the captures directly; extract
the string slices using get
.
let mut m = lua_patterns::LuaPattern::new("(%S)%S+");
let split: Vec<_> = m.gmatch_captures("dog cat leopard wolf")
.map(|cc| cc.get(1)).collect();
assert_eq!(split,&["d","c","l","w"]);
sourcepub fn gmatch_bytes<'b>(&'a mut self, bytes: &'b [u8]) -> GMatchBytes<'a, 'b> ⓘ
pub fn gmatch_bytes<'b>(&'a mut self, bytes: &'b [u8]) -> GMatchBytes<'a, 'b> ⓘ
An iterator over all matches in a slice of bytes.
let bytes = &[0xAA,0x01,0x01,0x03,0xBB,0x01,0x01,0x01];
let patt = &[0x01,b'+'];
let mut m = lua_patterns::LuaPattern::from_bytes(patt);
let mut iter = m.gmatch_bytes(bytes);
assert_eq!(iter.next().unwrap(), &[0x01,0x01]);
assert_eq!(iter.next().unwrap(), &[0x01,0x01,0x01]);
assert_eq!(iter.next(), None);
sourcepub fn gsub_with<F>(&mut self, text: &str, lookup: F) -> Stringwhere
F: Fn(Captures<'_, '_, '_>) -> String,
pub fn gsub_with<F>(&mut self, text: &str, lookup: F) -> Stringwhere
F: Fn(Captures<'_, '_, '_>) -> String,
Globally substitute all matches with a replacement provided by a function of the captures.
let mut m = lua_patterns::LuaPattern::new("%$(%S+)");
let res = m.gsub_with("hello $dolly you're so $fine!",
|cc| cc.get(1).to_uppercase()
);
assert_eq!(res, "hello DOLLY you're so FINE!");
sourcepub fn gsub(&mut self, text: &str, repl: &str) -> String
pub fn gsub(&mut self, text: &str, repl: &str) -> String
Globally substitute all matches with a replacement string
This string may have capture references (“%0”,..). Use “%%” to represent “%”. Plain strings like “” work just fine ;)
let mut m = lua_patterns::LuaPattern::new("(%S+)%s*=%s*(%S+);%s*");
let res = m.gsub("a=2; b=3; c = 4;", "'%2':%1 ");
assert_eq!(res,"'2':a '3':b '4':c ");
sourcepub fn gsub_bytes_with<F>(&mut self, bytes: &[u8], lookup: F) -> Vec<u8>where
F: Fn(ByteCaptures<'_, '_>) -> Vec<u8>,
pub fn gsub_bytes_with<F>(&mut self, bytes: &[u8], lookup: F) -> Vec<u8>where
F: Fn(ByteCaptures<'_, '_>) -> Vec<u8>,
Globally substitute all byte matches with a replacement provided by a function of the captures.
let bytes = &[0xAA,0x01,0x02,0x03,0xBB];
let patt = &[0x01,0x02];
let mut m = lua_patterns::LuaPattern::from_bytes(patt);
let res = m.gsub_bytes_with(bytes,|cc| vec![0xFF]);
assert_eq!(res, &[0xAA,0xFF,0x03,0xBB]);