Skip to content

Commit 78cc52f

Browse files
authored
Merge pull request #64 from dtolnay/formula
Add formula for MAX_STR_LEN
2 parents 7f68977 + b150860 commit 78cc52f

File tree

2 files changed

+36
-18
lines changed

2 files changed

+36
-18
lines changed

src/lib.rs

Lines changed: 20 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -132,18 +132,19 @@ const DEC_DIGITS_LUT: [u8; 200] = *b"\
132132
// Adaptation of the original implementation at
133133
// https://github.com/rust-lang/rust/blob/b8214dc6c6fc20d0a660fb5700dca9ebf51ebe89/src/libcore/fmt/num.rs#L188-L266
134134
macro_rules! impl_Integer {
135-
($t:ty[len = $max_len:expr] as $large_unsigned:ty) => {
135+
($t:ty as $large_unsigned:ty) => {
136136
impl Integer for $t {
137-
const MAX_STR_LEN: usize = $max_len;
137+
#[allow(unused_comparisons)]
138+
const MAX_STR_LEN: usize = <$t>::MAX.ilog10() as usize + 1 + (<$t>::MIN < 0) as usize;
138139
}
139140

140141
impl private::Sealed for $t {
141-
type Buffer = [MaybeUninit<u8>; $max_len];
142+
type Buffer = [MaybeUninit<u8>; Self::MAX_STR_LEN];
142143

143144
#[allow(unused_comparisons)]
144145
#[inline]
145146
#[cfg_attr(feature = "no-panic", no_panic)]
146-
fn write(self, buf: &mut [MaybeUninit<u8>; $max_len]) -> &str {
147+
fn write(self, buf: &mut Self::Buffer) -> &str {
147148
let is_nonnegative = self >= 0;
148149
let mut n = if is_nonnegative {
149150
self as $large_unsigned
@@ -208,14 +209,14 @@ macro_rules! impl_Integer {
208209
};
209210
}
210211

211-
impl_Integer!(i8[len = 4] as u32);
212-
impl_Integer!(u8[len = 3] as u32);
213-
impl_Integer!(i16[len = 6] as u32);
214-
impl_Integer!(u16[len = 5] as u32);
215-
impl_Integer!(i32[len = 11] as u32);
216-
impl_Integer!(u32[len = 10] as u32);
217-
impl_Integer!(i64[len = 20] as u64);
218-
impl_Integer!(u64[len = 20] as u64);
212+
impl_Integer!(i8 as u32);
213+
impl_Integer!(u8 as u32);
214+
impl_Integer!(i16 as u32);
215+
impl_Integer!(u16 as u32);
216+
impl_Integer!(i32 as u32);
217+
impl_Integer!(u32 as u32);
218+
impl_Integer!(i64 as u64);
219+
impl_Integer!(u64 as u64);
219220

220221
macro_rules! impl_Integer_size {
221222
($t:ty as $primitive:ident #[cfg(target_pointer_width = $width:literal)]) => {
@@ -245,18 +246,19 @@ impl_Integer_size!(isize as i64 #[cfg(target_pointer_width = "64")]);
245246
impl_Integer_size!(usize as u64 #[cfg(target_pointer_width = "64")]);
246247

247248
macro_rules! impl_Integer128 {
248-
($t:ty[len = $max_len:expr]) => {
249+
($t:ty) => {
249250
impl Integer for $t {
250-
const MAX_STR_LEN: usize = $max_len;
251+
#[allow(unused_comparisons)]
252+
const MAX_STR_LEN: usize = <$t>::MAX.ilog10() as usize + 1 + (<$t>::MIN < 0) as usize;
251253
}
252254

253255
impl private::Sealed for $t {
254-
type Buffer = [MaybeUninit<u8>; $max_len];
256+
type Buffer = [MaybeUninit<u8>; Self::MAX_STR_LEN];
255257

256258
#[allow(unused_comparisons)]
257259
#[inline]
258260
#[cfg_attr(feature = "no-panic", no_panic)]
259-
fn write(self, buf: &mut [MaybeUninit<u8>; $max_len]) -> &str {
261+
fn write(self, buf: &mut Self::Buffer) -> &str {
260262
let is_nonnegative = self >= 0;
261263
let n = if is_nonnegative {
262264
self as u128
@@ -325,5 +327,5 @@ macro_rules! impl_Integer128 {
325327
};
326328
}
327329

328-
impl_Integer128!(i128[len = 40]);
329-
impl_Integer128!(u128[len = 39]);
330+
impl_Integer128!(i128);
331+
impl_Integer128!(u128);

tests/test.rs

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -27,3 +27,19 @@ test! {
2727
test_i128_min(i128::MIN, "-170141183460469231731687303715884105728")
2828
test_i128_max(i128::MAX, "170141183460469231731687303715884105727")
2929
}
30+
31+
#[test]
32+
fn test_max_str_len() {
33+
use itoa::Integer as _;
34+
35+
assert_eq!(i8::MAX_STR_LEN, 4);
36+
assert_eq!(u8::MAX_STR_LEN, 3);
37+
assert_eq!(i16::MAX_STR_LEN, 6);
38+
assert_eq!(u16::MAX_STR_LEN, 5);
39+
assert_eq!(i32::MAX_STR_LEN, 11);
40+
assert_eq!(u32::MAX_STR_LEN, 10);
41+
assert_eq!(i64::MAX_STR_LEN, 20);
42+
assert_eq!(u64::MAX_STR_LEN, 20);
43+
assert_eq!(i128::MAX_STR_LEN, 40);
44+
assert_eq!(u128::MAX_STR_LEN, 39);
45+
}

0 commit comments

Comments
 (0)