]> git.lizzy.rs Git - rust.git/blob - src/librustc_data_structures/base_n.rs
f1bd3f03aef8d13840aa668d4e64dbec18661b39
[rust.git] / src / librustc_data_structures / base_n.rs
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.
3
4 use std::str;
5
6 pub const MAX_BASE: usize = 64;
7 pub const ALPHANUMERIC_ONLY: usize = 62;
8 pub const CASE_INSENSITIVE: usize = 36;
9
10 const BASE_64: &[u8; MAX_BASE as usize] =
11     b"0123456789abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ@$";
12
13 #[inline]
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];
17     let mut index = 0;
18
19     let base = base as u128;
20
21     loop {
22         s[index] = BASE_64[(n % base) as usize];
23         index += 1;
24         n /= base;
25
26         if n == 0 {
27             break;
28         }
29     }
30     s[0..index].reverse();
31
32     output.push_str(str::from_utf8(&s[0..index]).unwrap());
33 }
34
35 #[inline]
36 pub fn encode(n: u128, base: usize) -> String {
37     let mut s = String::new();
38     push_str(n, base, &mut s);
39     s
40 }
41
42 #[test]
43 fn test_encode() {
44     fn test(n: u128, base: usize) {
45         assert_eq!(Ok(n), u128::from_str_radix(&encode(n, base), base as u32));
46     }
47
48     for base in 2..37 {
49         test(0, base);
50         test(1, base);
51         test(35, base);
52         test(36, base);
53         test(37, base);
54         test(u64::max_value() as u128, base);
55         test(u128::max_value(), base);
56
57         for i in 0 .. 1_000 {
58             test(i * 983, base);
59         }
60     }
61 }