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
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
use crate::{parse, Align, Base, Builder, Dynamic, Numeric, Sign};
use iterext::prelude::*;
use std::{any::type_name, collections::VecDeque, str::FromStr};

#[derive(Debug, thiserror::Error, PartialEq, Eq, Clone)]
pub enum Error {
    #[error("Zero formatter is only compatible with Align::Right or Align::Decimal")]
    IncompatibleAlignment,
    #[error("{0:?} formatting not implemented for {1}")]
    NotImplemented(Base, &'static str),
}

/// Formatter for numbers.
#[derive(Clone, PartialEq, Eq, Debug, Default)]
pub struct NumFmt {
    pub(crate) fill: Option<char>,
    pub(crate) align: Align,
    pub(crate) sign: Sign,
    pub(crate) hash: bool,
    pub(crate) zero: bool,
    pub(crate) width: usize,
    pub(crate) precision: Option<usize>,
    pub(crate) base: Base,
    pub(crate) separator: Option<char>,
    pub(crate) spacing: Option<usize>,
    pub(crate) decimal_separator: Option<char>,
}

impl NumFmt {
    /// Create a [`Builder`] to customize the parameters of a `NumFmt`.
    pub fn builder() -> Builder {
        Builder::default()
    }

    /// Parse a `NumFmt` instance from a format string.
    ///
    /// See crate-level documentation for the grammar.
    pub fn from_str(s: &str) -> Result<Self, parse::Error> {
        parse::parse(s)
    }

    #[inline]
    fn width_desired(&self, dynamic: Dynamic) -> usize {
        let mut width_desired = self.width_with(dynamic);
        if self.hash() {
            width_desired = width_desired.saturating_sub(2);
        }
        if width_desired == 0 {
            width_desired = 1;
        }

        width_desired
    }

    /// normalize a digit iterator
    ///
    /// - ensure that the iterator returns, bare minimum, a single char (default 0)
    /// - pad it to the desired width
    /// - space it out to the desired spacing
    fn normalize(&self, digits: impl Iterator<Item = char>, dynamic: Dynamic) -> VecDeque<char> {
        let pad_to = if self.zero() {
            self.width_desired(dynamic)
        } else {
            1
        };

        let pad_char = if self.zero() { '0' } else { self.fill() };

        let mut digits = digits.peekable();
        let mut digits: Box<dyn Iterator<Item = char>> = if digits.peek().is_some() {
            Box::new(digits)
        } else {
            Box::new(std::iter::once('0'))
        };

        digits = Box::new(digits.pad(pad_char, pad_to));

        if let Some((separator, spacing)) = self.separator_and_spacing_with(dynamic) {
            digits.separate(separator, spacing)
        } else {
            digits.collect()
        }
    }

    /// Format the provided number according to this configuration.
    ///
    /// Will return `None` in the event that the configured format is incompatible with
    /// the number provided. This is most often the case when the number is not an
    /// integer but an integer format such as `b`, `o`, or `x` is configured.
    pub fn fmt<N: Numeric>(&self, number: N) -> Result<String, Error> {
        self.fmt_with(number, Dynamic::default())
    }

