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
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
//---------------------------------------------------------------------------------------------------- Use
#[cfg(feature = "serde")]
use serde::{Serialize,Deserialize};

use std::num::*;
use crate::macros::*;
use crate::constants::*;

//---------------------------------------------------------------------------------------------------- Unsigned
/// Human readable unsigned integer.
///
/// ## Creation
/// For [`u8`], [`u16`], [`u32`], [`u64`], [`usize`] or any [`NonZeroU8`] variant:
/// - Use [`Unsigned::from`]
///
/// [`f32`] or [`f64`] inputs will work, but:
/// - Signed floats will turn into `0`
/// - Fractional parts will be ignored
/// - Under/overflows will return [`Unsigned::unknown`]
/// - Special floats like [`f64::NAN`] will return [`Unsigned::unknown`]
///
/// For [`i8`] and other signed integers:
/// - You need to use [`Unsigned::try_from`]
/// - [`Unsigned::unknown`] will be returned on error
///
/// ## Cloning
/// [`Copy`] is available.
///
/// The actual string used internally is not a [`String`](https://doc.rust-lang.org/std/string/struct.String.html),
/// but a 26 byte array buffer, literally: `[u8; 26]`.
///
/// The documentation will still refer to the inner buffer as a [`String`]. Anything returned will also be a [`String`].
/// ```rust
/// # use readable::Unsigned;
/// let a = Unsigned::from(100_000_u64);
///
/// // Copy 'a', use 'b'.
/// let b = a;
/// assert!(b == 100_000_u64);
///
/// // We can still use 'a'
/// assert!(a == 100_000_u64);
/// ```
///
/// ## Float Errors
/// - Inputting [`f64::NAN`] returns [`Unsigned::unknown`]
/// - Inputting [`f64::INFINITY`] returns [`Unsigned::unknown`]
/// - Inputting [`f64::NEG_INFINITY`] returns [`Unsigned::unknown`]
///
/// 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`]: `Unsigned::from(1) + Unsigned::from(1)`
/// - Or with the inner number itself: `Unsigned::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!(Unsigned::from(10_u64) + 10 == Unsigned::from(20_u64));
/// assert!(Unsigned::from(10_u64) - 10 == Unsigned::from(0_u64));
/// assert!(Unsigned::from(10_u64) / 10 == Unsigned::from(1_u64));
/// assert!(Unsigned::from(10_u64) * 10 == Unsigned::from(100_u64));
/// assert!(Unsigned::from(10_u64) % 10 == Unsigned::from(0_u64));
/// ```
/// Overflow example:
/// ```rust,should_panic
/// # use readable::*;
/// let n = Unsigned::from(u64::MAX) + u64::MAX;
/// ```
///
/// ## Examples
/// ```rust
/// # use readable::Unsigned;
/// // From unsigned integers.
/// assert!(Unsigned::from(100_u8)        == "100");
/// assert!(Unsigned::from(10_000_u16)    == "10,000");
/// assert!(Unsigned::from(100_000_u32)   == "100,000");
/// assert!(Unsigned::from(1_000_000_u64) == "1,000,000");
///
/// // From floats.
/// assert!(Unsigned::from(-1.0)        == "0");
/// assert!(Unsigned::from(1_000.123)   == "1,000");
/// assert!(Unsigned::from(100_000.123) == "100,000");
/// assert!(Unsigned::from(100_000.123) == "100,000");
/// assert!(Unsigned::from(f32::NAN)    == "???");
///
/// // From signed integers.
/// assert!(Unsigned::try_from(100_i8)         == Ok(Unsigned::from(100_u8)));
/// assert!(Unsigned::try_from(-100_i8)        == Err(Unsigned::unknown()));
/// assert!(Unsigned::try_from(1_000_000_i64)  == Ok(Unsigned::from(1_000_000_u32)));
/// assert!(Unsigned::try_from(-1_000_000_i64) == Err(Unsigned::unknown()));
/// ```

