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
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
//! Miscellaneous tools to format and parse numbers, durations, etc.
//!
//! TODO(emilk): move some of this numeric formatting into `emath` so we can use it in `egui_plot`.

mod time;

use std::{cmp::PartialOrd, fmt::Display};

pub use time::{format_timestamp_seconds, next_grid_tick_magnitude_ns, parse_timestamp_seconds};

// --- Numbers ---

/// The minus character: <https://www.compart.com/en/unicode/U+2212>
///
/// Looks slightly different from the normal hyphen `-`.
const MINUS: char = '−';

// TODO(rust-num/num-traits#315): waiting for https://github.com/rust-num/num-traits/issues/315 to land
pub trait UnsignedAbs {
    /// An unsigned type which is large enough to hold the absolute value of `Self`.
    type Unsigned;

    /// Computes the absolute value of `self` without any wrapping or panicking.
    fn unsigned_abs(self) -> Self::Unsigned;
}

impl UnsignedAbs for i8 {
    type Unsigned = u8;

    #[inline]
    fn unsigned_abs(self) -> Self::Unsigned {
        self.unsigned_abs()
    }
}

impl UnsignedAbs for i16 {
    type Unsigned = u16;

    #[inline]
    fn unsigned_abs(self) -> Self::Unsigned {
        self.unsigned_abs()
    }
}

impl UnsignedAbs for i32 {
    type Unsigned = u32;

    #[inline]
    fn unsigned_abs(self) -> Self::Unsigned {
        self.unsigned_abs()
    }
}

impl UnsignedAbs for i64 {
    type Unsigned = u64;

    #[inline]
    fn unsigned_abs(self) -> Self::Unsigned {
        self.unsigned_abs()
    }
}

impl UnsignedAbs for i128 {
    type Unsigned = u128;

    #[inline]
    fn unsigned_abs(self) -> Self::Unsigned {
        self.unsigned_abs()
    }
}

impl UnsignedAbs for isize {
    type Unsigned = usize;

    #[inline]
    fn unsigned_abs(self) -> Self::Unsigned {
        self.unsigned_abs()
    }
}

/// Pretty format a signed number by using thousands separators for readability.
///
/// The returned value is for human eyes only, and can not be parsed
/// by the normal `usize::from_str` function.
pub fn format_int<Int>(number: Int) -> String
where
    Int: Display + PartialOrd + num_traits::Zero + UnsignedAbs,
    Int::Unsigned: Display + num_traits::Unsigned,
{
    if number < Int::zero() {
        format!("{MINUS}{}", format_uint(number.unsigned_abs()))
    } else {
        add_thousands_separators(&number.to_string())
    }
}

/// Pretty format an unsigned integer by using thousands separators for readability.
///
/// The returned value is for human eyes only, and can not be parsed
/// by the normal `usize::from_str` function.
#[allow(clippy::needless_pass_by_value)]
pub fn format_uint<Uint>(number: Uint) -> String
where
    Uint: Display + num_traits::Unsigned,
{
    add_thousands_separators(&number.to_string())
}

/// Add thousands separators to a number, every three steps,
/// counting from the last character.
fn add_thousands_separators(number: &str) -> String {
    let mut chars = number.chars().rev().peekable();

    let mut result = vec![];
    while chars.peek().is_some() {
        if !result.is_empty() {
            // thousands-deliminator:
            let thin_space = '\u{2009}'; // https://en.wikipedia.org/wiki/Thin_space
            result.push(thin_space);
        }
        for _ in 0..3 {
            if let Some(c) = chars.next() {
                result.push(c);
            }
        }
    }

    result.reverse();
    result.into_iter().collect()
}

#[test]
fn test_format_uint() {
    assert_eq!(format_uint(42_u32), "42");
    assert_eq!(format_uint(999_u32), "999");
    assert_eq!(format_uint(1_000_u32), "1 000");
    assert_eq!(format_uint(123_456_u32), "123 456");
    assert_eq!(format_uint(1_234_567_u32), "1 234 567");
}

/// Options for how to format a floating point number, e.g. an [`f64`].
#[derive(Clone, Copy, Debug, PartialEq, Eq, Hash)]
pub struct FloatFormatOptions {
    /// Always show the sign, even if it is positive (`+`).
    pub always_sign: bool,

    /// Maximum digits of precision to use.
    ///
    /// This includes both the integer part and the fractional part.
    pub precision: usize,

