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
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
//! This crate provides even faster functions for printing integers with decimal format
//! than [itoa](https://crates.io/crates/itoa) crate.
//!
//! If you want to write integers in decimal format to `String`, `Vec` or any other
//! contiguous buffer, then this crate is the best choice.
//!
//! If you want to write integers to a `std::io::Write` or `std::fmt::Write`,
//! [itoa](https://github.com/dtolnay/itoa) crate and `itoap` crate shows almost same
//! performance.
//!
//! The implementation is based on the `sse2` algorithm from
//! [itoa-benchmark](https://github.com/miloyip/itoa-benchmark) repository.
//! While `itoa` crate writes integers from **last** digits, this algorithm writes
//! from **first** digits. It allows integers to be written directly to the buffer.
//! That's why `itoap` is faster than `itoa`.
//!
//! # Feature Flags
//!
//! - `alloc`: use [alloc](https://doc.rust-lang.org/alloc/) crate (enabled by default)
//! - `std`: use [std](https://doc.rust-lang.org/std/) crate (enabled by default)
//! - `simd`: use SIMD intrinsics if available
//!
//! # Examples
//!
//! ```
//! # #[cfg(feature = "std")] {
//! let value = 17u64;
//!
//! let mut buf = String::new();
//! buf.push_str("value: ");
//! itoap::write_to_string(&mut buf, value);
//!
//! assert_eq!(buf, "value: 17");
//! # }
//! ```
//!
//! ```
//! use core::mem::{MaybeUninit, transmute};
//! use itoap::Integer;
//!
//! unsafe {
//!     let mut buf = [MaybeUninit::<u8>::uninit(); i32::MAX_LEN];
//!     let len = itoap::write_to_ptr(buf.as_mut_ptr() as *mut u8, -2953);
//!     let result: &[u8] = transmute(&buf[..len]);
//!     assert_eq!(result, b"-2953");
//! }
//! ```

#![allow(clippy::many_single_char_names, clippy::needless_range_loop)]
#![cfg_attr(docsrs, feature(doc_cfg))]
#![no_std]

#[cfg(feature = "alloc")]
extern crate alloc;
#[cfg(feature = "alloc")]
use alloc::string::String;
#[cfg(feature = "alloc")]
use alloc::vec::Vec;

#[cfg(feature = "std")]
extern crate std;

mod common;
use common::*;

#[cfg(not(all(
    any(target_arch = "x86_64", target_arch = "x86"),
    target_feature = "sse2",
    feature = "simd",
    not(miri),
)))]
mod fallback;

#[cfg(not(all(
    any(target_arch = "x86_64", target_arch = "x86"),
    target_feature = "sse2",
    feature = "simd",
    not(miri),
)))]
use fallback::{write_u32, write_u64};

#[cfg(all(
    any(target_arch = "x86_64", target_arch = "x86"),
    target_feature = "sse2",
    feature = "simd",
    not(miri),
))]
mod sse2;

#[cfg(all(
    any(target_arch = "x86_64", target_arch = "x86"),
    target_feature = "sse2",
    feature = "simd",
    not(miri),
))]
use sse2::{write_u32, write_u64};

mod private {
    pub trait Sealed {}
}

/// An integer that can be written to pointer.
pub trait Integer: private::Sealed {
    /// Maximum digits of the integer
    const MAX_LEN: usize;

    #[doc(hidden)]
    unsafe fn write_to(self, buf: *mut u8) -> usize;
}

macro_rules! impl_integer {
    ($unsigned:ty, $signed:ty, $conv:ty, $func:ident, $max_len:expr) => {
        impl private::Sealed for $unsigned {}
        impl private::Sealed for $signed {}

        impl Integer for $unsigned {
            const MAX_LEN: usize = $max_len;

            #[inline]
            unsafe fn write_to(self, buf: *mut u8) -> usize {
                $func(self as $conv, buf)
            }
        }

        impl Integer for $signed {
            const MAX_LEN: usize = $max_len + 1;

            #[inline]
            unsafe fn write_to(self, mut buf: *mut u8) -> usize {
                let mut n = self as $conv;
                if self < 0 {
                    *buf = b'-';
                    buf = buf.add(1);
                    n = (!n).wrapping_add(1);
                }

                $func(n, buf) + (self < 0) as usize
            }
        }
    };
}

impl_integer!(u8, i8, u8, write_u8, 3);
impl_integer!(u16, i16, u16, write_u16, 5);
impl_integer!(u32, i32, u32, write_u32, 10);
impl_integer!(u64, i64, u64, write_u64, 20);
impl_integer!(u128, i128, u128, write_u128, 39);

#[cfg(target_pointer_width = "16")]
impl_integer!(usize, isize, u16, write_u16, 5);

