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
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
//---------------------------------------------------------------------------------------------------- Use
#[cfg(feature = "serde")]
use serde::{Serialize,Deserialize};

use crate::inner::*;
use crate::macros::*;
use crate::constants::*;

//---------------------------------------------------------------------------------------------------- Int
/// Human readable signed integer.
///
/// [`Int::from`] takes a signed integer as input and returns a ready-to-[`print!()`] [`Int`].
///
/// [`f32`] or [`f64`] inputs will work, but:
/// - Fractional parts will be ignored
///
/// ## Cloning
/// [`Copy`] available, [`Clone`] is cheap.
///
/// The inner type is either a `&'static str` or a buffer
/// allocated on the stack, both are able to be cheaply `Copy`-ied:
/// ```rust
/// # use readable::Int;
/// let a = Int::from(100_000);
///
/// // Copy 'a', use 'b'.
/// let b = a;
/// assert!(b == 100_000);
///
/// // We can still use 'a'
/// assert!(a == 100_000);
/// ```
///
/// ## Exceptions
/// - [`f64::NAN`] outputs [`NAN`]
/// - [`f64::INFINITY`] outputs [`INFINITY`]
///
/// To disable checks for these, (you are _sure_ you don't have NaN's), enable the `ignore_nan_inf` feature flag.
///
/// ## Math
/// These operators are overloaded. They will always output a new [`Self`]:
/// - `Add +`
/// - `Sub -`
/// - `Div /`
/// - `Mul *`
/// - `Rem %`
///
/// They can either be:
/// - Combined with another [`Self`]: `Int::from(1) + Int::from(1)`
/// - Or with the inner number itself: `Int::from(1) + 1`
///
/// They also have the same `panic!()` behavior on overflow as the normal ones, because internally,
/// it is just calling `.inner() $OPERATOR $NUMBER`.
///
/// ```rust
/// # use readable::*;
/// assert!(Int::from(10) + 10 == Int::from(20));
/// assert!(Int::from(10) - 10 == Int::from(0));
/// assert!(Int::from(10) / 10 == Int::from(1));
/// assert!(Int::from(10) * 10 == Int::from(100));
/// assert!(Int::from(10) % 10 == Int::from(0));
/// ```
/// Overflow example:
/// ```rust,should_panic
/// # use readable::*;
/// let n = Int::from(i64::MAX) + i64::MAX;
/// ```
///
/// ## Examples
/// ```rust
/// # use readable::Int;
/// // From u32.
/// assert!(Int::from(1_000_u32)     == "1,000");
/// assert!(Int::from(100_000_u32)   == "100,000");
/// assert!(Int::from(1_000_000_u32) == "1,000,000");
///
/// // From signed integers.
/// assert!(Int::from(-1_000)   == "-1,000");
/// assert!(Int::from(-100_000) == "-100,000");
/// assert!(Int::from(-100_000) == "-100,000");
///
/// // From floats.
/// assert!(Int::from(-1.0)        == "-1");
/// assert!(Int::from(1_000.123)   == "1,000");
/// assert!(Int::from(100_000.123) == "100,000");
/// assert!(Int::from(100_000.123) == "100,000");
/// ```

#[cfg_attr(feature = "serde", derive(Serialize, Deserialize))]
#[derive(Debug, Copy, Clone, Eq, PartialEq, Ord, PartialOrd)]
pub struct Int(i64, Inner);

impl Int {
	impl_inner!(i64);
	impl_common!(i64);
	impl_isize!();
}

impl_math!(Int, i64);
impl_traits!(Int, i64);
impl_from!(i8, i16, i32, i64, isize, Int);
impl_from_single!(u8, i64, Int);
impl_from_single!(u16, i64, Int);
impl_from_single!(u32, i64, Int);

//---------------------------------------------------------------------------------------------------- TESTS
#[cfg(test)]
mod tests {
	use super::*;

