@@ -124,6 +124,7 @@ functions.
124
124
125
125
pub use self :: decoder:: { decode, DecodableFloat , Decoded , FullDecoded } ;
126
126
127
+ use super :: fmt:: { Formatted , Part } ;
127
128
use crate :: mem:: MaybeUninit ;
128
129
129
130
pub mod decoder;
@@ -170,107 +171,6 @@ pub fn round_up(d: &mut [u8]) -> Option<u8> {
170
171
}
171
172
}
172
173
173
- /// Formatted parts.
174
- #[ derive( Copy , Clone , PartialEq , Eq , Debug ) ]
175
- pub enum Part < ' a > {
176
- /// Given number of zero digits.
177
- Zero ( usize ) ,
178
- /// A literal number up to 5 digits.
179
- Num ( u16 ) ,
180
- /// A verbatim copy of given bytes.
181
- Copy ( & ' a [ u8 ] ) ,
182
- }
183
-
184
- impl < ' a > Part < ' a > {
185
- /// Returns the exact byte length of given part.
186
- pub fn len ( & self ) -> usize {
187
- match * self {
188
- Part :: Zero ( nzeroes) => nzeroes,
189
- Part :: Num ( v) => {
190
- if v < 1_000 {
191
- if v < 10 {
192
- 1
193
- } else if v < 100 {
194
- 2
195
- } else {
196
- 3
197
- }
198
- } else {
199
- if v < 10_000 { 4 } else { 5 }
200
- }
201
- }
202
- Part :: Copy ( buf) => buf. len ( ) ,
203
- }
204
- }
205
-
206
- /// Writes a part into the supplied buffer.
207
- /// Returns the number of written bytes, or `None` if the buffer is not enough.
208
- /// (It may still leave partially written bytes in the buffer; do not rely on that.)
209
- pub fn write ( & self , out : & mut [ u8 ] ) -> Option < usize > {
210
- let len = self . len ( ) ;
211
- if out. len ( ) >= len {
212
- match * self {
213
- Part :: Zero ( nzeroes) => {
214
- for c in & mut out[ ..nzeroes] {
215
- * c = b'0' ;
216
- }
217
- }
218
- Part :: Num ( mut v) => {
219
- for c in out[ ..len] . iter_mut ( ) . rev ( ) {
220
- * c = b'0' + ( v % 10 ) as u8 ;
221
- v /= 10 ;
222
- }
223
- }
224
- Part :: Copy ( buf) => {
225
- out[ ..buf. len ( ) ] . copy_from_slice ( buf) ;
226
- }
227
- }
228
- Some ( len)
229
- } else {
230
- None
231
- }
232
- }
233
- }
234
-
235
- /// Formatted result containing one or more parts.
236
- /// This can be written to the byte buffer or converted to the allocated string.
237
- #[ allow( missing_debug_implementations) ]
238
- #[ derive( Clone ) ]
239
- pub struct Formatted < ' a > {
240
- /// A byte slice representing a sign, either `""`, `"-"` or `"+"`.
241
- pub sign : & ' static str ,
242
- /// Formatted parts to be rendered after a sign and optional zero padding.
243
- pub parts : & ' a [ Part < ' a > ] ,
244
- }
245
-
246
- impl < ' a > Formatted < ' a > {
247
- /// Returns the exact byte length of combined formatted result.
248
- pub fn len ( & self ) -> usize {
249
- let mut len = self . sign . len ( ) ;
250
- for part in self . parts {
251
- len += part. len ( ) ;
252
- }
253
- len
254
- }
255
-
256
- /// Writes all formatted parts into the supplied buffer.
257
- /// Returns the number of written bytes, or `None` if the buffer is not enough.
258
- /// (It may still leave partially written bytes in the buffer; do not rely on that.)
259
- pub fn write ( & self , out : & mut [ u8 ] ) -> Option < usize > {
260
- if out. len ( ) < self . sign . len ( ) {
261
- return None ;
262
- }
263
- out[ ..self . sign . len ( ) ] . copy_from_slice ( self . sign . as_bytes ( ) ) ;
264
-
265
- let mut written = self . sign . len ( ) ;
266
- for part in self . parts {
267
- let len = part. write ( & mut out[ written..] ) ?;
268
- written += len;
269
- }
270
- Some ( written)
271
- }
272
- }
273
-
274
174
/// Formats given decimal digits `0.<...buf...> * 10^exp` into the decimal form
275
175
/// with at least given number of fractional digits. The result is stored to
276
176
/// the supplied parts array and a slice of written parts is returned.
0 commit comments