tailwind_css_fixes/systems/builder/base62/
mod.rs

1pub const BASE62: &[u8; 62] = b"ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789";
2
3/// Encodes a u64 hash to base-62 string.
4pub trait Base62 {
5    /// Encodes a u64 hash to base-62 string.
6    fn base62(&self) -> String;
7}
8
9impl Base62 for u64 {
10    /// Since `52*62^10 > 2^64`, so 11 digit base62 string can represent u64 hash.
11    fn base62(&self) -> String {
12        const L: usize = 11;
13        const R1: u64 = 52;
14        const R2: u64 = 62;
15        let mut v = *self;
16        let mut digits: Vec<u8> = Vec::with_capacity(L);
17        if v > 0 {
18            let digit = (v % R1) as usize;
19            v /= R1;
20            digits.push(BASE62[digit]);
21        }
22        while v > 0 {
23            let digit = (v % R2) as usize;
24            v /= R2;
25            digits.push(BASE62[digit]);
26        }
27        while digits.len() < L {
28            digits.push(b'A');
29        }
30        unsafe { String::from_utf8_unchecked(digits) }
31    }
32}
33
34impl Base62 for u32 {
35    /// Since `52*62^5 > 2^32`, so 6 digit base62 string can represent u32 hash.
36    fn base62(&self) -> String {
37        const L: usize = 6;
38        const R1: u32 = 52;
39        const R2: u32 = 62;
40        let mut v = *self;
41        let mut digits: Vec<u8> = Vec::with_capacity(L);
42        if v > 0 {
43            let digit = (v % R1) as usize;
44            v /= R1;
45            digits.push(BASE62[digit]);
46        }
47        while v > 0 {
48            let digit = (v % R2) as usize;
49            v /= R2;
50            digits.push(BASE62[digit]);
51        }
52        while digits.len() < L {
53            digits.push(b'A');
54        }
55        unsafe { String::from_utf8_unchecked(digits) }
56    }
57}