Struct LuaPatternBuilder

Source
pub struct LuaPatternBuilder { /* private fields */ }
Expand description

Build a byte Lua pattern, optionally escaping ‘magic’ characters

Implementations§

Source§

impl LuaPatternBuilder

Source

pub fn new() -> LuaPatternBuilder

Create a new Lua pattern builder

Examples found in repository?
examples/range.rs (line 20)
4fn main() {
5    let mut m = LuaPattern::new("(%a+) one");
6    let text = " hello one two";
7    assert!(m.matches(text));
8    assert_eq!(m.capture(1), 1..6);
9    assert_eq!(m.capture(0), 1..10);
10
11    let v = m.captures(text);
12    assert_eq!(v, &["hello one", "hello"]);
13
14    let mut v = Vec::new();
15    assert!(m.capture_into(text, &mut v));
16    assert_eq!(v, &["hello one", "hello"]);
17
18    let bytes = &[0xFF, 0xEE, 0x0, 0xDE, 0x24, 0x24, 0xBE, 0x0, 0x0];
19
20    let patt = LuaPatternBuilder::new()
21        .bytes_as_hex("DE24")
22        .text("+")
23        .bytes(&[0xBE])
24        .build();
25
26    let mut m = LuaPattern::from_bytes(&patt);
27    assert!(m.matches_bytes(bytes));
28    assert_eq!(&bytes[m.capture(0)], &[0xDE, 0x24, 0x24, 0xBE]);
29
30    let mut m = LuaPattern::new("(%S+)%s*=%s*(%S+);%s*");
31    let res = m.gsub("a=2; b=3; c = 4;", "'%2':%1 ");
32    println!("{}", res);
33
34    let mut m = LuaPattern::new("%s+");
35    let res = m.gsub("hello dolly you're so fine", "");
36    println!("{}", res);
37}
Source

pub fn text(&mut self, s: &str) -> &mut Self

Add unescaped characters from a string

let patt = lua_patterns::LuaPatternBuilder::new()
    .text("(boo)")
    .build();
assert_eq!(std::str::from_utf8(&patt).unwrap(), "(boo)");
Examples found in repository?
examples/range.rs (line 22)
4fn main() {
5    let mut m = LuaPattern::new("(%a+) one");
6    let text = " hello one two";
7    assert!(m.matches(text));
8    assert_eq!(m.capture(1), 1..6);
9    assert_eq!(m.capture(0), 1..10);
10
11    let v = m.captures(text);
12    assert_eq!(v, &["hello one", "hello"]);
13
14    let mut v = Vec::new();
15    assert!(m.capture_into(text, &mut v));
16    assert_eq!(v, &["hello one", "hello"]);
17
18    let bytes = &[0xFF, 0xEE, 0x0, 0xDE, 0x24, 0x24, 0xBE, 0x0, 0x0];
19
20    let patt = LuaPatternBuilder::new()
21        .bytes_as_hex("DE24")
22        .text("+")
23        .bytes(&[0xBE])
24        .build();
25
26    let mut m = LuaPattern::from_bytes(&patt);
27    assert!(m.matches_bytes(bytes));
28    assert_eq!(&bytes[m.capture(0)], &[0xDE, 0x24, 0x24, 0xBE]);
29
30    let mut m = LuaPattern::new("(%S+)%s*=%s*(%S+);%s*");
31    let res = m.gsub("a=2; b=3; c = 4;", "'%2':%1 ");
32    println!("{}", res);
33
34    let mut m = LuaPattern::new("%s+");
35    let res = m.gsub("hello dolly you're so fine", "");
36    println!("{}", res);
37}
Source

pub fn text_lines(&mut self, lines: &str) -> &mut Self

Add unescaped characters from lines

This looks for first non-whitespace run in each line, useful for spreading patterns out and commmenting them. Works with patterns that use ‘%s’ religiously!

