/src/rust-lexical/lexical-write-integer/src/write.rs
Line | Count | Source |
1 | | //! Shared trait and methods for writing integers. |
2 | | |
3 | | #![doc(hidden)] |
4 | | |
5 | | use lexical_util::format; |
6 | | |
7 | | /// Select the back-end. |
8 | | #[cfg(feature = "compact")] |
9 | | use crate::compact::Compact; |
10 | | #[cfg(not(feature = "compact"))] |
11 | | use crate::decimal::Decimal; |
12 | | #[cfg(all(not(feature = "compact"), feature = "power-of-two"))] |
13 | | use crate::radix::Radix; |
14 | | |
15 | | /// Define the implementation to write significant digits. |
16 | | macro_rules! write_mantissa { |
17 | | ($($t:tt)+) => { |
18 | | /// Internal implementation to write significant digits for float writers. |
19 | | #[doc(hidden)] |
20 | | #[inline(always)] |
21 | 413 | fn write_mantissa<const FORMAT: u128>(self, buffer: &mut [u8]) -> usize { |
22 | 413 | self.write_integer::<FORMAT, { format::RADIX }, { format::RADIX_SHIFT }>(buffer) |
23 | 413 | } <usize as lexical_write_integer::write::WriteInteger>::write_mantissa::<0xa0000000000000000000000000c> Line | Count | Source | 21 | 122 | fn write_mantissa<const FORMAT: u128>(self, buffer: &mut [u8]) -> usize { | 22 | 122 | self.write_integer::<FORMAT, { format::RADIX }, { format::RADIX_SHIFT }>(buffer) | 23 | 122 | } |
<u8 as lexical_write_integer::write::WriteInteger>::write_mantissa::<0xa0000000000000000000000000c> Line | Count | Source | 21 | 39 | fn write_mantissa<const FORMAT: u128>(self, buffer: &mut [u8]) -> usize { | 22 | 39 | self.write_integer::<FORMAT, { format::RADIX }, { format::RADIX_SHIFT }>(buffer) | 23 | 39 | } |
<u64 as lexical_write_integer::write::WriteInteger>::write_mantissa::<0xa0000000000000000000000000c> Line | Count | Source | 21 | 36 | fn write_mantissa<const FORMAT: u128>(self, buffer: &mut [u8]) -> usize { | 22 | 36 | self.write_integer::<FORMAT, { format::RADIX }, { format::RADIX_SHIFT }>(buffer) | 23 | 36 | } |
<u128 as lexical_write_integer::write::WriteInteger>::write_mantissa::<0xa0000000000000000000000000c> Line | Count | Source | 21 | 143 | fn write_mantissa<const FORMAT: u128>(self, buffer: &mut [u8]) -> usize { | 22 | 143 | self.write_integer::<FORMAT, { format::RADIX }, { format::RADIX_SHIFT }>(buffer) | 23 | 143 | } |
<u16 as lexical_write_integer::write::WriteInteger>::write_mantissa::<0xa0000000000000000000000000c> Line | Count | Source | 21 | 35 | fn write_mantissa<const FORMAT: u128>(self, buffer: &mut [u8]) -> usize { | 22 | 35 | self.write_integer::<FORMAT, { format::RADIX }, { format::RADIX_SHIFT }>(buffer) | 23 | 35 | } |
<u32 as lexical_write_integer::write::WriteInteger>::write_mantissa::<0xa0000000000000000000000000c> Line | Count | Source | 21 | 38 | fn write_mantissa<const FORMAT: u128>(self, buffer: &mut [u8]) -> usize { | 22 | 38 | self.write_integer::<FORMAT, { format::RADIX }, { format::RADIX_SHIFT }>(buffer) | 23 | 38 | } |
|
24 | | |
25 | | /// Internal implementation to write significant digits for float writers. |
26 | | #[doc(hidden)] |
27 | | #[inline(always)] |
28 | 322 | fn write_mantissa_signed<const FORMAT: u128>(self, buffer: &mut [u8]) -> usize { |
29 | 322 | self.write_integer_signed::<FORMAT, { format::RADIX }, { format::RADIX_SHIFT }>(buffer) |
30 | 322 | } <u16 as lexical_write_integer::write::WriteInteger>::write_mantissa_signed::<0xa0000000000000000000000000c> Line | Count | Source | 28 | 36 | fn write_mantissa_signed<const FORMAT: u128>(self, buffer: &mut [u8]) -> usize { | 29 | 36 | self.write_integer_signed::<FORMAT, { format::RADIX }, { format::RADIX_SHIFT }>(buffer) | 30 | 36 | } |
<u128 as lexical_write_integer::write::WriteInteger>::write_mantissa_signed::<0xa0000000000000000000000000c> Line | Count | Source | 28 | 141 | fn write_mantissa_signed<const FORMAT: u128>(self, buffer: &mut [u8]) -> usize { | 29 | 141 | self.write_integer_signed::<FORMAT, { format::RADIX }, { format::RADIX_SHIFT }>(buffer) | 30 | 141 | } |
<u8 as lexical_write_integer::write::WriteInteger>::write_mantissa_signed::<0xa0000000000000000000000000c> Line | Count | Source | 28 | 38 | fn write_mantissa_signed<const FORMAT: u128>(self, buffer: &mut [u8]) -> usize { | 29 | 38 | self.write_integer_signed::<FORMAT, { format::RADIX }, { format::RADIX_SHIFT }>(buffer) | 30 | 38 | } |
<u64 as lexical_write_integer::write::WriteInteger>::write_mantissa_signed::<0xa0000000000000000000000000c> Line | Count | Source | 28 | 36 | fn write_mantissa_signed<const FORMAT: u128>(self, buffer: &mut [u8]) -> usize { | 29 | 36 | self.write_integer_signed::<FORMAT, { format::RADIX }, { format::RADIX_SHIFT }>(buffer) | 30 | 36 | } |
<u64 as lexical_write_integer::write::WriteInteger>::write_mantissa_signed::<0xa0000000000000000000000000c> Line | Count | Source | 28 | 35 | fn write_mantissa_signed<const FORMAT: u128>(self, buffer: &mut [u8]) -> usize { | 29 | 35 | self.write_integer_signed::<FORMAT, { format::RADIX }, { format::RADIX_SHIFT }>(buffer) | 30 | 35 | } |
<u32 as lexical_write_integer::write::WriteInteger>::write_mantissa_signed::<0xa0000000000000000000000000c> Line | Count | Source | 28 | 36 | fn write_mantissa_signed<const FORMAT: u128>(self, buffer: &mut [u8]) -> usize { | 29 | 36 | self.write_integer_signed::<FORMAT, { format::RADIX }, { format::RADIX_SHIFT }>(buffer) | 30 | 36 | } |
|
31 | | }; |
32 | | } |
33 | | |
34 | | /// Define the implementation to write exponent digits. |
35 | | macro_rules! write_exponent { |
36 | | ($($t:tt)+) => ( |
37 | | // NOTE: This should always be signed, but for backwards compatibility as |
38 | | // a precaution we keep the original just in case someone uses the private API. |
39 | | |
40 | | /// Internal implementation to write exponent digits for float writers. |
41 | | // NOTE: This is not part of the public API. |
42 | | #[doc(hidden)] |
43 | | #[inline(always)] |
44 | | #[deprecated = "use `write_exponent_signed`, since exponents are always signed."] |
45 | | fn write_exponent<const FORMAT: u128>(self, buffer: &mut [u8]) -> usize |
46 | | { |
47 | | self.write_integer::<FORMAT, { format::EXPONENT_RADIX }, { format::EXPONENT_RADIX_SHIFT }>(buffer) |
48 | | } |
49 | | |
50 | | /// Internal implementation to write exponent digits for float writers. |
51 | | #[doc(hidden)] |
52 | | #[inline(always)] |
53 | 995 | fn write_exponent_signed<const FORMAT: u128>(self, buffer: &mut [u8]) -> usize |
54 | | { |
55 | 995 | self.write_integer_signed::<FORMAT, { format::EXPONENT_RADIX }, { format::EXPONENT_RADIX_SHIFT }>(buffer) |
56 | 995 | } <u32 as lexical_write_integer::write::WriteInteger>::write_exponent_signed::<0xa0000000000000000000000000c> Line | Count | Source | 53 | 214 | fn write_exponent_signed<const FORMAT: u128>(self, buffer: &mut [u8]) -> usize | 54 | | { | 55 | 214 | self.write_integer_signed::<FORMAT, { format::EXPONENT_RADIX }, { format::EXPONENT_RADIX_SHIFT }>(buffer) | 56 | 214 | } |
<u32 as lexical_write_integer::write::WriteInteger>::write_exponent_signed::<0xa0000000000000000000000000c> Line | Count | Source | 53 | 781 | fn write_exponent_signed<const FORMAT: u128>(self, buffer: &mut [u8]) -> usize | 54 | | { | 55 | 781 | self.write_integer_signed::<FORMAT, { format::EXPONENT_RADIX }, { format::EXPONENT_RADIX_SHIFT }>(buffer) | 56 | 781 | } |
|
57 | | ) |
58 | | } |
59 | | |
60 | | /// Write integer trait, implemented in terms of the compact back-end. |
61 | | #[cfg(feature = "compact")] |
62 | | pub trait WriteInteger: Compact { |
63 | | /// Forward write integer parameters to an unoptimized backend. |
64 | | /// |
65 | | /// # Preconditions |
66 | | /// |
67 | | /// `self` must be non-negative and unsigned. |
68 | | /// |
69 | | /// [`FORMATTED_SIZE`]: lexical_util::constants::FormattedSize::FORMATTED_SIZE |
70 | | /// [`FORMATTED_SIZE_DECIMAL`]: lexical_util::constants::FormattedSize::FORMATTED_SIZE_DECIMAL |
71 | | fn write_integer<const FORMAT: u128, const MASK: u128, const SHIFT: i32>( |
72 | | self, |
73 | | buffer: &mut [u8], |
74 | | ) -> usize { |
75 | | let radix = format::radix_from_flags(FORMAT, MASK, SHIFT); |
76 | | self.compact(radix, buffer) |
77 | | } |
78 | | |
79 | | /// Forward write integer parameters to an optimized backend. |
80 | | /// |
81 | | /// This requires a type that was previously signed. |
82 | | /// |
83 | | /// # Preconditions |
84 | | /// |
85 | | /// `self` must be non-negative but is `>= 0` and `<= Signed::MAX`. |
86 | | /// |
87 | | /// [`FORMATTED_SIZE_DECIMAL`]: lexical_util::constants::FormattedSize::FORMATTED_SIZE_DECIMAL |
88 | | #[inline(always)] |
89 | | fn write_integer_signed<const FORMAT: u128, const MASK: u128, const SHIFT: i32>( |
90 | | self, |
91 | | buffer: &mut [u8], |
92 | | ) -> usize { |
93 | | self.write_integer::<FORMAT, MASK, SHIFT>(buffer) |
94 | | } |
95 | | |
96 | | write_mantissa!(Compact); |
97 | | write_exponent!(Compact); |
98 | | } |
99 | | |
100 | | /// Write integer trait, implemented in terms of the optimized, decimal |
101 | | /// back-end. |
102 | | #[cfg(all(not(feature = "compact"), not(feature = "power-of-two")))] |
103 | | pub trait WriteInteger: Decimal { |
104 | | /// Forward write integer parameters to an optimized backend. |
105 | | /// |
106 | | /// # Preconditions |
107 | | /// |
108 | | /// `self` must be non-negative and unsigned. |
109 | | /// |
110 | | /// [`FORMATTED_SIZE_DECIMAL`]: lexical_util::constants::FormattedSize::FORMATTED_SIZE_DECIMAL |
111 | | #[inline(always)] |
112 | 413 | fn write_integer<const __: u128, const ___: u128, const ____: i32>( |
113 | 413 | self, |
114 | 413 | buffer: &mut [u8], |
115 | 413 | ) -> usize { |
116 | 413 | self.decimal(buffer) |
117 | 413 | } <usize as lexical_write_integer::write::WriteInteger>::write_integer::<0xa0000000000000000000000000c, 0xff00000000000000000000000000, 104> Line | Count | Source | 112 | 122 | fn write_integer<const __: u128, const ___: u128, const ____: i32>( | 113 | 122 | self, | 114 | 122 | buffer: &mut [u8], | 115 | 122 | ) -> usize { | 116 | 122 | self.decimal(buffer) | 117 | 122 | } |
<u8 as lexical_write_integer::write::WriteInteger>::write_integer::<0xa0000000000000000000000000c, 0xff00000000000000000000000000, 104> Line | Count | Source | 112 | 39 | fn write_integer<const __: u128, const ___: u128, const ____: i32>( | 113 | 39 | self, | 114 | 39 | buffer: &mut [u8], | 115 | 39 | ) -> usize { | 116 | 39 | self.decimal(buffer) | 117 | 39 | } |
<u64 as lexical_write_integer::write::WriteInteger>::write_integer::<0xa0000000000000000000000000c, 0xff00000000000000000000000000, 104> Line | Count | Source | 112 | 36 | fn write_integer<const __: u128, const ___: u128, const ____: i32>( | 113 | 36 | self, | 114 | 36 | buffer: &mut [u8], | 115 | 36 | ) -> usize { | 116 | 36 | self.decimal(buffer) | 117 | 36 | } |
<u128 as lexical_write_integer::write::WriteInteger>::write_integer::<0xa0000000000000000000000000c, 0xff00000000000000000000000000, 104> Line | Count | Source | 112 | 143 | fn write_integer<const __: u128, const ___: u128, const ____: i32>( | 113 | 143 | self, | 114 | 143 | buffer: &mut [u8], | 115 | 143 | ) -> usize { | 116 | 143 | self.decimal(buffer) | 117 | 143 | } |
<u16 as lexical_write_integer::write::WriteInteger>::write_integer::<0xa0000000000000000000000000c, 0xff00000000000000000000000000, 104> Line | Count | Source | 112 | 35 | fn write_integer<const __: u128, const ___: u128, const ____: i32>( | 113 | 35 | self, | 114 | 35 | buffer: &mut [u8], | 115 | 35 | ) -> usize { | 116 | 35 | self.decimal(buffer) | 117 | 35 | } |
<u32 as lexical_write_integer::write::WriteInteger>::write_integer::<0xa0000000000000000000000000c, 0xff00000000000000000000000000, 104> Line | Count | Source | 112 | 38 | fn write_integer<const __: u128, const ___: u128, const ____: i32>( | 113 | 38 | self, | 114 | 38 | buffer: &mut [u8], | 115 | 38 | ) -> usize { | 116 | 38 | self.decimal(buffer) | 117 | 38 | } |
|
118 | | |
119 | | /// Forward write integer parameters to an optimized backend. |
120 | | /// |
121 | | /// This requires a type that was previously signed. |
122 | | /// |
123 | | /// # Preconditions |
124 | | /// |
125 | | /// `self` must be non-negative but is `>= 0` and `<= Signed::MAX`. |
126 | | /// |
127 | | /// [`FORMATTED_SIZE_DECIMAL`]: lexical_util::constants::FormattedSize::FORMATTED_SIZE_DECIMAL |
128 | | #[inline(always)] |
129 | 1.31k | fn write_integer_signed<const __: u128, const ___: u128, const ____: i32>( |
130 | 1.31k | self, |
131 | 1.31k | buffer: &mut [u8], |
132 | 1.31k | ) -> usize { |
133 | 1.31k | self.decimal_signed(buffer) |
134 | 1.31k | } <u32 as lexical_write_integer::write::WriteInteger>::write_integer_signed::<0xa0000000000000000000000000c, 0xff000000000000000000000000000000, 120> Line | Count | Source | 129 | 214 | fn write_integer_signed<const __: u128, const ___: u128, const ____: i32>( | 130 | 214 | self, | 131 | 214 | buffer: &mut [u8], | 132 | 214 | ) -> usize { | 133 | 214 | self.decimal_signed(buffer) | 134 | 214 | } |
<u16 as lexical_write_integer::write::WriteInteger>::write_integer_signed::<0xa0000000000000000000000000c, 0xff00000000000000000000000000, 104> Line | Count | Source | 129 | 36 | fn write_integer_signed<const __: u128, const ___: u128, const ____: i32>( | 130 | 36 | self, | 131 | 36 | buffer: &mut [u8], | 132 | 36 | ) -> usize { | 133 | 36 | self.decimal_signed(buffer) | 134 | 36 | } |
<u128 as lexical_write_integer::write::WriteInteger>::write_integer_signed::<0xa0000000000000000000000000c, 0xff00000000000000000000000000, 104> Line | Count | Source | 129 | 141 | fn write_integer_signed<const __: u128, const ___: u128, const ____: i32>( | 130 | 141 | self, | 131 | 141 | buffer: &mut [u8], | 132 | 141 | ) -> usize { | 133 | 141 | self.decimal_signed(buffer) | 134 | 141 | } |
<u32 as lexical_write_integer::write::WriteInteger>::write_integer_signed::<0xa0000000000000000000000000c, 0xff000000000000000000000000000000, 120> Line | Count | Source | 129 | 781 | fn write_integer_signed<const __: u128, const ___: u128, const ____: i32>( | 130 | 781 | self, | 131 | 781 | buffer: &mut [u8], | 132 | 781 | ) -> usize { | 133 | 781 | self.decimal_signed(buffer) | 134 | 781 | } |
<u8 as lexical_write_integer::write::WriteInteger>::write_integer_signed::<0xa0000000000000000000000000c, 0xff00000000000000000000000000, 104> Line | Count | Source | 129 | 38 | fn write_integer_signed<const __: u128, const ___: u128, const ____: i32>( | 130 | 38 | self, | 131 | 38 | buffer: &mut [u8], | 132 | 38 | ) -> usize { | 133 | 38 | self.decimal_signed(buffer) | 134 | 38 | } |
<u64 as lexical_write_integer::write::WriteInteger>::write_integer_signed::<0xa0000000000000000000000000c, 0xff00000000000000000000000000, 104> Line | Count | Source | 129 | 36 | fn write_integer_signed<const __: u128, const ___: u128, const ____: i32>( | 130 | 36 | self, | 131 | 36 | buffer: &mut [u8], | 132 | 36 | ) -> usize { | 133 | 36 | self.decimal_signed(buffer) | 134 | 36 | } |
<u64 as lexical_write_integer::write::WriteInteger>::write_integer_signed::<0xa0000000000000000000000000c, 0xff00000000000000000000000000, 104> Line | Count | Source | 129 | 35 | fn write_integer_signed<const __: u128, const ___: u128, const ____: i32>( | 130 | 35 | self, | 131 | 35 | buffer: &mut [u8], | 132 | 35 | ) -> usize { | 133 | 35 | self.decimal_signed(buffer) | 134 | 35 | } |
<u32 as lexical_write_integer::write::WriteInteger>::write_integer_signed::<0xa0000000000000000000000000c, 0xff00000000000000000000000000, 104> Line | Count | Source | 129 | 36 | fn write_integer_signed<const __: u128, const ___: u128, const ____: i32>( | 130 | 36 | self, | 131 | 36 | buffer: &mut [u8], | 132 | 36 | ) -> usize { | 133 | 36 | self.decimal_signed(buffer) | 134 | 36 | } |
|
135 | | |
136 | | write_mantissa!(Decimal); |
137 | | write_exponent!(Decimal); |
138 | | } |
139 | | |
140 | | /// Write integer trait, implemented in terms of the optimized, decimal or radix |
141 | | /// back-end. |
142 | | #[cfg(all(not(feature = "compact"), feature = "power-of-two"))] |
143 | | pub trait WriteInteger: Decimal + Radix { |
144 | | /// Forward write integer parameters to an optimized backend. |
145 | | /// |
146 | | /// # Preconditions |
147 | | /// |
148 | | /// `self` must be non-negative and unsigned. |
149 | | /// |
150 | | /// [`FORMATTED_SIZE`]: lexical_util::constants::FormattedSize::FORMATTED_SIZE |
151 | | /// [`FORMATTED_SIZE_DECIMAL`]: lexical_util::constants::FormattedSize::FORMATTED_SIZE_DECIMAL |
152 | | #[inline(always)] |
153 | | fn write_integer<const FORMAT: u128, const MASK: u128, const SHIFT: i32>( |
154 | | self, |
155 | | buffer: &mut [u8], |
156 | | ) -> usize { |
157 | | if format::radix_from_flags(FORMAT, MASK, SHIFT) == 10 { |
158 | | self.decimal(buffer) |
159 | | } else { |
160 | | self.radix::<FORMAT, MASK, SHIFT>(buffer) |
161 | | } |
162 | | } |
163 | | |
164 | | /// Forward write integer parameters to an optimized backend. |
165 | | /// |
166 | | /// This requires a type that was previously signed. |
167 | | /// |
168 | | /// # Preconditions |
169 | | /// |
170 | | /// `self` must be non-negative but is `>= 0` and `<= Signed::MAX`. |
171 | | /// |
172 | | /// [`FORMATTED_SIZE_DECIMAL`]: lexical_util::constants::FormattedSize::FORMATTED_SIZE_DECIMAL |
173 | | #[inline(always)] |
174 | | fn write_integer_signed<const FORMAT: u128, const MASK: u128, const SHIFT: i32>( |
175 | | self, |
176 | | buffer: &mut [u8], |
177 | | ) -> usize { |
178 | | if format::radix_from_flags(FORMAT, MASK, SHIFT) == 10 { |
179 | | self.decimal_signed(buffer) |
180 | | } else { |
181 | | self.radix::<FORMAT, MASK, SHIFT>(buffer) |
182 | | } |
183 | | } |
184 | | |
185 | | write_mantissa!(Decimal + Radix); |
186 | | write_exponent!(Decimal + Radix); |
187 | | } |
188 | | |
189 | | macro_rules! write_integer_impl { |
190 | | ($($t:ty)*) => ($( |
191 | | impl WriteInteger for $t {} |
192 | | )*) |
193 | | } |
194 | | |
195 | | write_integer_impl! { u8 u16 u32 u64 u128 usize } |