diff --git a/.travis.yml b/.travis.yml index 0e4e989..6a5a6db 100644 --- a/.travis.yml +++ b/.travis.yml @@ -1,6 +1,17 @@ sudo: false - language: rust -rust: - - nightly +matrix: + include: + - rust: stable + - rust: beta + - rust: nightly + env: + - FEATURES="i128" + - BUILD_BENCH="true" + +script: + - cargo build --verbose --features "$FEATURES" + - cargo test --verbose --features "$FEATURES" + - if [ "$BUILD_BENCH" == "true" ]; then cargo bench --verbose --no-run --features "$FEATURES"; fi + diff --git a/Cargo.toml b/Cargo.toml index db6516d..6eb09b2 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -9,3 +9,6 @@ documentation = "https://github.com/dtolnay/itoa" categories = ["value-formatting"] readme = "README.md" exclude = ["performance.png"] + +[features] +i128 = [] diff --git a/README.md b/README.md index 9e202e9..524957b 100644 --- a/README.md +++ b/README.md @@ -41,8 +41,9 @@ fn write(writer: W, value: V) -> io::Result { + ( + $( + $(#[$attr:meta])* + $name:ident($value:expr) + ),* + ) => { mod bench_itoa { use test::{Bencher, black_box}; $( + $(#[$attr])* #[bench] fn $name(b: &mut Bencher) { use itoa; - let mut buf = Vec::with_capacity(20); + let mut buf = Vec::with_capacity(40); b.iter(|| { buf.clear(); @@ -26,11 +33,12 @@ macro_rules! benches { mod bench_fmt { use test::{Bencher, black_box}; $( + $(#[$attr])* #[bench] fn $name(b: &mut Bencher) { use std::io::Write; - let mut buf = Vec::with_capacity(20); + let mut buf = Vec::with_capacity(40); b.iter(|| { buf.clear(); @@ -42,11 +50,16 @@ macro_rules! benches { } } -benches!( - bench_0u64(0u64), - bench_HALFu64(::max_value() as u64), - bench_MAXu64(::max_value()), +benches!{ + bench_u64_0(0u64), + bench_u64_half(::max_value() as u64), + bench_u64_max(::max_value()), - bench_0i16(0i16), - bench_MINi16(::min_value()), -); + bench_i16_0(0i16), + bench_i16_min(::min_value()), + + #[cfg(feature = "i128")] + bench_u128_0(0u128), + #[cfg(feature = "i128")] + bench_u128_max(::max_value()) +} diff --git a/src/lib.rs b/src/lib.rs index 9f8b030..b999365 100644 --- a/src/lib.rs +++ b/src/lib.rs @@ -8,6 +8,8 @@ #![doc(html_root_url = "https://docs.rs/itoa/0.3.3")] +#![cfg_attr(feature = "i128", feature(i128_type, i128))] + use std::{io, mem, ptr, slice}; #[inline] @@ -30,7 +32,7 @@ const DEC_DIGITS_LUT: &'static[u8] = 6061626364656667686970717273747576777879\ 8081828384858687888990919293949596979899"; -const MAX_LEN: usize = 20; // Tie between i64::MIN (including minus sign) and u64::MAX +const MAX_LEN: usize = 40; // i128::MIN (including minus sign) // Adaptation of the original implementation at // https://github.com/rust-lang/rust/blob/b8214dc6c6fc20d0a660fb5700dca9ebf51ebe89/src/libcore/fmt/num.rs#L188-L266 @@ -115,3 +117,5 @@ impl_Integer!(isize, usize as u16); impl_Integer!(isize, usize as u32); #[cfg(target_pointer_width = "64")] impl_Integer!(isize, usize as u64); +#[cfg(feature = "i128")] +impl_Integer!(i128, u128 as u128); diff --git a/tests/test.rs b/tests/test.rs index 0a33f80..c6e26de 100644 --- a/tests/test.rs +++ b/tests/test.rs @@ -1,13 +1,20 @@ +#![cfg_attr(feature = "i128", feature(i128_type, i128))] #![allow(non_snake_case)] extern crate itoa; macro_rules! test { - ($($name:ident($value:expr, $expected:expr),)*) => { + ( $( + $(#[$attr:meta])* + $name:ident($value:expr, $expected:expr) + ),* + ) => { + $( + $(#[$attr])* #[test] fn $name() { - let mut buf = [b'\0'; 20]; + let mut buf = [b'\0'; 40]; let len = itoa::write(&mut buf[..], $value).unwrap(); assert_eq!(&buf[0..len], $expected.as_bytes()); } @@ -15,12 +22,19 @@ macro_rules! test { } } -test!( - test_0u64(0u64, "0"), - test_HALFu64(::max_value() as u64, "4294967295"), - test_MAXu64(::max_value(), "18446744073709551615"), - test_MINi64(::min_value(), "-9223372036854775808"), +test!{ + test_u64_0(0u64, "0"), + test_u64_half(::max_value() as u64, "4294967295"), + test_u64_max(::max_value(), "18446744073709551615"), + test_i64_min(::min_value(), "-9223372036854775808"), - test_0i16(0i16, "0"), - test_MINi16(::min_value(), "-32768"), -); + test_i16_0(0i16, "0"), + test_i16_min(::min_value(), "-32768"), + + #[cfg(feature = "i128")] + test_u128_0(0u128, "0"), + #[cfg(feature = "i128")] + test_u128_max(::max_value(), "340282366920938463463374607431768211455"), + #[cfg(feature = "i128")] + test_i128_min(::min_value(), "-170141183460469231731687303715884105728") +}