#[cfg(target_pointer_width = "32")]
impl_integer!(usize, isize, u32, write_u32, 10);

#[cfg(target_pointer_width = "64")]
impl_integer!(usize, isize, u64, write_u64, 20);

/// Write integer to the buffer pointer directly.
///
/// This is fast operation, but does not check any safety.
///
/// # Safety
///
/// Behaviour is undefined if any of the following conditions are violated:
///
/// - `buf` must point to sufficient
/// [valid](https://doc.rust-lang.org/core/ptr/index.html#safety) bytes of memory to
/// write `value`
/// - `buf` must be aligned with `core::mem::align_of::<u8>()` bytes
#[inline]
pub unsafe fn write_to_ptr<V: Integer>(buf: *mut u8, value: V) -> usize {
    value.write_to(buf)
}

/// Write integer to `Vec<u8>`.
///
/// Note that this function is safe because it checks the capacity of `Vec` and calls
/// `Vec::reserve()` if the `Vec` doesn't have enough capacity.
#[cfg(feature = "alloc")]
#[cfg_attr(docsrs, doc(cfg(feature = "alloc")))]
#[inline]
pub fn write_to_vec<V: Integer>(buf: &mut Vec<u8>, value: V) {
    debug_assert!(buf.len() <= core::isize::MAX as usize);

    // benchmark result suggests that we gain more speed by manually checking the
    // buffer capacity and limits `reserve()` call
    if buf.len().wrapping_add(V::MAX_LEN) > buf.capacity() {
        buf.reserve(V::MAX_LEN);
    }

    unsafe {
        let l = value.write_to(buf.as_mut_ptr().add(buf.len()));
        buf.set_len(buf.len() + l);
    }
}

/// Write integer to `String`.
///
/// Note that this function is safe because it checks the capacity of `String` and calls
/// `String::reserve()` if the `String` doesn't have enough capacity.
#[cfg(feature = "alloc")]
#[cfg_attr(docsrs, doc(cfg(feature = "alloc")))]
#[inline]
pub fn write_to_string<V: Integer>(buf: &mut String, value: V) {
    unsafe { write_to_vec(buf.as_mut_vec(), value) };
}

/// Write integer to an `fmt::Write`
///
/// Note that this operation may be slow because it writes the `value` to stack memory,
/// and then copy the result into `writer`.
///
/// This function is for compatibility with [itoa](https://docs.rs/itoa) crate and you
/// should use `write_to_vec` or `write_to_string` if possible.
#[inline]
pub fn fmt<W: core::fmt::Write, V: Integer>(
    mut writer: W,
    value: V,
) -> core::fmt::Result {
    use core::mem::MaybeUninit;

    unsafe {
        let mut buf = [MaybeUninit::<u8>::uninit(); 40];
        let l = value.write_to(buf.as_mut_ptr() as *mut u8);
        let slc = core::slice::from_raw_parts(buf.as_ptr() as *const u8, l);
        writer.write_str(core::str::from_utf8_unchecked(slc))
    }
}

/// Write integer to an `io::Write`
///
/// Note that this operation may be slow because it writes the `value` to stack memory,
/// and then copy the result into `writer`.
/// You should use `write_to_vec` or `write_to_string` if possible.
///
/// This function is for compatibility with [itoa](https://docs.rs/itoa) crate and you
/// should use `write_to_vec` or `write_to_string` if possible.
#[cfg(feature = "std")]
#[cfg_attr(docsrs, doc(cfg(feature = "std")))]
#[inline]
pub fn write<W: std::io::Write, V: Integer>(
    mut writer: W,
    value: V,
) -> std::io::Result<usize> {
    use core::mem::MaybeUninit;

    unsafe {
        let mut buf = [MaybeUninit::<u8>::uninit(); 40];
        let l = value.write_to(buf.as_mut_ptr() as *mut u8);
        let slc = core::slice::from_raw_parts(buf.as_ptr() as *const u8, l);
        writer.write(slc)
    }
}

#[cfg(test)]
mod tests {
    use core::cmp::PartialEq;
    use core::fmt;
    use rand::rngs::SmallRng;
    use rand::{Rng, SeedableRng};

    struct ArrayStr {
        buf: [u8; 40],
        len: usize,
    }

    impl ArrayStr {
        fn new() -> Self {
            Self {
                buf: [0u8; 40],
                len: 0,
            }
        }

        fn as_str(&self) -> &str {
            core::str::from_utf8(&self.buf[..self.len]).unwrap()
        }
    }

    impl fmt::Write for ArrayStr {
        fn write_str(&mut self, s: &str) -> fmt::Result {
            self.buf[self.len..self.len + s.len()].copy_from_slice(s.as_bytes());
            self.len += s.len();
            Ok(())
        }
    }