	#[test]
	fn unsigned() {
		assert!(Int::from(1_000_i64) == "1,000");
		assert!(Int::from(65_535_i64) == "65,535");
		assert!(Int::from(65_536_i64) == "65,536");
		assert!(Int::from(100_000_i64) == "100,000");
		assert!(Int::from(1_000_000_i64) == "1,000,000");
		assert!(Int::from(10_000_000_i64) == "10,000,000");
		assert!(Int::from(100_000_000_i64) == "100,000,000");
		assert!(Int::from(1_000_000_000_i64) == "1,000,000,000");
		assert!(Int::from(4_294_967_295_i64) == "4,294,967,295");
		assert!(Int::from(4_294_967_296_i64) == "4,294,967,296");
		assert!(Int::from(10_000_000_000_i64) == "10,000,000,000");
		assert!(Int::from(100_000_000_000_i64) == "100,000,000,000");
		assert!(Int::from(1_000_000_000_000_i64) == "1,000,000,000,000");
		assert!(Int::from(10_000_000_000_000_i64) == "10,000,000,000,000");
		assert!(Int::from(100_000_000_000_000_i64) == "100,000,000,000,000");
		assert!(Int::from(1_000_000_000_000_000_i64) == "1,000,000,000,000,000");
		assert!(Int::from(10_000_000_000_000_000_i64) == "10,000,000,000,000,000");
	}

	#[test]
	fn int() {
		assert!(Int::from(-1_000_i64) == "-1,000");
		assert!(Int::from(-65_535_i64) == "-65,535");
		assert!(Int::from(-65_536_i64) == "-65,536");
		assert!(Int::from(-100_000_i64) == "-100,000");
		assert!(Int::from(-1_000_000_i64) == "-1,000,000");
		assert!(Int::from(-10_000_000_i64) == "-10,000,000");
		assert!(Int::from(-100_000_000_i64) == "-100,000,000");
		assert!(Int::from(-1_000_000_000_i64) == "-1,000,000,000");
		assert!(Int::from(-4_294_967_295_i64) == "-4,294,967,295");
		assert!(Int::from(-4_294_967_296_i64) == "-4,294,967,296");
		assert!(Int::from(-10_000_000_000_i64) == "-10,000,000,000");
		assert!(Int::from(-100_000_000_000_i64) == "-100,000,000,000");
		assert!(Int::from(-1_000_000_000_000_i64) == "-1,000,000,000,000");
		assert!(Int::from(-10_000_000_000_000_i64) == "-10,000,000,000,000");
		assert!(Int::from(-100_000_000_000_000_i64) == "-100,000,000,000,000");
		assert!(Int::from(-1_000_000_000_000_000_i64) == "-1,000,000,000,000,000");
		assert!(Int::from(-10_000_000_000_000_000_i64) == "-10,000,000,000,000,000");

		assert!(Int::from(i64::MIN) == "-9,223,372,036,854,775,808");
		assert!(Int::from(i64::MAX) == "9,223,372,036,854,775,807");
	}

	#[test]
	fn float() {
		assert!(Int::from(-1_000.0) == "-1,000");
		assert!(Int::from(-65_535.0) == "-65,535");
		assert!(Int::from(-65_536.0) == "-65,536");
		assert!(Int::from(-100_000.0) == "-100,000");
		assert!(Int::from(-1_000_000.0) == "-1,000,000");
		assert!(Int::from(-10_000_000.0) == "-10,000,000");
		assert!(Int::from(-100_000_000.0) == "-100,000,000");
		assert!(Int::from(-1_000_000_000.0) == "-1,000,000,000");
		assert!(Int::from(-4_294_967_295.0) == "-4,294,967,295");
		assert!(Int::from(-4_294_967_296.0) == "-4,294,967,296");
		assert!(Int::from(-10_000_000_000.0) == "-10,000,000,000");
		assert!(Int::from(-100_000_000_000.0) == "-100,000,000,000");
		assert!(Int::from(-1_000_000_000_000.0) == "-1,000,000,000,000");
		assert!(Int::from(-10_000_000_000_000.0) == "-10,000,000,000,000");
		assert!(Int::from(-100_000_000_000_000.0) == "-100,000,000,000,000");
		assert!(Int::from(-1_000_000_000_000_000.0) == "-1,000,000,000,000,000");
		assert!(Int::from(i64::MIN as f64) == "-9,223,372,036,854,775,808");
		assert!(Int::from(i64::MAX as f64) == "9,223,372,036,854,775,807");
	}

	#[test]
	fn special() {
		assert!(Int::from(f64::NAN)          == crate::NAN);
		assert!(Int::from(f64::INFINITY)     == crate::INFINITY);
		assert!(Int::from(f64::NEG_INFINITY) == crate::INFINITY);
	}
}