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
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
//! Debugging assertions to check a radix is valid.

#[cfg(feature = "write")]
use crate::constants::FormattedSize;
use crate::format::NumberFormat;

// RADIX

/// Check radix is in range `[2, 36]` in debug builds.
#[inline]
#[cfg(feature = "radix")]
pub fn debug_assert_radix(radix: u32) {
    debug_assert!((2..=36).contains(&radix), "Numerical base must be from 2-36.");
}

/// Check radix is is 10 or a power of 2.
#[inline]
#[cfg(all(feature = "power-of-two", not(feature = "radix")))]
pub fn debug_assert_radix(radix: u32) {
    debug_assert!(matches!(radix, 2 | 4 | 8 | 10 | 16 | 32), "Numerical base must be from 2-36.");
}

/// Check radix is equal to 10.
#[inline]
#[cfg(not(feature = "power-of-two"))]
pub fn debug_assert_radix(radix: u32) {
    debug_assert!(radix == 10, "Numerical base must be 10.");
}

/// Assert radix is in range `[2, 36]`.
#[inline]
#[cfg(feature = "radix")]
pub fn assert_radix<const FORMAT: u128>() {
    assert!(
        (2..=36).contains(&NumberFormat::<{ FORMAT }>::RADIX),
        "Numerical base must be from 2-36."
    );
}

/// Check radix is is 10 or a power of 2.
#[inline]
#[cfg(all(feature = "power-of-two", not(feature = "radix")))]
pub fn assert_radix<const FORMAT: u128>() {
    assert!(
        matches!(NumberFormat::<{ FORMAT }>::RADIX, 2 | 4 | 8 | 10 | 16 | 32),
        "Numerical base must be from 2, 4, 8, 10, 16, or 32."
    );
}

/// Check radix is equal to 10.
#[inline]
#[cfg(not(feature = "power-of-two"))]
pub fn assert_radix<const FORMAT: u128>() {
    assert!(NumberFormat::<{ FORMAT }>::RADIX == 10, "Numerical base must be 10.");
}

// BUFFER

/// Debug assertion the buffer has sufficient room for the output.
#[inline]
#[cfg(feature = "write")]
pub fn debug_assert_buffer<T: FormattedSize>(radix: u32, len: usize) {
    debug_assert!(
        match radix {
            10 => len >= T::FORMATTED_SIZE_DECIMAL,
            _ => len >= T::FORMATTED_SIZE,
        },
        "Buffer is too small: may overwrite buffer in release builds."
    );
}

/// Assertion the buffer has sufficient room for the output.
#[inline]
#[cfg(all(feature = "power-of-two", feature = "write"))]
pub fn assert_buffer<T: FormattedSize>(radix: u32, len: usize) {
    assert!(
        match radix {
            10 => len >= T::FORMATTED_SIZE_DECIMAL,
            _ => len >= T::FORMATTED_SIZE,
        },
        "Buffer is too small: may overwrite buffer, panicking!"
    );
}

/// Assertion the buffer has sufficient room for the output.
#[inline]
#[cfg(all(not(feature = "power-of-two"), feature = "write"))]
pub fn assert_buffer<T: FormattedSize>(_: u32, len: usize) {
    assert!(
        len >= T::FORMATTED_SIZE_DECIMAL,
        "Buffer is too small: may overwrite buffer, panicking!"
    );
}