let patt = lua_patterns::LuaPatternBuilder::new()
    .text_lines("
      hello-dolly
      you-are-fine  # comment
      cool
     ")
    .build();
assert_eq!(std::str::from_utf8(&patt).unwrap(),
  "hello-dollyyou-are-finecool");
Source

pub fn bytes(&mut self, b: &[u8]) -> &mut Self

Add escaped bytes from a slice

let patt = lua_patterns::LuaPatternBuilder::new()
    .text("^")
    .bytes(b"^") // magic character!
    .build();
assert_eq!(std::str::from_utf8(&patt).unwrap(), "^%^");
Examples found in repository?
examples/range.rs (line 23)
4fn main() {
5    let mut m = LuaPattern::new("(%a+) one");
6    let text = " hello one two";
7    assert!(m.matches(text));
8    assert_eq!(m.capture(1), 1..6);
9    assert_eq!(m.capture(0), 1..10);
10
11    let v = m.captures(text);
12    assert_eq!(v, &["hello one", "hello"]);
13
14    let mut v = Vec::new();
15    assert!(m.capture_into(text, &mut v));
16    assert_eq!(v, &["hello one", "hello"]);
17
18    let bytes = &[0xFF, 0xEE, 0x0, 0xDE, 0x24, 0x24, 0xBE, 0x0, 0x0];
19
20    let patt = LuaPatternBuilder::new()
21        .bytes_as_hex("DE24")
22        .text("+")
23        .bytes(&[0xBE])
24        .build();
25
26    let mut m = LuaPattern::from_bytes(&patt);
27    assert!(m.matches_bytes(bytes));
28    assert_eq!(&bytes[m.capture(0)], &[0xDE, 0x24, 0x24, 0xBE]);
29
30    let mut m = LuaPattern::new("(%S+)%s*=%s*(%S+);%s*");
31    let res = m.gsub("a=2; b=3; c = 4;", "'%2':%1 ");
32    println!("{}", res);
33
34    let mut m = LuaPattern::new("%s+");
35    let res = m.gsub("hello dolly you're so fine", "");
36    println!("{}", res);
37}
Source

pub fn bytes_as_hex(&mut self, bs: &str) -> &mut Self

Add escaped bytes from hex string

This consists of adjacent pairs of hex digits.

let patt = lua_patterns::LuaPatternBuilder::new()
    .text("^")
    .bytes_as_hex("5E") // which is ASCII '^'
    .build();
assert_eq!(std::str::from_utf8(&patt).unwrap(), "^%^");
Examples found in repository?
examples/range.rs (line 21)
4fn main() {
5    let mut m = LuaPattern::new("(%a+) one");
6    let text = " hello one two";
7    assert!(m.matches(text));
8    assert_eq!(m.capture(1), 1..6);
9    assert_eq!(m.capture(0), 1..10);
10
11    let v = m.captures(text);
12    assert_eq!(v, &["hello one", "hello"]);
13
14    let mut v = Vec::new();
15    assert!(m.capture_into(text, &mut v));
16    assert_eq!(v, &["hello one", "hello"]);
17
18    let bytes = &[0xFF, 0xEE, 0x0, 0xDE, 0x24, 0x24, 0xBE, 0x0, 0x0];
19
20    let patt = LuaPatternBuilder::new()
21        .bytes_as_hex("DE24")
22        .text("+")
23        .bytes(&[0xBE])
24        .build();
25
26    let mut m = LuaPattern::from_bytes(&patt);
27    assert!(m.matches_bytes(bytes));
28    assert_eq!(&bytes[m.capture(0)], &[0xDE, 0x24, 0x24, 0xBE]);
29
30    let mut m = LuaPattern::new("(%S+)%s*=%s*(%S+);%s*");
31    let res = m.gsub("a=2; b=3; c = 4;", "'%2':%1 ");
32    println!("{}", res);
33
34    let mut m = LuaPattern::new("%s+");
35    let res = m.gsub("hello dolly you're so fine", "");
36    println!("{}", res);
37}
Source

pub fn build(&mut self) -> Vec<u8>

Create the pattern

Examples found in repository?
examples/range.rs (line 24)
4fn main() {
5    let mut m = LuaPattern::new("(%a+) one");
6    let text = " hello one two";
7    assert!(m.matches(text));
8    assert_eq!(m.capture(1), 1..6);
9    assert_eq!(m.capture(0), 1..10);
10
11    let v = m.captures(text);
12    assert_eq!(v, &["hello one", "hello"]);
13
14    let mut v = Vec::new();
15    assert!(m.capture_into(text, &mut v));
16    assert_eq!(v, &["hello one", "hello"]);
17
18    let bytes = &[0xFF, 0xEE, 0x0, 0xDE, 0x24, 0x24, 0xBE, 0x0, 0x0];
19
20    let patt = LuaPatternBuilder::new()
21        .bytes_as_hex("DE24")
22        .text("+")
23        .bytes(&[0xBE])
24        .build();
25
26    let mut m = LuaPattern::from_bytes(&patt);
27    assert!(m.matches_bytes(bytes));
28    assert_eq!(&bytes[m.capture(0)], &[0xDE, 0x24, 0x24, 0xBE]);
29
30    let mut m = LuaPattern::new("(%S+)%s*=%s*(%S+);%s*");
31    let res = m.gsub("a=2; b=3; c = 4;", "'%2':%1 ");
32    println!("{}", res);
33
34    let mut m = LuaPattern::new("%s+");
35    let res = m.gsub("hello dolly you're so fine", "");
36    println!("{}", res);
37}
Source

pub fn hex_to_bytes(s: &str) -> Vec<u8>

Utility to create a vector of bytes from a hex string

let bb = lua_patterns::LuaPatternBuilder::hex_to_bytes("AEFE00FE");
assert_eq!(bb, &[0xAE,0xFE,0x00,0xFE]);
Source

pub fn bytes_to_hex(s: &[u8]) -> String

Utility to create a hex string from a slice of bytes

let hex = lua_patterns::LuaPatternBuilder::bytes_to_hex(&[0xAE,0xFE,0x00,0xFE]);
assert_eq!(hex,"AEFE00FE");

Auto Trait Implementations§

Blanket Implementations§

Source§

impl<T> Any for T
where T: 'static + ?Sized,

Source§

fn type_id(&self) -> TypeId

Gets the TypeId of self. Read more
Source§

impl<T> Borrow<T> for T
where T: ?Sized,

Source§

fn borrow(&self) -> &T

Immutably borrows from an owned value. Read more
Source§

impl<T> BorrowMut<T> for T
where T: ?Sized,

Source§

fn borrow_mut(&mut self) -> &mut T

Mutably borrows from an owned value. Read more
Source§

impl<T> From<T> for T

Source§

fn from(t: T) -> T

Returns the argument unchanged.

Source§

impl<T, U> Into<U> for T
where U: From<T>,

Source§

fn into(self) -> U

Calls U::from(self).

That is, this conversion is whatever the implementation of From<T> for U chooses to do.

Source§

impl<T, U> TryFrom<U> for T
where U: Into<T>,

Source§

type Error = Infallible

The type returned in the event of a conversion error.
Source§

fn try_from(value: U) -> Result<T, <T as TryFrom<U>>::Error>

Performs the conversion.
Source§

impl<T, U> TryInto<U> for T
where U: TryFrom<T>,

Source§

type Error = <U as TryFrom<T>>::Error

The type returned in the event of a conversion error.
Source§

fn try_into(self) -> Result<U, <U as TryFrom<T>>::Error>

Performs the conversion.