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
#![doc(html_root_url = "https://docs.rs/itoa/0.4.7")]
#![cfg_attr(not(feature = "std"), no_std)]
#![cfg_attr(feature = "cargo-clippy", allow(renamed_and_removed_lints))]
#![cfg_attr(
feature = "cargo-clippy",
allow(const_static_lifetime, transmute_ptr_to_ptr)
)]
#[cfg(feature = "i128")]
mod udiv128;
#[cfg(feature = "std")]
use std::{fmt, io, mem, ptr, slice, str};
#[cfg(not(feature = "std"))]
use core::{fmt, mem, ptr, slice, str};
#[cfg(feature = "std")]
#[inline]
pub fn write<W: io::Write, V: Integer>(mut wr: W, value: V) -> io::Result<usize> {
let mut buf = Buffer::new();
let s = buf.format(value);
match wr.write_all(s.as_bytes()) {
Ok(()) => Ok(s.len()),
Err(e) => Err(e),
}
}
#[inline]
pub fn fmt<W: fmt::Write, V: Integer>(mut wr: W, value: V) -> fmt::Result {
let mut buf = Buffer::new();
wr.write_str(buf.format(value))
}
#[derive(Copy)]
pub struct Buffer {
bytes: [u8; I128_MAX_LEN],
}
impl Default for Buffer {
#[inline]
fn default() -> Buffer {
Buffer::new()
}
}
impl Clone for Buffer {
#[inline]
fn clone(&self) -> Self {
Buffer::new()
}
}
impl Buffer {
#[inline]
#[allow(deprecated)]
pub fn new() -> Buffer {
Buffer {
bytes: unsafe { mem::uninitialized() },
}
}
pub fn format<I: Integer>(&mut self, i: I) -> &str {
i.write(self)
}
}
mod private {
pub trait Sealed {}
}
pub trait Integer: private::Sealed {
#[doc(hidden)]
fn write(self, buf: &mut Buffer) -> &str;
}
trait IntegerPrivate<B> {
fn write_to(self, buf: &mut B) -> &[u8];
}
const DEC_DIGITS_LUT: &'static [u8] = b"\
0001020304050607080910111213141516171819\
2021222324252627282930313233343536373839\
4041424344454647484950515253545556575859\
6061626364656667686970717273747576777879\
8081828384858687888990919293949596979899";
macro_rules! impl_IntegerCommon {
($max_len:expr, $t:ident) => {
impl Integer for $t {
#[inline]
fn write(self, buf: &mut Buffer) -> &str {
unsafe {
debug_assert!($max_len <= I128_MAX_LEN);
let buf = mem::transmute::<&mut [u8; I128_MAX_LEN], &mut [u8; $max_len]>(
&mut buf.bytes,
);
let bytes = self.write_to(buf);
str::from_utf8_unchecked(bytes)
}
}
}
impl private::Sealed for $t {}
};
}
macro_rules! impl_Integer {
($($max_len:expr => $t:ident),* as $conv_fn:ident) => {$(
impl_IntegerCommon!($max_len, $t);
impl IntegerPrivate<[u8; $max_len]> for $t {
#[allow(unused_comparisons)]
#[inline]
fn write_to(self, buf: &mut [u8; $max_len]) -> &[u8] {
let is_nonnegative = self >= 0;
let mut n = if is_nonnegative {
self as $conv_fn
} else {
(!(self as $conv_fn)).wrapping_add(1)
};
let mut curr = buf.len() as isize;
let buf_ptr = buf.as_mut_ptr();
let lut_ptr = DEC_DIGITS_LUT.as_ptr();
unsafe {
if mem::size_of::<$t>() >= 2 {
while n >= 10000 {
let rem = (n % 10000) as isize;
n /= 10000;
let d1 = (rem / 100) << 1;
let d2 = (rem % 100) << 1;
curr -= 4;
ptr::copy_nonoverlapping(lut_ptr.offset(d1), buf_ptr.offset(curr), 2);
ptr::copy_nonoverlapping(lut_ptr.offset(d2), buf_ptr.offset(curr + 2), 2);
}
}
let mut n = n as isize;
if n >= 100 {
let d1 = (n % 100) << 1;
n /= 100;
curr -= 2;
ptr::copy_nonoverlapping(lut_ptr.offset(d1), buf_ptr.offset(curr), 2);
}
if n < 10 {
curr -= 1;
*buf_ptr.offset(curr) = (n as u8) + b'0';
} else {
let d1 = n << 1;
curr -= 2;
ptr::copy_nonoverlapping(lut_ptr.offset(d1), buf_ptr.offset(curr), 2);
}
if !is_nonnegative {
curr -= 1;
*buf_ptr.offset(curr) = b'-';
}
}
let len = buf.len() - curr as usize;
unsafe { slice::from_raw_parts(buf_ptr.offset(curr), len) }
}
}
)*};
}
const I8_MAX_LEN: usize = 4;
const U8_MAX_LEN: usize = 3;
const I16_MAX_LEN: usize = 6;
const U16_MAX_LEN: usize = 5;
const I32_MAX_LEN: usize = 11;
const U32_MAX_LEN: usize = 10;
const I64_MAX_LEN: usize = 20;
const U64_MAX_LEN: usize = 20;
impl_Integer!(
I8_MAX_LEN => i8,
U8_MAX_LEN => u8,
I16_MAX_LEN => i16,
U16_MAX_LEN => u16,
I32_MAX_LEN => i32,
U32_MAX_LEN => u32
as u32);
impl_Integer!(I64_MAX_LEN => i64, U64_MAX_LEN => u64 as u64);
#[cfg(target_pointer_width = "16")]
impl_Integer!(I16_MAX_LEN => isize, U16_MAX_LEN => usize as u16);
#[cfg(target_pointer_width = "32")]
impl_Integer!(I32_MAX_LEN => isize, U32_MAX_LEN => usize as u32);
#[cfg(target_pointer_width = "64")]
impl_Integer!(I64_MAX_LEN => isize, U64_MAX_LEN => usize as u64);
#[cfg(all(feature = "i128"))]
macro_rules! impl_Integer128 {
($($max_len:expr => $t:ident),*) => {$(
impl_IntegerCommon!($max_len, $t);
impl IntegerPrivate<[u8; $max_len]> for $t {
#[allow(unused_comparisons)]
#[inline]
fn write_to(self, buf: &mut [u8; $max_len]) -> &[u8] {
let is_nonnegative = self >= 0;
let n = if is_nonnegative {
self as u128
} else {
(!(self as u128)).wrapping_add(1)
};
let mut curr = buf.len() as isize;
let buf_ptr = buf.as_mut_ptr();
unsafe {
let (n, rem) = udiv128::udivmod_1e19(n);
let buf1 = buf_ptr.offset(curr - U64_MAX_LEN as isize) as *mut [u8; U64_MAX_LEN];
curr -= rem.write_to(&mut *buf1).len() as isize;
if n != 0 {
let target = buf.len() as isize - 19;
ptr::write_bytes(buf_ptr.offset(target), b'0', (curr - target) as usize);
curr = target;
let (n, rem) = udiv128::udivmod_1e19(n);
let buf2 = buf_ptr.offset(curr - U64_MAX_LEN as isize) as *mut [u8; U64_MAX_LEN];
curr -= rem.write_to(&mut *buf2).len() as isize;
if n != 0 {
let target = buf.len() as isize - 38;
ptr::write_bytes(buf_ptr.offset(target), b'0', (curr - target) as usize);
curr = target;
curr -= 1;
*buf_ptr.offset(curr) = (n as u8) + b'0';
}
}
if !is_nonnegative {
curr -= 1;
*buf_ptr.offset(curr) = b'-';
}
let len = buf.len() - curr as usize;
slice::from_raw_parts(buf_ptr.offset(curr), len)
}
}
}
)*};
}
#[cfg(all(feature = "i128"))]
const U128_MAX_LEN: usize = 39;
const I128_MAX_LEN: usize = 40;
#[cfg(all(feature = "i128"))]
impl_Integer128!(I128_MAX_LEN => i128, U128_MAX_LEN => u128);