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
62
63
64
65
66
67
68
69
70
71
72
73
/*
Copyright 2024 Owain Davies
SPDX-License-Identifier: Apache-2.0 OR MIT
*/
use crate::{Arbi, Digit};
use alloc::string::String;
use core::fmt::Write;
impl Arbi {
/// Prints the integer in the form
///
/// ```text
/// (d_p * 2**(Digit::BITS * p) + ... + d_0 * 2**(Digit::BITS * 0))
/// ```
///
/// followed by a newline.
///
/// If the integer is negative, the output will be preceded by a minus sign
/// (`-`).
///
/// Useful for understanding the internal representation of the integer.
///
/// ## Complexity
/// \\( O(n) \\)
#[allow(dead_code)]
fn to_string_internal(&self) -> String {
let mut result = String::new();
if self.size() == 0 {
writeln!(result, "0 * 2**({} * 0)", Digit::BITS).unwrap();
return result;
}
if self.is_negative() {
result.push('-');
}
let last_index = self.size() - 1;
write!(
result,
"({} * 2**({} * {})",
self.vec[last_index],
Digit::BITS,
last_index
)
.unwrap();
for i in (0..last_index).rev() {
write!(result, " + {} * 2**({} * {})", self.vec[i], Digit::BITS, i)
.unwrap();
}
write!(result, ")").unwrap();
result
}
}
#[cfg(test)]
mod tests {
use super::*;
use crate::DDigit;
#[test]
fn print_test() {
let s = Arbi::from(Digit::MAX).to_string_internal();
assert_eq!(s, "(4294967295 * 2**(32 * 0))");
let s = Arbi::from(DDigit::MAX).to_string_internal();
assert_eq!(s, "(4294967295 * 2**(32 * 1) + 4294967295 * 2**(32 * 0))");
}
}