#[cfg_attr(feature = "serde", derive(Serialize, Deserialize))]
#[cfg_attr(feature = "bincode", derive(bincode::Encode, bincode::Decode))]
#[derive(Debug, Copy, Clone, Eq, PartialEq, Ord, PartialOrd, Hash)]
pub struct Unsigned(u64, Buffer);

impl Unsigned {
	impl_common!(u64);
	impl_const!();
	impl_usize!();
	impl_buffer!(MAX_BUF_LEN, UNKNOWN_NUM_BUFFER, UNKNOWN.len());

	#[inline]
	/// ```rust
	/// # use readable::*;
	/// assert!(Unsigned::zero() == 0);
	/// ```
	pub const fn zero() -> Self {
		Self(0, Buffer::zero())
	}

	#[inline]
	/// ```rust
	/// # use readable::*;
	/// assert!(Unsigned::unknown() == UNKNOWN);
	/// ```
	pub const fn unknown() -> Self {
		Self(0, Buffer::unknown())
	}
}

macro_rules! impl_u {
	($( $from:ty ),*) => {
		$(
			impl From<$from> for Unsigned {
				fn from(uint: $from) -> Self {
					let u = uint as u64;
					Self(u, Buffer::from_u(u))
				}
			}
		)*
	}
}
impl_u!(u8,u16,u32,u64,usize);

macro_rules! impl_nonu {
	($( $from:ty ),*) => {
		$(
			impl From<$from> for Unsigned {
				fn from(uint: $from) -> Self {
					let u = uint.get() as u64;
					Self(u, Buffer::from_u(u))
				}
			}
		)*
	}
}
impl_nonu! {
	NonZeroU8,NonZeroU16,NonZeroU32,NonZeroU64,NonZeroUsize,
	&NonZeroU8,&NonZeroU16,&NonZeroU32,&NonZeroU64,&NonZeroUsize
}

macro_rules! impl_f {
	($from:ty) => {
		/// This will silently return [`Self::unknown`]
		/// if the input float is `NAN`, `INFINITY`, etc.
		///
		/// [`Self::zero`] will be returned on negative floats.
		impl From<$from> for Unsigned {
			fn from(float: $from) -> Self {
				handle_nan_runtime!(float);
				let u = float as u64;
				Self(u, Buffer::from_u(u))
			}
		}
	}
}
impl_f!(f32);
impl_f!(f64);

macro_rules! impl_try {
	($( $from:ty ),*) => {
		$(
			/// This will return [`Self::unknown`] wrapped
			/// in [`Result::Err`] if the conversion fails.
			impl TryFrom<$from> for Unsigned {
				type Error = Self;
				fn try_from(num: $from) -> Result<Self, Self> {
					match u64::try_from(num) {
						Ok(u) => Ok(Self(u, Buffer::from_u(u))),
						_ => Err(Self::unknown()),
					}
				}
			}
		)*
	}
}
impl_try!(i8,i16,i32,i64,isize);

macro_rules! impl_noni {
	($( $from:ty ),*) => {
		$(
			/// This will return [`Self::unknown`] wrapped
			/// in [`Result::Err`] if the conversion fails.
			impl TryFrom<$from> for Unsigned {
				type Error = Self;
				fn try_from(num: $from) -> Result<Self, Self> {
					match u64::try_from(num.get()) {
						Ok(u) => Ok(Self(u, Buffer::from_u(u))),
						_ => Err(Self::unknown()),
					}
				}
			}
		)*
	}
}
impl_noni! {
	NonZeroI8,NonZeroI16,NonZeroI32,NonZeroI64,NonZeroIsize,
	&NonZeroI8,&NonZeroI16,&NonZeroI32,&NonZeroI64,&NonZeroIsize
}

impl_math!(Unsigned, u64);
impl_traits!(Unsigned, u64);

//---------------------------------------------------------------------------------------------------- Buffer
buffer!(MAX_BUF_LEN, UNKNOWN_NUM_BUFFER, UNKNOWN.len());

impl Buffer {
	#[inline(always)]
	const fn zero() -> Self {
		Self {
			buf: ZERO_NUM_BUFFER,
			len: 1,
		}
	}