    /// Max number of decimals to show after the decimal point.
    ///
    /// If not specified, [`Self::precision`] is used instead.
    pub num_decimals: Option<usize>,

    pub strip_trailing_zeros: bool,

    /// Only add thousands separators to decimals if there are at least this many decimals.
    pub min_decimals_for_thousands_separators: usize,
}

impl FloatFormatOptions {
    /// Default options for formatting an [`f32`].
    #[allow(non_upper_case_globals)]
    pub const DEFAULT_f32: Self = Self {
        always_sign: false,
        precision: 7,
        num_decimals: None,
        strip_trailing_zeros: true,
        min_decimals_for_thousands_separators: 6,
    };

    /// Default options for formatting an [`f64`].
    #[allow(non_upper_case_globals)]
    pub const DEFAULT_f64: Self = Self {
        always_sign: false,
        precision: 15,
        num_decimals: None,
        strip_trailing_zeros: true,
        min_decimals_for_thousands_separators: 6,
    };

    /// Always show the sign, even if it is positive (`+`).
    #[inline]
    pub fn with_always_sign(mut self, always_sign: bool) -> Self {
        self.always_sign = always_sign;
        self
    }

    /// Show at most this many digits of precision,
    /// including both the integer part and the fractional part.
    #[inline]
    pub fn with_precision(mut self, precision: usize) -> Self {
        self.precision = precision;
        self
    }

    /// Max number of decimals to show after the decimal point.
    ///
    /// If not specified, [`Self::precision`] is used instead.
    #[inline]
    pub fn with_decimals(mut self, num_decimals: usize) -> Self {
        self.num_decimals = Some(num_decimals);
        self
    }

    /// Strip trailing zeros from decimal expansion?
    #[inline]
    pub fn with_strip_trailing_zeros(mut self, strip_trailing_zeros: bool) -> Self {
        self.strip_trailing_zeros = strip_trailing_zeros;
        self
    }

    /// The returned value is for human eyes only, and can not be parsed
    /// by the normal `f64::from_str` function.
    pub fn format(&self, value: impl Into<f64>) -> String {
        self.format_f64(value.into())
    }

    fn format_f64(&self, mut value: f64) -> String {
        fn reverse(s: &str) -> String {
            s.chars().rev().collect()
        }

        let Self {
            always_sign,
            precision,
            num_decimals,
            strip_trailing_zeros,
            min_decimals_for_thousands_separators,
        } = *self;

        if value.is_nan() {
            return "NaN".to_owned();
        }

        let sign = if value < 0.0 {
            value = -value;
            "−" // NOTE: the minus character: <https://www.compart.com/en/unicode/U+2212>
        } else if always_sign {
            "+"
        } else {
            ""
        };

        let abs_string = if value == f64::INFINITY {
            "∞".to_owned()
        } else {
            let magnitude = value.log10();
            let max_decimals = precision as f64 - magnitude.max(0.0);

            if max_decimals < 0.0 {
                // A very large number (more digits than we have precision),
                // so use scientific notation.
                // TODO(emilk): nice formatting of scientific notation with thousands separators
                format!("{:.*e}", precision.saturating_sub(1), value)
            } else {
                let max_decimals = max_decimals as usize;

                let num_decimals = if let Some(num_decimals) = num_decimals {
                    num_decimals.min(max_decimals)
                } else {
                    max_decimals
                };

                let mut formatted = format!("{value:.num_decimals$}");

                if strip_trailing_zeros && formatted.contains('.') {
                    while formatted.ends_with('0') {
                        formatted.pop();
                    }
                    if formatted.ends_with('.') {
                        formatted.pop();
                    }
                }

                if let Some(dot) = formatted.find('.') {
                    let integer_part = &formatted[..dot];
                    let fractional_part = &formatted[dot + 1..];
                    // let fractional_part = &fractional_part[..num_decimals.min(fractional_part.len())];

                    let integer_part = add_thousands_separators(integer_part);

                    if fractional_part.len() < min_decimals_for_thousands_separators {
                        format!("{integer_part}.{fractional_part}")
                    } else {
                        // For the fractional part we should start counting thousand separators from the _front_, so we reverse:
                        let fractional_part =
                            reverse(&add_thousands_separators(&reverse(fractional_part)));
                        format!("{integer_part}.{fractional_part}")
                    }
                } else {
                    add_thousands_separators(&formatted) // it's an integer
                }
            }
        };

        format!("{sign}{abs_string}")
    }
}

