pub const BASE: u8 = 62; pub const ALPHABET: &'static [u8; BASE as usize] = b"0123456789abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ"; pub fn number_to_digits(mut x: u128, base: u8) -> Vec { let mut out = Vec::new(); let base = base as u128; while x > base { out.push((x % base) as u8); x /= base; } out.push(x as u8); out.reverse(); out } #[allow(dead_code)] pub fn digits_to_number(x: I, base: u8) -> u128 where I: Iterator { let base = base as u128; x.fold(0, |out, digit| out * base + digit as u128) } pub fn digits_to_string<'a, I>(x: I, alphabet: &'a [u8]) -> String where I: Iterator { x.map(|d| alphabet[d as usize] as char).collect() } #[cfg(test)] mod test { use crate::base62::*; #[test] fn test_number_to_digits() { assert_eq!(vec![0], number_to_digits(0, 10)); assert_eq!(vec![8], number_to_digits(8, BASE)); assert_eq!(vec![1, 2], number_to_digits(64, BASE)); assert_eq!(vec![6, 4], number_to_digits(64, 10)); } #[test] fn test_digits_to_number() { assert_eq!(digits_to_number(vec![1u8, 2].into_iter(), BASE), 64); assert_eq!(digits_to_number(vec![6u8, 4].into_iter(), 10), 64); } #[test] fn test_digits_to_string() { assert_eq!(digits_to_string(vec![1u8, 2].into_iter(), ALPHABET), "12"); } }