|
| 1 | +use crate::mem::MaybeUninit; |
| 2 | + |
| 3 | +/// A minimal buffer implementation containing elements of type |
| 4 | +/// `MaybeUninit::<u8>`. |
| 5 | +pub struct NumBuffer<const BUF_SIZE: usize> { |
| 6 | + pub contents: [MaybeUninit::<u8>; BUF_SIZE] |
| 7 | +} |
| 8 | + |
| 9 | +impl<const BUF_SIZE: usize> NumBuffer<BUF_SIZE> { |
| 10 | + fn new() -> Self { |
| 11 | + NumBuffer { |
| 12 | + contents: [MaybeUninit::<u8>::uninit(); BUF_SIZE] |
| 13 | + } |
| 14 | + } |
| 15 | +} |
| 16 | + |
| 17 | +macro_rules! int_impl_format_into { |
| 18 | + ($($T:ident)*) => { |
| 19 | + $( |
| 20 | + #[unstable(feature = "int_format_into", issue = "138215")] |
| 21 | + impl $T { |
| 22 | + /// Allows users to write an integer (in signed decimal format) into a variable `buf` of |
| 23 | + /// type [`NumBuffer`] that is passed by the caller by mutable reference. |
| 24 | + /// |
| 25 | + /// This function panics if `buf` does not have enough size to store |
| 26 | + /// the signed decimal version of the number. |
| 27 | + /// |
| 28 | + /// # Examples |
| 29 | + /// ``` |
| 30 | + /// #![feature(int_format_into)] |
| 31 | + #[doc = concat!("let n = 32", stringify!($T), ";")] |
| 32 | + /// let mut buf = NumBuffer::<3>::new(); |
| 33 | + /// |
| 34 | + /// assert_eq!(n.format_into(&mut buf), "32"); |
| 35 | + /// ``` |
| 36 | + /// |
| 37 | + fn format_into<const BUF_SIZE: usize>(self, buf: &mut crate::num::NumBuffer<BUF_SIZE>) -> &str { |
| 38 | + // 2 digit decimal look up table |
| 39 | + const DEC_DIGITS_LUT: &[u8; 200] = b"\ |
| 40 | + 0001020304050607080910111213141516171819\ |
| 41 | + 2021222324252627282930313233343536373839\ |
| 42 | + 4041424344454647484950515253545556575859\ |
| 43 | + 6061626364656667686970717273747576777879\ |
| 44 | + 8081828384858687888990919293949596979899"; |
| 45 | + |
| 46 | + const NEGATIVE_SIGN: &[u8; 1] = b"-"; |
| 47 | + |
| 48 | + // counting space for negative sign too, if `self` is negative |
| 49 | + let sign_offset = if self < 0 {1} else {0}; |
| 50 | + let decimal_string_size: usize = self.ilog(10) as usize + 1 + sign_offset; |
| 51 | + |
| 52 | + // `buf` must have minimum size to store the decimal string version. |
| 53 | + // BUF_SIZE is the size of the buffer. |
| 54 | + if BUF_SIZE < decimal_string_size { |
| 55 | + panic!("Not enough buffer size to format into!"); |
| 56 | + } |
| 57 | + |
| 58 | + // Count the number of bytes in `buf` that are not initialized. |
| 59 | + let mut offset = BUF_SIZE; |
| 60 | + // Consume the least-significant decimals from a working copy. |
| 61 | + let mut remain = self; |
| 62 | + |
| 63 | + // Format per four digits from the lookup table. |
| 64 | + // Four digits need a 16-bit $unsigned or wider. |
| 65 | + while size_of::<Self>() > 1 && remain > 999.try_into().expect("branch is not hit for types that cannot fit 999 (u8)") { |
| 66 | + // SAFETY: All of the decimals fit in buf, since it now is size-checked |
| 67 | + // and the while condition ensures at least 4 more decimals. |
| 68 | + unsafe { core::hint::assert_unchecked(offset >= 4) } |
| 69 | + // SAFETY: The offset counts down from its initial value BUF_SIZE |
| 70 | + // without underflow due to the previous precondition. |
| 71 | + unsafe { core::hint::assert_unchecked(offset <= BUF_SIZE) } |
| 72 | + offset -= 4; |
| 73 | + |
| 74 | + // pull two pairs |
| 75 | + let scale: Self = 1_00_00.try_into().expect("branch is not hit for types that cannot fit 1E4 (u8)"); |
| 76 | + let quad = remain % scale; |
| 77 | + remain /= scale; |
| 78 | + let pair1 = (quad / 100) as usize; |
| 79 | + let pair2 = (quad % 100) as usize; |
| 80 | + buf.contents[offset + 0].write(DEC_DIGITS_LUT[pair1 * 2 + 0]); |
| 81 | + buf.contents[offset + 1].write(DEC_DIGITS_LUT[pair1 * 2 + 1]); |
| 82 | + buf.contents[offset + 2].write(DEC_DIGITS_LUT[pair2 * 2 + 0]); |
| 83 | + buf.contents[offset + 3].write(DEC_DIGITS_LUT[pair2 * 2 + 1]); |
| 84 | + } |
| 85 | + |
| 86 | + // Format per two digits from the lookup table. |
| 87 | + if remain > 9 { |
| 88 | + // SAFETY: All of the decimals fit in buf, since it now is size-checked |
| 89 | + // and the while condition ensures at least 2 more decimals. |
| 90 | + unsafe { core::hint::assert_unchecked(offset >= 2) } |
| 91 | + // SAFETY: The offset counts down from its initial value BUF_SIZE |
| 92 | + // without underflow due to the previous precondition. |
| 93 | + unsafe { core::hint::assert_unchecked(offset <= BUF_SIZE) } |
| 94 | + offset -= 2; |
| 95 | + |
| 96 | + let pair = (remain % 100) as usize; |
| 97 | + remain /= 100; |
| 98 | + buf.contents[offset + 0].write(DEC_DIGITS_LUT[pair * 2 + 0]); |
| 99 | + buf.contents[offset + 1].write(DEC_DIGITS_LUT[pair * 2 + 1]); |
| 100 | + } |
| 101 | + |
| 102 | + // Format the last remaining digit, if any. |
| 103 | + if remain != 0 || self == 0 { |
| 104 | + // SAFETY: All of the decimals fit in buf, since it now is size-checked |
| 105 | + // and the if condition ensures (at least) 1 more decimals. |
| 106 | + unsafe { core::hint::assert_unchecked(offset >= 1) } |
| 107 | + // SAFETY: The offset counts down from its initial value BUF_SIZE |
| 108 | + // without underflow due to the previous precondition. |
| 109 | + unsafe { core::hint::assert_unchecked(offset <= BUF_SIZE) } |
| 110 | + offset -= 1; |
| 111 | + |
| 112 | + // Either the compiler sees that remain < 10, or it prevents |
| 113 | + // a boundary check up next. |
| 114 | + let last = (remain & 15) as usize; |
| 115 | + buf.contents[offset].write(DEC_DIGITS_LUT[last * 2 + 1]); |
| 116 | + // not used: remain = 0; |
| 117 | + } |
| 118 | + |
| 119 | + if self < 0 { |
| 120 | + // SAFETY: All of the decimals (with the sign) fit in buf, since it now is size-checked |
| 121 | + // and the if condition ensures (at least) that the sign can be added. |
| 122 | + unsafe { core::hint::assert_unchecked(offset >= 1) } |
| 123 | + |
| 124 | + // SAFETY: The offset counts down from its initial value BUF_SIZE |
| 125 | + // without underflow due to the previous precondition. |
| 126 | + unsafe { core::hint::assert_unchecked(offset <= BUF_SIZE) } |
| 127 | + |
| 128 | + // Setting sign for the negative number |
| 129 | + offset -= 1; |
| 130 | + buf.contents[offset].write(NEGATIVE_SIGN[0]); |
| 131 | + } |
| 132 | + |
| 133 | + // SAFETY: All buf content since offset is set. |
| 134 | + let written = unsafe { buf.contents.get_unchecked(offset..) }; |
| 135 | + |
| 136 | + // SAFETY: Writes use ASCII from the lookup table |
| 137 | + // (and `NEGATIVE_SIGN` in case of negative numbers) exclusively. |
| 138 | + let as_str = unsafe { |
| 139 | + str::from_utf8_unchecked(crate::slice::from_raw_parts( |
| 140 | + crate::mem::MaybeUninit::slice_as_ptr(written), |
| 141 | + written.len(), |
| 142 | + )) |
| 143 | + }; |
| 144 | + as_str |
| 145 | + } |
| 146 | + } |
| 147 | + )* |
| 148 | + }; |
| 149 | +} |
| 150 | + |
| 151 | +int_impl_format_into! { i8 i16 i32 i64 i128 isize } |
| 152 | +int_impl_format_into! { u8 u16 u32 u64 u128 usize } |
0 commit comments