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
use core::mem;

use itoa;

use crate::constants::{MAX_BUF_LEN, TABLE};
use crate::sealed::Sealed;
use crate::utils::{self, Separator};
use crate::{Buffer, Format, ToFormattedStr};

impl ToFormattedStr for u8 {
    #[doc(hidden)]
    #[inline(always)]
    fn read_to_buffer<'a, F>(&self, buf: &'a mut Buffer, _: &F) -> usize
    where
        F: Format,
    {
        let c = itoa::write(&mut buf.inner[..], *self).unwrap();
        buf.pos = 0;
        buf.end = c;
        c
    }
}

impl Sealed for u8 {}

macro_rules! impl_unsigned {
    ($type:ty) => {
        impl ToFormattedStr for $type {
            #[doc(hidden)]
            #[inline(always)]
            fn read_to_buffer<'a, F>(&self, buf: &'a mut Buffer, format: &F) -> usize
            where
                F: Format,
            {
                // Encode the separator, if any, into a byte buffer
                let mut sep_buf: [u8; 4] = unsafe { mem::uninitialized() };
                let mut sep: Option<Separator> = Separator::new(format, &mut sep_buf);

                // Bail out early if we can just use itoa
                // (i.e. if we don't have a separator)
                if sep.is_none() {
                    let c = itoa::write(&mut buf.inner[..], *self).unwrap();
                    buf.pos = 0;
                    buf.end = c;
                    return c;
                }

                // Reset our position to the end of the buffer
                buf.pos = MAX_BUF_LEN;
                buf.end = MAX_BUF_LEN;

                // Get a pointer to TABLE, which will be needed later
                let table_ptr = TABLE.as_ptr();

                // Start the main algorithm
                #[allow(trivial_numeric_casts)]
                let mut n = *self as u128;
                while n >= 10_000 {
                    let remainder = n % 10_000;
                    let table_index = ((remainder % 100) << 1) as isize;
                    utils::write_two_bytes(buf, &mut sep, table_ptr, table_index);
                    let table_index = ((remainder / 100) << 1) as isize;
                    utils::write_two_bytes(buf, &mut sep, table_ptr, table_index);
                    n /= 10_000;
                }
                let mut n = n as isize;
                while n >= 100 {
                    let table_index = (n % 100) << 1;
                    utils::write_two_bytes(buf, &mut sep, table_ptr, table_index);
                    n /= 100;
                }
                if n >= 10 {
                    let table_index = n << 1;
                    utils::write_two_bytes(buf, &mut sep, table_ptr, table_index);
                } else {
                    let table_index = n << 1;
                    utils::write_one_byte(buf, &mut sep, table_ptr, table_index + 1);
                }

                buf.end - buf.pos
            }
        }

        impl Sealed for $type {}
    };
}

impl_unsigned!(u16);
impl_unsigned!(u32);
impl_unsigned!(usize);
impl_unsigned!(u64);
impl_unsigned!(u128);