/// Format a number with about 15 decimals of precision.
///
/// The returned value is for human eyes only, and can not be parsed
/// by the normal `f64::from_str` function.
pub fn format_f64(value: f64) -> String {
    FloatFormatOptions::DEFAULT_f64.format(value)
}

/// Format a number with about 7 decimals of precision.
///
/// The returned value is for human eyes only, and can not be parsed
/// by the normal `f64::from_str` function.
pub fn format_f32(value: f32) -> String {
    FloatFormatOptions::DEFAULT_f32.format(value)
}

/// Format a latitude or longitude value.
///
/// For human eyes only.
pub fn format_lat_lon(value: f64) -> String {
    format!(
        "{}°",
        FloatFormatOptions {
            always_sign: true,
            precision: 10,
            num_decimals: Some(6),
            strip_trailing_zeros: false,
            min_decimals_for_thousands_separators: 10,
        }
        .format_f64(value)
    )
}

#[test]
fn test_format_f32() {
    let cases = [
        (f32::NAN, "NaN"),
        (f32::INFINITY, "∞"),
        (f32::NEG_INFINITY, "−∞"),
        (0.0, "0"),
        (42.0, "42"),
        (10_000.0, "10 000"),
        (1_000_000.0, "1 000 000"),
        (10_000_000.0, "10 000 000"),
        (11_000_000.0, "1.100000e7"),
        (-42.0, "−42"),
        (-4.20, "−4.2"),
        (123_456.78, "123 456.8"),
        (78.4321, "78.4321"), // min_decimals_for_thousands_separators
        (-std::f32::consts::PI, "−3.141 593"),
        (-std::f32::consts::PI * 1e6, "−3 141 593"),
        (-std::f32::consts::PI * 1e20, "−3.141593e20"), // We switch to scientific notation to not show false precision
    ];
    for (value, expected) in cases {
        let got = format_f32(value);
        assert!(
            got == expected,
            "Expected to format {value} as '{expected}', but got '{got}'"
        );
    }
}

#[test]
fn test_format_f64() {
    let cases = [
        (f64::NAN, "NaN"),
        (f64::INFINITY, "∞"),
        (f64::NEG_INFINITY, "−∞"),
        (0.0, "0"),
        (42.0, "42"),
        (-42.0, "−42"),
        (-4.20, "−4.2"),
        (123_456_789.0, "123 456 789"),
        (123_456_789.123_45, "123 456 789.12345"), // min_decimals_for_thousands_separators
        (0.0000123456789, "0.000 012 345 678 9"),
        (0.123456789, "0.123 456 789"),
        (1.23456789, "1.234 567 89"),
        (12.3456789, "12.345 678 9"),
        (123.456789, "123.456 789"),
        (1234.56789, "1 234.56789"), // min_decimals_for_thousands_separators
        (12345.6789, "12 345.6789"), // min_decimals_for_thousands_separators
        (78.4321, "78.4321"),        // min_decimals_for_thousands_separators
        (-std::f64::consts::PI, "−3.141 592 653 589 79"),
        (-std::f64::consts::PI * 1e6, "−3 141 592.653 589 79"),
        (-std::f64::consts::PI * 1e20, "−3.14159265358979e20"), // We switch to scientific notation to not show false precision
    ];
    for (value, expected) in cases {
        let got = format_f64(value);
        assert!(
            got == expected,
            "Expected to format {value} as '{expected}', but got '{got}'"
        );
    }
}

#[test]
fn test_format_f64_custom() {
    let cases = [(
        FloatFormatOptions::DEFAULT_f64.with_decimals(2),
        123.456789,
        "123.46",
    )];
    for (options, value, expected) in cases {
        let got = options.format(value);
        assert!(
            got == expected,
            "Expected to format {value} as '{expected}', but got '{got}'. Options: {options:#?}"
        );
    }
}

/// Parses a number, ignoring whitespace (e.g. thousand separators),
/// and treating the special minus character `MINUS` (−) as a minus sign.
pub fn parse_f64(text: &str) -> Option<f64> {
    let text: String = text
        .chars()
        // Ignore whitespace (trailing, leading, and thousands separators):
        .filter(|c| !c.is_whitespace())
        // Replace special minus character with normal minus (hyphen):
        .map(|c| if c == '−' { '-' } else { c })
        .collect();

    text.parse().ok()
}

