1 /// Converts unsigned integers into a string representation with some base.
2 /// Bases up to and including 36 can be used for case-insensitive things.
6 pub const MAX_BASE: usize = 64;
7 pub const ALPHANUMERIC_ONLY: usize = 62;
8 pub const CASE_INSENSITIVE: usize = 36;
10 const BASE_64: &[u8; MAX_BASE as usize] =
11 b"0123456789abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ@$";
14 pub fn push_str(mut n: u128, base: usize, output: &mut String) {
15 debug_assert!(base >= 2 && base <= MAX_BASE);
16 let mut s = [0u8; 128];
19 let base = base as u128;
22 s[index] = BASE_64[(n % base) as usize];
30 s[0..index].reverse();
32 output.push_str(str::from_utf8(&s[0..index]).unwrap());
36 pub fn encode(n: u128, base: usize) -> String {
37 let mut s = String::new();
38 push_str(n, base, &mut s);
44 fn test(n: u128, base: usize) {
45 assert_eq!(Ok(n), u128::from_str_radix(&encode(n, base), base as u32));
54 test(u64::max_value() as u128, base);
55 test(u128::max_value(), base);