    /// Format the provided number according to this configuration and dynamic parameters.
    ///
    /// Note that dynamic parameters always override the formatter's parameters:
    ///
    /// ```rust
    /// # use num_runtime_fmt::{NumFmt, Dynamic};
    /// let fmt = NumFmt::from_str("#04x_2").unwrap();
    /// assert_eq!(fmt.fmt(0).unwrap(), "0x00");
    /// assert_eq!(fmt.fmt_with(0, Dynamic::width(7)).unwrap(), "0x00_00");
    /// ```
    ///
    /// Will return `None` in the event that the configured format is incompatible with
    /// the number provided. This is most often the case when the number is not an
    /// integer but an integer format such as `b`, `o`, or `x` is configured.
    pub fn fmt_with<N: Numeric>(&self, number: N, dynamic: Dynamic) -> Result<String, Error> {
        if self.zero() && !(self.align() == Align::Right || self.align() == Align::Decimal) {
            return Err(Error::IncompatibleAlignment);
        }
        let negative = number.is_negative() && self.base() == Base::Decimal;
        let decimal_separator = self.decimal_separator();

        // if the separator is set, returns true when it matches the provided char
        // otherwise, always false
        let matches_separator = |ch: char| {
            self.separator_and_spacing_with(dynamic)
                .map(|(separator, _)| separator == ch)
                .unwrap_or_default()
        };

        // core formatting: construct a reversed queue of digits, with separator and decimal
        // decimal is the index of the decimal point
        let (mut digits, decimal_pos): (VecDeque<_>, Option<usize>) = match self.base() {
            Base::Binary => (
                self.normalize(
                    number
                        .binary()
                        .ok_or_else(|| Error::NotImplemented(self.base(), type_name::<N>()))?,
                    dynamic,
                ),
                None,
            ),
            Base::Octal => (
                self.normalize(
                    number
                        .octal()
                        .ok_or_else(|| Error::NotImplemented(self.base(), type_name::<N>()))?,
                    dynamic,
                ),
                None,
            ),
            Base::Decimal => {
                let (left, right) = number.decimal();
                let mut dq = self.normalize(left, dynamic);
                let decimal = dq.len();
                let past_decimal: Option<Box<dyn Iterator<Item = char>>> =
                    match (right, self.precision_with(dynamic)) {
                        (Some(digits), None) => Some(Box::new(digits)),
                        (Some(digits), Some(precision)) => Some(Box::new(
                            digits.chain(std::iter::repeat('0')).take(precision),
                        )),
                        (None, Some(precision)) => {
                            Some(Box::new(std::iter::repeat('0').take(precision)))
                        }
                        (None, None) => None,
                    };
                if let Some(past_decimal) = past_decimal {
                    dq.push_front(self.decimal_separator());

                    // .extend only pushes to the back
                    for item in past_decimal {
                        dq.push_front(item);
                    }
                }
                (dq, Some(decimal))
            }
            Base::LowerHex => (
                self.normalize(
                    number
                        .hex()
                        .ok_or_else(|| Error::NotImplemented(self.base(), type_name::<N>()))?,
                    dynamic,
                ),
                None,
            ),
            Base::UpperHex => (
                self.normalize(
                    number
                        .hex()
                        .ok_or_else(|| Error::NotImplemented(self.base(), type_name::<N>()))?
                        .map(|ch| ch.to_ascii_uppercase()),
                    dynamic,
                ),
                None,
            ),
        };

        debug_assert!(
            {
                let legal: Box<dyn Fn(&char) -> bool> = match self.base() {
                    Base::Binary => {
                        Box::new(move |ch| matches_separator(*ch) || ('0'..='1').contains(ch))
                    }
                    Base::Octal => {
                        Box::new(move |ch| matches_separator(*ch) || ('0'..='7').contains(ch))
                    }
                    Base::Decimal => Box::new(move |ch| {
                        *ch == decimal_separator
                            || matches_separator(*ch)
                            || ('0'..='9').contains(ch)
                    }),
                    Base::LowerHex => Box::new(move |ch| {
                        matches_separator(*ch)
                            || ('0'..='9').contains(ch)
                            || ('a'..='f').contains(ch)
                    }),
                    Base::UpperHex => Box::new(move |ch| {
                        matches_separator(*ch)
                            || ('0'..='9').contains(ch)
                            || ('A'..='F').contains(ch)
                    }),
                };
                digits.iter().all(legal)
            },
            "illegal characters in number; check its `impl Numeric`",
        );

        let width_desired = self.width_desired(dynamic);
        let mut decimal_pos = decimal_pos.unwrap_or_else(|| digits.len());
        let mut digit_count = if self.align() == Align::Decimal {
            decimal_pos
        } else {
            digits.len()
        };
        // padding and separating can introduce extraneous leading 0 chars, so let's fix that
        while digit_count > width_desired && {
            let last = *digits.back().expect("can't be empty while decimal_pos > 0");
            last == '0' || matches_separator(last)
        } {
            digit_count -= 1;
            decimal_pos -= 1;
            digits.pop_back();
        }

        let width_used = digits.len();
        let (mut padding_front, padding_rear) = match self.align() {
            Align::Right => (width_desired.saturating_sub(width_used), 0),
            Align::Left => (0, width_desired.saturating_sub(width_used)),
            Align::Center => {
                let unused_width = width_desired.saturating_sub(width_used);
                let half_unused_width = unused_width / 2;
                // bias right
                (unused_width - half_unused_width, half_unused_width)
            }
            Align::Decimal => (width_desired.saturating_sub(decimal_pos), 0),
        };

        let sign_char = match (self.sign(), negative) {
            (Sign::PlusAndMinus, _) => Some(if negative { '-' } else { '+' }),
            (Sign::OnlyMinus, true) => Some('-'),
            (Sign::OnlyMinus, false) => None,
        };
        if sign_char.is_some() {
            padding_front = padding_front.saturating_sub(1);
            if !digits.is_empty() {
                let back = *digits.back().expect("known not to be empty");
                if back == '0' || matches_separator(back) {
                    digits.pop_back();
                }
            }
        }

        let prefix = match (self.hash(), self.base()) {
            (false, _) => None,
            (_, Base::Binary) => Some("0b"),
            (_, Base::Octal) => Some("0o"),
            (_, Base::Decimal) => Some("0d"),
            (_, Base::LowerHex) | (_, Base::UpperHex) => Some("0x"),
        };
        if prefix.is_some() {
            padding_front = padding_front.saturating_sub(2);
        }

        // constant 3 ensures that even with a sign and a prefix, we don't have to reallocate
        let mut rendered = String::with_capacity(padding_front + padding_rear + width_used + 3);

        // finally, assemble all the ingredients
        //
        // the actual ordering depends on the configuration of `self.zero`:
        // when `true`, it's sign -> prefix -> padding;
        // when `false`, it's padding -> sign -> prefix

        if !self.zero {
            for _ in 0..padding_front {
                rendered.push(self.fill());
            }
        }

        if let Some(sign) = sign_char {
            rendered.push(sign);
        }
        if let Some(prefix) = prefix {
            rendered.push_str(prefix);
        }

        if self.zero {
            for _ in 0..padding_front {
                rendered.push(self.fill());
            }
        }

        for digit in digits.into_iter().rev() {
            rendered.push(digit);
        }
        for _ in 0..padding_rear {
            rendered.push(self.fill());
        }

        Ok(rendered)
    }