/// Parses a number, ignoring whitespace (e.g. thousand separators),
/// and treating the special minus character `MINUS` (−) as a minus sign.
pub fn parse_i64(text: &str) -> Option<i64> {
    let text: String = text
        .chars()
        // Ignore whitespace (trailing, leading, and thousands separators):
        .filter(|c| !c.is_whitespace())
        // Replace special minus character with normal minus (hyphen):
        .map(|c| if c == '−' { '-' } else { c })
        .collect();

    text.parse().ok()
}

/// Pretty format a large number by using SI notation (base 10), e.g.
///
/// ```
/// # use re_format::approximate_large_number;
/// assert_eq!(approximate_large_number(123 as _), "123");
/// assert_eq!(approximate_large_number(12_345 as _), "12k");
/// assert_eq!(approximate_large_number(1_234_567 as _), "1.2M");
/// assert_eq!(approximate_large_number(123_456_789 as _), "123M");
/// ```
///
/// Prefer to use [`format_uint`], which outputs an exact string,
/// while still being readable thanks to half-width spaces used as thousands-separators.
pub fn approximate_large_number(number: f64) -> String {
    if number < 0.0 {
        format!("{MINUS}{}", approximate_large_number(-number))
    } else if number < 1000.0 {
        format!("{number:.0}")
    } else if number < 1_000_000.0 {
        let decimals = (number < 10_000.0) as usize;
        format!("{:.*}k", decimals, number / 1_000.0)
    } else if number < 1_000_000_000.0 {
        let decimals = (number < 10_000_000.0) as usize;
        format!("{:.*}M", decimals, number / 1_000_000.0)
    } else {
        let decimals = (number < 10_000_000_000.0) as usize;
        format!("{:.*}G", decimals, number / 1_000_000_000.0)
    }
}

#[test]
fn test_format_large_number() {
    let test_cases = [
        (999.0, "999"),
        (1000.0, "1.0k"),
        (1001.0, "1.0k"),
        (999_999.0, "1000k"),
        (1_000_000.0, "1.0M"),
        (999_999_999.0, "1000M"),
        (1_000_000_000.0, "1.0G"),
        (999_999_999_999.0, "1000G"),
        (1_000_000_000_000.0, "1000G"),
        (123.0, "123"),
        (12_345.0, "12k"),
        (1_234_567.0, "1.2M"),
        (123_456_789.0, "123M"),
    ];

    for (value, expected) in test_cases {
        assert_eq!(expected, approximate_large_number(value));
    }
}

// --- Bytes ---

/// Pretty format a number of bytes by using SI notation (base2), e.g.
///
/// ```
/// # use re_format::format_bytes;
/// assert_eq!(format_bytes(123.0), "123 B");
/// assert_eq!(format_bytes(12_345.0), "12.1 KiB");
/// assert_eq!(format_bytes(1_234_567.0), "1.2 MiB");
/// assert_eq!(format_bytes(123_456_789.0), "118 MiB");
/// ```
pub fn format_bytes(number_of_bytes: f64) -> String {
    if number_of_bytes < 0.0 {
        format!("{MINUS}{}", format_bytes(-number_of_bytes))
    } else if number_of_bytes == 0.0 {
        "0 B".to_owned()
    } else if number_of_bytes < 1.0 {
        format!("{number_of_bytes} B")
    } else if number_of_bytes < 20.0 {
        let is_integer = number_of_bytes.round() == number_of_bytes;
        if is_integer {
            format!("{number_of_bytes:.0} B")
        } else {
            format!("{number_of_bytes:.1} B")
        }
    } else if number_of_bytes < 10.0_f64.exp2() {
        format!("{number_of_bytes:.0} B")
    } else if number_of_bytes < 20.0_f64.exp2() {
        let decimals = (10.0 * number_of_bytes < 20.0_f64.exp2()) as usize;
        format!("{:.*} KiB", decimals, number_of_bytes / 10.0_f64.exp2())
    } else if number_of_bytes < 30.0_f64.exp2() {
        let decimals = (10.0 * number_of_bytes < 30.0_f64.exp2()) as usize;
        format!("{:.*} MiB", decimals, number_of_bytes / 20.0_f64.exp2())
    } else {
        let decimals = (10.0 * number_of_bytes < 40.0_f64.exp2()) as usize;
        format!("{:.*} GiB", decimals, number_of_bytes / 30.0_f64.exp2())
    }
}

