1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
use bigint::BigUint;
pub(crate) fn encode<T>(alpha: &[T], input: &[u8]) -> Vec<T>
where
    T: Copy,
{
    if input.is_empty() {
        return Vec::new();
    }
    let base = alpha.len() as u32;
    
    let mut big = BigUint::from_bytes_be(input);
    let mut out = Vec::with_capacity(input.len());
    
    let big_pow = 32 / (32 - base.leading_zeros());
    let big_base = base.pow(big_pow);
    'fast: loop {
        
        
        
        
        
        
        let mut big_rem = big.div_mod(big_base);
        if big.is_zero() {
            loop {
                let (result, remainder) = (big_rem / base, big_rem % base);
                out.push(alpha[remainder as usize]);
                big_rem = result;
                if big_rem == 0 {
                    break 'fast; 
                }
            }
        } else {
            for _ in 0..big_pow {
                let (result, remainder) = (big_rem / base, big_rem % base);
                out.push(alpha[remainder as usize]);
                big_rem = result;
            }
        }
    }
    let leaders = input
        .iter()
        .take(input.len() - 1)
        .take_while(|i| **i == 0)
        .map(|_| alpha[0]);
    out.extend(leaders);
    out
}