    /// `char` used to pad the extra space when the rendered number is smaller than the `width`.
    #[inline]
    pub fn fill(&self) -> char {
        self.fill.unwrap_or(' ')
    }

    /// Desired alignment.
    #[inline]
    pub fn align(&self) -> Align {
        self.align
    }

    /// Which signs are printed with the number.
    #[inline]
    pub fn sign(&self) -> Sign {
        self.sign
    }

    /// Whether to print a base specification before the number.
    #[inline]
    pub fn hash(&self) -> bool {
        self.hash
    }

    /// Whether the zero formatter was used.
    #[inline]
    pub fn zero(&self) -> bool {
        self.zero && self.fill() == '0'
    }

    /// Configured render width in bytes.
    #[inline]
    pub fn width(&self) -> usize {
        self.width
    }

    /// Configured post-decimal precision in bytes.
    ///
    /// Precision will pad or truncate as required if set. If unset, passes through as many
    /// digits past the decimal as the underlying type naturally returns.
    #[inline]
    pub fn precision(&self) -> Option<usize> {
        self.precision
    }

    /// Configured output format.
    #[inline]
    pub fn base(&self) -> Base {
        self.base
    }

    /// Configured group separator and spacing.
    ///
    /// If one or the other of these is set, the other will adopt
    /// an appropriate default. However, if neither is configured, then
    /// no group separation will be performed.
    fn separator_and_spacing_with(&self, dynamic: Dynamic) -> Option<(char, usize)> {
        match (self.separator, self.spacing_with(dynamic)) {
            (Some(sep), Some(spc)) => Some((sep, spc)),
            (Some(sep), None) => Some((sep, 3)),
            (None, Some(spc)) => Some((',', spc)),
            (None, None) => None,
        }
    }

    /// Configured group separator and spacing.
    ///
    /// If one or the other of these is set, the other will adopt
    /// an appropriate default. However, if neither is configured, then
    /// no group separation will be performed.
    fn separator_and_spacing(&self) -> Option<(char, usize)> {
        self.separator_and_spacing_with(Dynamic::default())
    }

    /// Configured group separator.
    #[inline]
    pub fn separator(&self) -> Option<char> {
        self.separator_and_spacing().map(|(separator, _)| separator)
    }

    /// Configured group size.
    #[inline]
    pub fn spacing(&self) -> Option<usize> {
        self.separator_and_spacing().map(|(_, spacing)| spacing)
    }

    /// Configured decimal separator.
    #[inline]
    pub fn decimal_separator(&self) -> char {
        self.decimal_separator.unwrap_or('.')
    }

    fn width_with(&self, dynamic: Dynamic) -> usize {
        dynamic.width.unwrap_or(self.width)
    }

    fn precision_with(&self, dynamic: Dynamic) -> Option<usize> {
        dynamic.precision.or(self.precision)
    }

    fn spacing_with(&self, dynamic: Dynamic) -> Option<usize> {
        dynamic.spacing.or(self.spacing)
    }
}

impl FromStr for NumFmt {
    type Err = parse::Error;

    fn from_str(s: &str) -> Result<Self, Self::Err> {
        parse::parse(s)
    }
}

#[cfg(test)]
mod tests {
    use super::*;

    #[test]
    fn test_dynamic_width() {
        let fmt = NumFmt::from_str("#04x_2").unwrap();
        assert!(fmt.zero());
        assert_eq!(fmt.fmt(0).unwrap(), "0x00");

        let dynamic = Dynamic::width(7);
        dbg!(
            fmt.separator(),
            dynamic,
            fmt.width_with(dynamic),
            fmt.precision_with(dynamic),
            fmt.spacing_with(dynamic)
        );

        assert_eq!(fmt.fmt_with(0, dynamic).unwrap(), "0x00_00");
    }

    #[test]
    fn test_separator() {
        let fmt = NumFmt::from_str(",").unwrap();
        assert_eq!(fmt.fmt(123_456_789).unwrap(), "123,456,789");
    }
}