    impl fmt::Debug for ArrayStr {
        fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
            self.as_str().fmt(f)
        }
    }

    impl PartialEq<ArrayStr> for ArrayStr {
        fn eq(&self, rhs: &ArrayStr) -> bool {
            self.as_str().eq(rhs.as_str())
        }
    }

    fn itoap_fmt<I: super::Integer>(value: I) -> ArrayStr {
        let mut buf = ArrayStr::new();
        let _ = super::fmt(&mut buf, value);
        buf
    }

    fn std_fmt<I: fmt::Display>(value: I) -> ArrayStr {
        use core::fmt::Write;

        let mut buf = ArrayStr::new();
        let _ = write!(buf, "{}", value);
        buf
    }

    // comprehenisive test
    #[test]
    fn test_i8_all() {
        for n in core::i8::MIN..=core::i8::MAX {
            assert_eq!(itoap_fmt(n), std_fmt(n));
        }
    }

    // random test
    #[test]
    #[cfg(not(miri))]
    fn test_u64_random() {
        let mut rng = SmallRng::seed_from_u64(0xb0d39604298743d0);

        for _ in 0..1000 {
            let value = rng.gen::<u64>();
            assert_eq!(itoap_fmt(value), std_fmt(value));
        }
    }

    // random test
    #[test]
    #[cfg(not(miri))]
    fn test_u128_random() {
        let mut rng = SmallRng::seed_from_u64(0x73cdb9a66816e721);

        for _ in 0..1000 {
            let value = rng.gen::<u128>();
            assert_eq!(itoap_fmt(value), std_fmt(value));
        }
    }

    // random digits test
    #[test]
    #[cfg(not(miri))]
    fn test_u64_random_digits() {
        let mut rng = SmallRng::seed_from_u64(0xe6f827f2dce6fae4);

        for _ in 0..1000 {
            let value = rng.gen::<u64>() >> (rng.gen::<u8>() % 64);
            assert_eq!(itoap_fmt(value), std_fmt(value));
        }
    }

    // random digits test
    #[test]
    #[cfg(not(miri))]
    fn test_u128_random_digits() {
        let mut rng = SmallRng::seed_from_u64(0xd7b31256794c1406);

        for _ in 0..1000 {
            let value = rng.gen::<u128>() >> (rng.gen::<u8>() % 128);
            assert_eq!(itoap_fmt(value), std_fmt(value));
        }
    }

    // cov:begin-ignore
    macro_rules! boundary_test {
        ($name:ident, $type:ident) => {
            #[test]
            fn $name() {
                let mut current = 1;
                loop {
                    assert_eq!(itoap_fmt(current - 1), std_fmt(current - 1));
                    assert_eq!(itoap_fmt(current), std_fmt(current));
                    assert_eq!(itoap_fmt(current + 1), std_fmt(current + 1));

                    if current > core::$type::MAX / 10 {
                        break;
                    }

                    current *= 10;
                }

                assert_eq!(itoap_fmt(core::$type::MIN), std_fmt(core::$type::MIN));
                assert_eq!(itoap_fmt(core::$type::MAX), std_fmt(core::$type::MAX));
            }
        };
    }
    // cov:end-ignore

    // boundary tests
    boundary_test!(test_u8, u8);
    boundary_test!(test_u16, u16);
    boundary_test!(test_u32, u32);
    boundary_test!(test_u64, u64);
    boundary_test!(test_u128, u128);
    boundary_test!(test_usize, usize);

    boundary_test!(test_i8, i8);
    boundary_test!(test_i16, i16);
    boundary_test!(test_i32, i32);
    boundary_test!(test_i64, i64);
    boundary_test!(test_i128, i128);
    boundary_test!(test_isize, isize);

    #[test]
    #[cfg(feature = "alloc")]
    #[cfg(not(miri))]
    fn write_to_string_test() {
        use alloc::string::{String, ToString};

        let mut buf = String::new();
        let mut rng = SmallRng::seed_from_u64(0xa0983844f42abf9d);

        for _ in 0..1000 {
            let value = rng.gen::<i32>();
            buf.clear();
            super::write_to_string(&mut buf, value);
            assert_eq!(buf, value.to_string());
        }
    }

    #[test]
    #[cfg(feature = "std")]
    #[cfg(not(miri))]
    fn io_test() {
        use alloc::string::ToString;
        use alloc::vec::Vec;

        let mut buf = Vec::new();
        let mut rng = SmallRng::seed_from_u64(0x36f09d2f9acc29b8);

        for _ in 0..1000 {
            // xorshift
            let value = rng.gen::<i64>();
            buf.clear();
            super::write(&mut buf, value).unwrap();
            assert_eq!(std::str::from_utf8(&*buf).unwrap(), value.to_string());
        }
    }
}