added argument to change integer base

This commit is contained in:
ika4422 2026-04-08 21:41:26 +01:00
parent f9a479f4b5
commit 96bacd47bf
No known key found for this signature in database
GPG key ID: F7FBF981D2A33785
2 changed files with 24 additions and 4 deletions

View file

@ -48,6 +48,10 @@ struct Args {
/// Specifies how many codes to generate
count: usize,
#[arg(short, long, default_value_t = 10)]
/// Input/output base for integers (2, 8, 10, 16)
base: u32,
#[arg(long, default_value_t = false)]
/// Will generate codes without encoding it into dollcode
no_doll: bool,
@ -88,7 +92,7 @@ fn main() {
let as_num = if args.text {
None
} else {
BigUint::parse_bytes(generated.as_bytes(), 10)
BigUint::parse_bytes(generated.as_bytes(), args.base)
};
match as_num {
@ -106,7 +110,8 @@ fn main() {
}
}
} else if let Some(input) = args.encode {
let encode_as_text = args.text || input.parse::<BigUint>().is_err();
let encode_as_text =
args.text || BigUint::parse_bytes(input.as_bytes(), args.base).is_none();
if encode_as_text {
let result: Vec<String> = input
@ -115,7 +120,7 @@ fn main() {
.collect();
println!("{}", result.join(" "));
} else {
let num = input.parse::<BigUint>().unwrap();
let num = BigUint::parse_bytes(input.as_bytes(), args.base).unwrap();
match to_dollcode(num) {
Ok(code) => println!("{}", code),
@ -149,7 +154,7 @@ fn main() {
println!("{}", decoded_chars);
} else {
match from_dollcode(&input) {
Ok(val) => println!("{}", val),
Ok(val) => println!("{}", val.to_str_radix(args.base)),
Err(e) => {
eprintln!("Error: {}", e);
process::exit(1);

View file

@ -7,6 +7,11 @@ const LETTERS_MIXED: &[u8] = b"abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVW
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:
@ -17,6 +22,11 @@ Pattern Tokens:
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)
* any other character is used literally";
fn pick(rng: &mut impl rand::Rng, pool: &[u8]) -> char {
@ -36,6 +46,11 @@ pub fn generate_from_pattern(pattern: &str) -> String {
'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),
_ => None,
};