reworked pattern generation again to remove individual tokens for upper and mixed capitalization
This commit is contained in:
parent
f428ac59e0
commit
29a68bc047
1 changed files with 127 additions and 68 deletions
|
|
@ -1,65 +1,73 @@
|
|||
use rand::RngExt;
|
||||
|
||||
const DIGITS: &[u8] = b"0123456789";
|
||||
const LETTERS_LOWER: &[u8] = b"abcdefghijklmnopqrstuvwxyz";
|
||||
const LETTERS_UPPER: &[u8] = b"ABCDEFGHIJKLMNOPQRSTUVWXYZ";
|
||||
const LETTERS_MIXED: &[u8] = b"abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ";
|
||||
const ALNUM_LOWER: &[u8] = b"0123456789abcdefghijklmnopqrstuvwxyz";
|
||||
const ALNUM_UPPER: &[u8] = b"0123456789ABCDEFGHIJKLMNOPQRSTUVWXYZ";
|
||||
const ALNUM_MIXED: &[u8] = b"0123456789abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ";
|
||||
const DIGITS_BINARY: &[u8] = b"01";
|
||||
const DIGITS_OCTAL: &[u8] = b"01234567";
|
||||
const DIGITS_HEX_LOWER: &[u8] = b"0123456789abcdef";
|
||||
const DIGITS_HEX_UPPER: &[u8] = b"0123456789ABCDEF";
|
||||
const DIGITS_HEX_MIXED: &[u8] = b"0123456789abcdefABCDEF";
|
||||
|
||||
pub const TOKEN_HELP: &str = "\
|
||||
Pattern Tokens:
|
||||
n digit (0-9)
|
||||
l letter lower (a-z)
|
||||
L letter upper (A-Z)
|
||||
m letter mixed (a-z, A-Z)
|
||||
a alnum lower (0-9, a-z)
|
||||
A alnum upper (0-9, A-Z)
|
||||
x alnum mixed (0-9, a-z, A-Z)
|
||||
b binary digit (0-1)
|
||||
o octal digit (0-7)
|
||||
h hex lower (0-f)
|
||||
H hex upper (0-F)
|
||||
X hex mixed (0-9, a-f, A-F)
|
||||
r radix digit (0 to base-1, depends on --base)
|
||||
* any other character is used literally";
|
||||
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(|(_, n)| n).sum());
|
||||
let mut output = String::with_capacity(tokens.iter().map(|t| t.count).sum());
|
||||
let radix_pool = &ALNUM[..base as usize];
|
||||
|
||||
let base_digits = &ALNUM_LOWER[..base as usize];
|
||||
|
||||
for (c, count) in tokens {
|
||||
let pool = match c {
|
||||
'n' => Some(DIGITS),
|
||||
'l' => Some(LETTERS_LOWER),
|
||||
'L' => Some(LETTERS_UPPER),
|
||||
'm' => Some(LETTERS_MIXED),
|
||||
'a' => Some(ALNUM_LOWER),
|
||||
'A' => Some(ALNUM_UPPER),
|
||||
'x' => Some(ALNUM_MIXED),
|
||||
'b' => Some(DIGITS_BINARY),
|
||||
'o' => Some(DIGITS_OCTAL),
|
||||
'h' => Some(DIGITS_HEX_LOWER),
|
||||
'H' => Some(DIGITS_HEX_UPPER),
|
||||
'X' => Some(DIGITS_HEX_MIXED),
|
||||
'r' => Some(base_digits),
|
||||
_ => None,
|
||||
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,
|
||||
};
|
||||
|
||||
for _ in 0..count {
|
||||
output.push(match pool {
|
||||
Some(p) => pick(&mut rng, p),
|
||||
None => c,
|
||||
});
|
||||
output.push(ch);
|
||||
}
|
||||
}
|
||||
output
|
||||
|
|
@ -69,28 +77,79 @@ fn pick(rng: &mut impl rand::Rng, pool: &[u8]) -> char {
|
|||
pool[rng.random_range(0..pool.len())] as char
|
||||
}
|
||||
|
||||
fn parse_pattern(pattern: &str) -> Vec<(char, usize)> {
|
||||
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() {
|
||||
if chars.peek() == Some(&'{') {
|
||||
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();
|
||||
|
||||
let digits: String = chars.by_ref().take_while(|&x| x != '}').collect();
|
||||
|
||||
match digits.parse::<usize>() {
|
||||
Ok(n) if n > 0 => tokens.push((c, n)),
|
||||
_ => {
|
||||
tokens.push((c, 1));
|
||||
|
||||
for d in digits.chars() {
|
||||
tokens.push((d, 1));
|
||||
}
|
||||
}
|
||||
match inner.parse::<usize>() {
|
||||
Ok(n) if n > 0 => (n, None),
|
||||
_ => (1, Some(inner)),
|
||||
}
|
||||
} else {
|
||||
tokens.push((c, 1));
|
||||
(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,
|
||||
});
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue