reworked pattern generation again to remove individual tokens for upper and mixed capitalization

This commit is contained in:
ika4422 2026-04-13 18:15:29 +01:00
parent f428ac59e0
commit 29a68bc047
No known key found for this signature in database
GPG key ID: F7FBF981D2A33785

View file

@ -1,65 +1,73 @@
use rand::RngExt; 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 = "\ pub const TOKEN_HELP: &str = "\
Pattern Tokens: Cased Tokens:
n digit (0-9) l[^~] letter (a-z / A-Z / both)
l letter lower (a-z) a[^~] alphanumeric (0-9; a-z / A-Z / both)
L letter upper (A-Z) h[^~] hex digit (0-9; a-f / A-F / both)
m letter mixed (a-z, A-Z) r[^~] radix digit (0 to base-1, depends on --base)
a alnum lower (0-9, a-z)
A alnum upper (0-9, A-Z) Fixed Tokens:
x alnum mixed (0-9, a-z, A-Z) n / d digit (0-9)
b binary digit (0-1) b binary (0-1)
o octal digit (0-7) o octal (0-7)
h hex lower (0-f)
H hex upper (0-F) Repition:
X hex mixed (0-9, a-f, A-F) t{N} repeat token t exactly N times (e.g. a{4}, h^{8})
r radix digit (0 to base-1, depends on --base)
* any other character is used literally"; * 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 { pub fn generate_from_pattern(pattern: &str, base: u32) -> String {
let mut rng = rand::rng(); let mut rng = rand::rng();
let tokens = parse_pattern(pattern); 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 pt in tokens {
for _ in 0..pt.count {
for (c, count) in tokens { let ch = match pt.token {
let pool = match c { Token::Digit => pick(&mut rng, DIGITS),
'n' => Some(DIGITS), Token::Binary => pick(&mut rng, BINARY),
'l' => Some(LETTERS_LOWER), Token::Octal => pick(&mut rng, OCTAL),
'L' => Some(LETTERS_UPPER), Token::Letter(case) => pick_cased(&mut rng, LETTERS, case),
'm' => Some(LETTERS_MIXED), Token::Alnum(case) => pick_cased(&mut rng, ALNUM, case),
'a' => Some(ALNUM_LOWER), Token::Hex(case) => pick_cased(&mut rng, HEX, case),
'A' => Some(ALNUM_UPPER), Token::Radix(case) => pick_cased(&mut rng, radix_pool, case),
'x' => Some(ALNUM_MIXED), Token::Literal(c) => c,
'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,
}; };
output.push(ch);
for _ in 0..count {
output.push(match pool {
Some(p) => pick(&mut rng, p),
None => c,
});
} }
} }
output output
@ -69,28 +77,79 @@ fn pick(rng: &mut impl rand::Rng, pool: &[u8]) -> char {
pool[rng.random_range(0..pool.len())] as 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 tokens = Vec::new();
let mut chars = pattern.chars().peekable(); let mut chars = pattern.chars().peekable();
while let Some(c) = chars.next() { 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(); 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 inner.parse::<usize>() {
Ok(n) if n > 0 => (n, None),
match digits.parse::<usize>() { _ => (1, Some(inner)),
Ok(n) if n > 0 => tokens.push((c, n)),
_ => {
tokens.push((c, 1));
for d in digits.chars() {
tokens.push((d, 1));
}
}
} }
} else { } 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,
});
}
} }
} }