add client module
This commit is contained in:
parent
7447d2394b
commit
b85b9893a8
7 changed files with 550 additions and 29 deletions
28
src/util.rs
Normal file
28
src/util.rs
Normal file
|
|
@ -0,0 +1,28 @@
|
|||
use fancy_regex::Regex;
|
||||
use rand::Rng;
|
||||
|
||||
/// Return the given capture group that matches first in a list of regexes
|
||||
pub fn get_cg_from_regexes<'a, I>(mut regexes: I, text: &str, cg: usize) -> Option<String>
|
||||
where
|
||||
I: Iterator<Item = &'a Regex>,
|
||||
{
|
||||
regexes
|
||||
.find_map(|pattern| pattern.captures(text).ok().flatten())
|
||||
.map(|c| c.get(cg).unwrap().as_str().to_owned())
|
||||
}
|
||||
|
||||
/// Generates a random string with given length and byte charset.
|
||||
pub fn random_string(charset: &[u8], length: usize) -> String {
|
||||
let mut result = String::with_capacity(length);
|
||||
let mut rng = rand::thread_rng();
|
||||
|
||||
unsafe {
|
||||
for _ in 0..length {
|
||||
result.push(
|
||||
char::from(*charset.get_unchecked(rng.gen_range(0..charset.len())))
|
||||
);
|
||||
}
|
||||
}
|
||||
|
||||
result
|
||||
}
|
||||
Reference in a new issue