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
use core::mem;
use core::ptr;
use itoa;
use crate::constants::{MAX_BUF_LEN, TABLE};
use crate::sealed::Sealed;
use crate::utils::{self, Separator};
use crate::{Buffer, Format, ToFormattedStr};
macro_rules! impl_signed {
($type:ty, $max:expr) => {
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,
{
let mut sep_buf: [u8; 4] = unsafe { mem::uninitialized() };
let mut sep = Separator::new(format, &mut sep_buf);
let is_negative = self.is_negative();
let min = format.minus_sign().into_str();
if sep.is_none() && ((is_negative && min == "-") || !is_negative) {
let c = itoa::write(&mut buf.inner[..], *self).unwrap();
buf.pos = 0;
buf.end = c;
return c;
}
buf.pos = MAX_BUF_LEN;
buf.end = MAX_BUF_LEN;
let table_ptr = TABLE.as_ptr();
let mut n = if is_negative {
(!(*self as u128)).wrapping_add(1)
} else {
*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);
}
if is_negative {
let min_len = min.len();
buf.pos -= min_len;
let min_ptr = min.as_bytes().as_ptr();
unsafe {
ptr::copy_nonoverlapping(min_ptr, buf.as_mut_ptr().add(buf.pos), min_len)
}
}
buf.end - buf.pos
}
}
impl Sealed for $type {}
};
}
impl_signed!(i8, std::i8::MAX);
impl_signed!(i16, std::i16::MAX);
impl_signed!(i32, std::i32::MAX);
impl_signed!(isize, std::isize::MAX);
impl_signed!(i64, std::i64::MAX);
impl_signed!(i128, std::i128::MAX);