#[test]
fn test_format_bytes() {
    let test_cases = [
        (0.0, "0 B"),
        (0.25, "0.25 B"),
        (1.51, "1.5 B"),
        (11.0, "11 B"),
        (12.5, "12.5 B"),
        (999.0, "999 B"),
        (1000.0, "1000 B"),
        (1001.0, "1001 B"),
        (1023.0, "1023 B"),
        (1024.0, "1.0 KiB"),
        (1025.0, "1.0 KiB"),
        (1024.0 * 1.2345, "1.2 KiB"),
        (1024.0 * 12.345, "12.3 KiB"),
        (1024.0 * 123.45, "123 KiB"),
        (1024f64.powi(2) - 1.0, "1024 KiB"),
        (1024f64.powi(2) + 0.0, "1.0 MiB"),
        (1024f64.powi(2) + 1.0, "1.0 MiB"),
        (1024f64.powi(3) - 1.0, "1024 MiB"),
        (1024f64.powi(3) + 0.0, "1.0 GiB"),
        (1024f64.powi(3) + 1.0, "1.0 GiB"),
        (1.2345 * 30.0_f64.exp2(), "1.2 GiB"),
        (12.345 * 30.0_f64.exp2(), "12.3 GiB"),
        (123.45 * 30.0_f64.exp2(), "123 GiB"),
        (1024f64.powi(4) - 1.0, "1024 GiB"),
        (1024f64.powi(4) + 0.0, "1024 GiB"),
        (1024f64.powi(4) + 1.0, "1024 GiB"),
        (123.0, "123 B"),
        (12_345.0, "12.1 KiB"),
        (1_234_567.0, "1.2 MiB"),
        (123_456_789.0, "118 MiB"),
    ];

    for (value, expected) in test_cases {
        assert_eq!(format_bytes(value), expected);
    }
}

pub fn parse_bytes_base10(bytes: &str) -> Option<i64> {
    // Note: intentionally case sensitive so that we don't parse `Mb` (Megabit) as `MB` (Megabyte).
    if let Some(rest) = bytes.strip_prefix(MINUS) {
        Some(-parse_bytes_base10(rest)?)
    } else if let Some(kb) = bytes.strip_suffix("kB") {
        Some(kb.parse::<i64>().ok()? * 1_000)
    } else if let Some(mb) = bytes.strip_suffix("MB") {
        Some(mb.parse::<i64>().ok()? * 1_000_000)
    } else if let Some(gb) = bytes.strip_suffix("GB") {
        Some(gb.parse::<i64>().ok()? * 1_000_000_000)
    } else if let Some(tb) = bytes.strip_suffix("TB") {
        Some(tb.parse::<i64>().ok()? * 1_000_000_000_000)
    } else if let Some(b) = bytes.strip_suffix('B') {
        Some(b.parse::<i64>().ok()?)
    } else {
        None
    }
}

#[test]
fn test_parse_bytes_base10() {
    let test_cases = [
        ("999B", 999),
        ("1000B", 1_000),
        ("1kB", 1_000),
        ("1000kB", 1_000_000),
        ("1MB", 1_000_000),
        ("1000MB", 1_000_000_000),
        ("1GB", 1_000_000_000),
        ("1000GB", 1_000_000_000_000),
        ("1TB", 1_000_000_000_000),
        ("1000TB", 1_000_000_000_000_000),
        ("123B", 123),
        ("12kB", 12_000),
        ("123MB", 123_000_000),
        ("-10B", -10), // hyphen-minus
        ("−10B", -10), // proper minus
    ];
    for (value, expected) in test_cases {
        assert_eq!(Some(expected), parse_bytes_base10(value));
    }
}