	#[inline(always)]
	fn from_u(u: u64) -> Self {
		let (buf, len) = crate::buf::from_u(u);
		Self { buf, len }
	}
}

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

	#[test]
	fn unsigned() {
		assert_eq!(Unsigned::from(1_000_u64),                      "1,000");
		assert_eq!(Unsigned::from(65_535_u64),                     "65,535");
		assert_eq!(Unsigned::from(65_536_u64),                     "65,536");
		assert_eq!(Unsigned::from(100_000_u64),                    "100,000");
		assert_eq!(Unsigned::from(1_000_000_u64),                  "1,000,000");
		assert_eq!(Unsigned::from(10_000_000_u64),                 "10,000,000");
		assert_eq!(Unsigned::from(100_000_000_u64),                "100,000,000");
		assert_eq!(Unsigned::from(1_000_000_000_u64),              "1,000,000,000");
		assert_eq!(Unsigned::from(4_294_967_295_u64),              "4,294,967,295");
		assert_eq!(Unsigned::from(4_294_967_296_u64),              "4,294,967,296");
		assert_eq!(Unsigned::from(10_000_000_000_u64),             "10,000,000,000");
		assert_eq!(Unsigned::from(100_000_000_000_u64),            "100,000,000,000");
		assert_eq!(Unsigned::from(1_000_000_000_000_u64),          "1,000,000,000,000");
		assert_eq!(Unsigned::from(10_000_000_000_000_u64),         "10,000,000,000,000");
		assert_eq!(Unsigned::from(100_000_000_000_000_u64),        "100,000,000,000,000");
		assert_eq!(Unsigned::from(1_000_000_000_000_000_u64),      "1,000,000,000,000,000");
		assert_eq!(Unsigned::from(10_000_000_000_000_000_u64),     "10,000,000,000,000,000");
		assert_eq!(Unsigned::from(18_446_744_073_709_551_615_u64), "18,446,744,073,709,551,615");
	}

	#[test]
	fn float() {
		assert_eq!(Unsigned::from(1_000.0),                      "1,000");
		assert_eq!(Unsigned::from(65_535.0),                     "65,535");
		assert_eq!(Unsigned::from(65_536.0),                     "65,536");
		assert_eq!(Unsigned::from(100_000.0),                    "100,000");
		assert_eq!(Unsigned::from(1_000_000.0),                  "1,000,000");
		assert_eq!(Unsigned::from(10_000_000.0),                 "10,000,000");
		assert_eq!(Unsigned::from(100_000_000.0),                "100,000,000");
		assert_eq!(Unsigned::from(1_000_000_000.0),              "1,000,000,000");
		assert_eq!(Unsigned::from(4_294_967_295.0),              "4,294,967,295");
		assert_eq!(Unsigned::from(4_294_967_296.0),              "4,294,967,296");
		assert_eq!(Unsigned::from(10_000_000_000.0),             "10,000,000,000");
		assert_eq!(Unsigned::from(100_000_000_000.0),            "100,000,000,000");
		assert_eq!(Unsigned::from(1_000_000_000_000.0),          "1,000,000,000,000");
		assert_eq!(Unsigned::from(10_000_000_000_000.0),         "10,000,000,000,000");
		assert_eq!(Unsigned::from(100_000_000_000_000.0),        "100,000,000,000,000");
		assert_eq!(Unsigned::from(1_000_000_000_000_000.0),      "1,000,000,000,000,000");
		assert_eq!(Unsigned::from(10_000_000_000_000_000.0),     "10,000,000,000,000,000");
		assert_eq!(Unsigned::from(18_446_744_073_709_551_615.0), "18,446,744,073,709,551,615");
	}

	#[test]
	fn special() {
		assert_eq!(Unsigned::from(f64::NAN),          crate::UNKNOWN);
		assert_eq!(Unsigned::from(f64::INFINITY),     crate::UNKNOWN);
		assert_eq!(Unsigned::from(f64::NEG_INFINITY), crate::UNKNOWN);
	}
}