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
58
59
60
61
use crate::{LBNum, RADIX_TABLE, DivRemAssign};
use core::{fmt::{self, Formatter}, hint, convert::TryInto};
use alloc::string::{String, ToString};
impl LBNum {
pub fn into_string_with_radix(mut self, radix: u8) -> String {
if radix < 2 || radix > 36 {
panic!("invalid value for radix (not in range from 2 to 36, inclusively)");
}
if self == 0u8 {return '0'.to_string();}
let mut result = String::new();
while self != 0u8 {
let remainder = self.div_rem_assign(radix);
let remainder: usize = (&remainder).try_into().unwrap_or_else(|_| unsafe {hint::unreachable_unchecked()});
result.push(RADIX_TABLE[remainder]);
}
let result: String = result.chars().rev().collect();
result
}
}
impl fmt::Display for LBNum {
fn fmt(&self, f: &mut Formatter) -> fmt::Result {
f.write_str(&self.clone().into_string_with_radix(10))
}
}
impl fmt::Binary for LBNum {
fn fmt(&self, f: &mut Formatter) -> fmt::Result {
f.write_str(&self.clone().into_string_with_radix(2))
}
}
impl fmt::Octal for LBNum {
fn fmt(&self, f: &mut Formatter) -> fmt::Result {
f.write_str(&self.clone().into_string_with_radix(8))
}
}
impl fmt::UpperHex for LBNum {
fn fmt(&self, f: &mut Formatter) -> fmt::Result {
f.write_str(&self.clone().into_string_with_radix(16))
}
}
impl fmt::LowerHex for LBNum {
fn fmt(&self, f: &mut Formatter) -> fmt::Result {
let mut stringified = self.clone().into_string_with_radix(16);
stringified.make_ascii_lowercase();
f.write_str(&stringified)
}
}