pub fn parse_bytes_base2(bytes: &str) -> Option<i64> {
    // Note: intentionally case sensitive so that we don't parse `Mib` (Mebibit) as `MiB` (Mebibyte).
    if let Some(rest) = bytes.strip_prefix(MINUS) {
        Some(-parse_bytes_base2(rest)?)
    } else if let Some(kb) = bytes.strip_suffix("KiB") {
        Some(kb.parse::<i64>().ok()? * 1024)
    } else if let Some(mb) = bytes.strip_suffix("MiB") {
        Some(mb.parse::<i64>().ok()? * 1024 * 1024)
    } else if let Some(gb) = bytes.strip_suffix("GiB") {
        Some(gb.parse::<i64>().ok()? * 1024 * 1024 * 1024)
    } else if let Some(tb) = bytes.strip_suffix("TiB") {
        Some(tb.parse::<i64>().ok()? * 1024 * 1024 * 1024 * 1024)
    } else if let Some(b) = bytes.strip_suffix('B') {
        Some(b.parse::<i64>().ok()?)
    } else {
        None
    }
}

#[test]
fn test_parse_bytes_base2() {
    let test_cases = [
        ("999B", 999),
        ("1023B", 1_023),
        ("1024B", 1_024),
        ("1KiB", 1_024),
        ("1000KiB", 1_000 * 1024),
        ("1MiB", 1024 * 1024),
        ("1000MiB", 1_000 * 1024 * 1024),
        ("1GiB", 1024 * 1024 * 1024),
        ("1000GiB", 1_000 * 1024 * 1024 * 1024),
        ("1TiB", 1024 * 1024 * 1024 * 1024),
        ("1000TiB", 1_000 * 1024 * 1024 * 1024 * 1024),
        ("123B", 123),
        ("12KiB", 12 * 1024),
        ("123MiB", 123 * 1024 * 1024),
        ("-10B", -10), // hyphen-minus
        ("−10B", -10), // proper minus
    ];
    for (value, expected) in test_cases {
        assert_eq!(Some(expected), parse_bytes_base2(value));
    }
}

pub fn parse_bytes(bytes: &str) -> Option<i64> {
    parse_bytes_base10(bytes).or_else(|| parse_bytes_base2(bytes))
}

#[test]
fn test_parse_bytes() {
    let test_cases = [
        // base10
        ("999B", 999),
        ("1000B", 1_000),
        ("1kB", 1_000),
        ("1000kB", 1_000_000),
        ("1MB", 1_000_000),
        ("1000MB", 1_000_000_000),
        ("1GB", 1_000_000_000),
        ("1000GB", 1_000_000_000_000),
        ("1TB", 1_000_000_000_000),
        ("1000TB", 1_000_000_000_000_000),
        ("123B", 123),
        ("12kB", 12_000),
        ("123MB", 123_000_000),
        // base2
        ("999B", 999),
        ("1023B", 1_023),
        ("1024B", 1_024),
        ("1KiB", 1_024),
        ("1000KiB", 1_000 * 1024),
        ("1MiB", 1024 * 1024),
        ("1000MiB", 1_000 * 1024 * 1024),
        ("1GiB", 1024 * 1024 * 1024),
        ("1000GiB", 1_000 * 1024 * 1024 * 1024),
        ("1TiB", 1024 * 1024 * 1024 * 1024),
        ("1000TiB", 1_000 * 1024 * 1024 * 1024 * 1024),
        ("123B", 123),
        ("12KiB", 12 * 1024),
        ("123MiB", 123 * 1024 * 1024),
    ];
    for (value, expected) in test_cases {
        assert_eq!(Some(expected), parse_bytes(value));
    }
}

// --- Durations ---

pub fn parse_duration(duration: &str) -> Result<f32, String> {
    fn parse_num(s: &str) -> Result<f32, String> {
        s.parse()
            .map_err(|_ignored| format!("Expected a number, got {s:?}"))
    }

    if let Some(ms) = duration.strip_suffix("ms") {
        Ok(parse_num(ms)? * 1e-3)
    } else if let Some(s) = duration.strip_suffix('s') {
        Ok(parse_num(s)?)
    } else if let Some(s) = duration.strip_suffix('m') {
        Ok(parse_num(s)? * 60.0)
    } else if let Some(s) = duration.strip_suffix('h') {
        Ok(parse_num(s)? * 60.0 * 60.0)
    } else {
        Err(format!(
            "Expected a suffix of 'ms', 's', 'm' or 'h' in string {duration:?}"
        ))
    }
}

#[test]
fn test_parse_duration() {
    assert_eq!(parse_duration("3.2s"), Ok(3.2));
    assert_eq!(parse_duration("250ms"), Ok(0.250));
    assert_eq!(parse_duration("3m"), Ok(3.0 * 60.0));
}