157 lines
4.1 KiB
Rust
157 lines
4.1 KiB
Rust
use rand::RngExt;
|
|
|
|
pub const TOKEN_HELP: &str = "\
|
|
Cased Tokens:
|
|
l[^~] letter (a-z / A-Z / both)
|
|
a[^~] alphanumeric (0-9; a-z / A-Z / both)
|
|
h[^~] hex digit (0-9; a-f / A-F / both)
|
|
r[^~] radix digit (0 to base-1, depends on --base)
|
|
|
|
Fixed Tokens:
|
|
n / d digit (0-9)
|
|
b binary (0-1)
|
|
o octal (0-7)
|
|
|
|
Repition:
|
|
t{N} repeat token t exactly N times (e.g. a{4}, h^{8})
|
|
|
|
* any other character is taken literally";
|
|
|
|
const DIGITS: &[u8] = b"0123456789";
|
|
const LETTERS: &[u8] = b"abcdefghijklmnopqrstuvwxyz";
|
|
const ALNUM: &[u8] = b"0123456789abcdefghijklmnopqrstuvwxyz";
|
|
const BINARY: &[u8] = b"01";
|
|
const OCTAL: &[u8] = b"01234567";
|
|
const HEX: &[u8] = b"0123456789abcdef";
|
|
|
|
#[derive(Debug, Clone, Copy, PartialEq)]
|
|
enum Case {
|
|
Lower,
|
|
Upper,
|
|
Mixed,
|
|
}
|
|
|
|
#[derive(Debug, Clone, Copy)]
|
|
enum Token {
|
|
Digit,
|
|
Letter(Case),
|
|
Alnum(Case),
|
|
Binary,
|
|
Octal,
|
|
Hex(Case),
|
|
Radix(Case),
|
|
Literal(char),
|
|
}
|
|
|
|
#[derive(Debug, Clone)]
|
|
struct PatternToken {
|
|
token: Token,
|
|
count: usize,
|
|
}
|
|
|
|
pub fn generate_from_pattern(pattern: &str, base: u32) -> String {
|
|
let mut rng = rand::rng();
|
|
let tokens = parse_pattern(pattern);
|
|
let mut output = String::with_capacity(tokens.iter().map(|t| t.count).sum());
|
|
let radix_pool = &ALNUM[..base as usize];
|
|
|
|
for pt in tokens {
|
|
for _ in 0..pt.count {
|
|
let ch = match pt.token {
|
|
Token::Digit => pick(&mut rng, DIGITS),
|
|
Token::Binary => pick(&mut rng, BINARY),
|
|
Token::Octal => pick(&mut rng, OCTAL),
|
|
Token::Letter(case) => pick_cased(&mut rng, LETTERS, case),
|
|
Token::Alnum(case) => pick_cased(&mut rng, ALNUM, case),
|
|
Token::Hex(case) => pick_cased(&mut rng, HEX, case),
|
|
Token::Radix(case) => pick_cased(&mut rng, radix_pool, case),
|
|
Token::Literal(c) => c,
|
|
};
|
|
output.push(ch);
|
|
}
|
|
}
|
|
output
|
|
}
|
|
|
|
fn pick(rng: &mut impl rand::Rng, pool: &[u8]) -> char {
|
|
pool[rng.random_range(0..pool.len())] as char
|
|
}
|
|
|
|
fn pick_cased(rng: &mut impl rand::Rng, pool: &[u8], case: Case) -> char {
|
|
let ch = pick(rng, pool);
|
|
|
|
match case {
|
|
Case::Lower => ch,
|
|
Case::Upper => ch.to_ascii_uppercase(),
|
|
Case::Mixed => {
|
|
if rng.random::<bool>() {
|
|
ch.to_ascii_uppercase()
|
|
} else {
|
|
ch
|
|
}
|
|
}
|
|
}
|
|
}
|
|
|
|
fn parse_case(chars: &mut std::iter::Peekable<impl Iterator<Item = char>>) -> Case {
|
|
match chars.peek() {
|
|
Some(&'^') => {
|
|
chars.next();
|
|
Case::Upper
|
|
}
|
|
Some(&'~') => {
|
|
chars.next();
|
|
Case::Mixed
|
|
}
|
|
_ => Case::Lower,
|
|
}
|
|
}
|
|
|
|
fn parse_pattern(pattern: &str) -> Vec<PatternToken> {
|
|
let mut tokens = Vec::new();
|
|
let mut chars = pattern.chars().peekable();
|
|
|
|
while let Some(c) = chars.next() {
|
|
let case = if matches!(c, 'l' | 'a' | 'h' | 'r') {
|
|
parse_case(&mut chars)
|
|
} else {
|
|
Case::Lower
|
|
};
|
|
|
|
let (count, spill) = if chars.peek() == Some(&'{') {
|
|
chars.next();
|
|
let inner: String = chars.by_ref().take_while(|&x| x != '}').collect();
|
|
|
|
match inner.parse::<usize>() {
|
|
Ok(n) if n > 0 => (n, None),
|
|
_ => (1, Some(inner)),
|
|
}
|
|
} else {
|
|
(1, None)
|
|
};
|
|
|
|
let token = match c {
|
|
'n' | 'd' => Token::Digit,
|
|
'b' => Token::Binary,
|
|
'o' => Token::Octal,
|
|
'l' => Token::Letter(case),
|
|
'a' => Token::Alnum(case),
|
|
'h' => Token::Hex(case),
|
|
'r' => Token::Radix(case),
|
|
_ => Token::Literal(c),
|
|
};
|
|
|
|
tokens.push(PatternToken { token, count });
|
|
|
|
if let Some(spill) = spill {
|
|
for sc in spill.chars() {
|
|
tokens.push(PatternToken {
|
|
token: Token::Literal(sc),
|
|
count: 1,
|
|
});
|
|
}
|
|
}
|
|
}
|
|
|
|
tokens
|
|
}
|