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
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
//! An iterator that skips values equal to a provided value.
//!
//! Iterators over a contiguous slice, returning all values
//! except for those matching the provided skip value.
//!
//! # Complexity
//!
//! Although superficially quite simple, the level of complexity
//! introduced by digit separators can be quite complex, due
//! the number of permutations during parsing.
//!
//! We can consume any combinations of of \[0,3\] items from the following set:
//!     - \[l\]eading digit separators, where digit separators occur before digits.
//!     - \[i\]nternal digit separators, where digit separators occur between digits.
//!     - \[t\]railing digit separators, where digit separators occur after digits.
//!
//! In addition to those combinations, we can also have:
//!     - \[c\]onsecutive digit separators, which allows two digit separators to be adjacent.
//!
//! # Shorthand
//!
//! We will use the term consumer to denote a function that consumes digits,
//! splitting an input buffer at an index, where the leading section contains
//! valid input digits, and the trailing section contains invalid characters.
//! Due to the number of combinations for consumers, we use the following
//! shorthand to denote consumers:
//!     - `no`, does not use a digit separator.
//!     - `l`, consumes leading digit separators.
//!     - `i`, consumes internal digit separators.
//!     - `t`, consumes trailing digit separators.
//!     - `c`, consumes consecutive digit separators.
//!
//! The `next`/`iter` algorithms are therefore named `next_x`, where `x`
//! represents the shorthand name of the consumer, in sorted order.
//!  For example, `next_ilt` means that consumer can skip internal,
//! leading, and trailing digit separators, but not consecutive ones.

#![cfg(all(feature = "format", feature = "parse"))]

use crate::digit::char_is_digit_const;
use crate::format::NumberFormat;
use crate::format_flags as flags;
use crate::iterator::BytesIter;
use core::{mem, ptr};

// PEEK
// ----

/// Determine if the digit separator is internal.
///
/// Preconditions: Assumes `slc[index]` is a digit separator.
/// The compiler optimizes this pretty well: it's almost as efficient as
/// optimized assembly without bounds checking.
macro_rules! is_i {
    ($self:ident) => {
        !is_l!($self) && !is_t!($self)
    };
}

/// Determine if the digit separator is leading.
///
/// Preconditions: Assumes `slc[index]` is a digit separator.
/// The compiler optimizes this pretty well: it's almost as efficient as
/// optimized assembly without bounds checking.
macro_rules! is_l {
    ($self:ident) => {{
        // Consume any digit separators before the current one.
        let mut index = $self.byte.index;
        while index > 0
            && $self.byte.slc.get(index - 1).map_or(false, |&x| $self.is_digit_separator(x))
        {
            index -= 1;
        }

        // True if there are no items before the digit separator, or character
        // before the digit separators is not a digit.
        index == 0 || !$self.byte.slc.get(index - 1).map_or(false, |&x| $self.is_digit(x))
    }};
}

/// Determine if the digit separator is trailing.
///
/// Preconditions: Assumes `slc[index]` is a digit separator.
/// The compiler optimizes this pretty well: it's almost as efficient as
/// optimized assembly without bounds checking.
macro_rules! is_t {
    ($self:ident) => {{
        // Consume any digit separators after the current one.
        let mut index = $self.byte.index;
        while index < $self.byte.slc.len()
            && $self.byte.slc.get(index + 1).map_or(false, |&x| $self.is_digit_separator(x))
        {
            index += 1;
        }

        index == $self.byte.slc.len()
            || !$self.byte.slc.get(index + 1).map_or(false, |&x| $self.is_digit(x))
    }};
}

/// Determine if the digit separator is leading or internal.
///
/// Preconditions: Assumes `slc[index]` is a digit separator.
macro_rules! is_il {
    ($self:ident) => {
        is_l!($self) || !is_t!($self)
    };
}

/// Determine if the digit separator is internal or trailing.
///
/// Preconditions: Assumes `slc[index]` is a digit separator.
macro_rules! is_it {
    ($self:ident) => {
        is_t!($self) || !is_l!($self)
    };
}

/// Determine if the digit separator is leading or trailing.
///
/// Preconditions: Assumes `slc[index]` is a digit separator.
macro_rules! is_lt {
    ($self:ident) => {
        is_l!($self) || is_t!($self)
    };
}

/// Determine if the digit separator is internal, leading, or trailing.
macro_rules! is_ilt {
    ($self:ident) => {
        true
    };
}

/// Consumes 1 or more digit separators.
/// Peeks the next token that's not a digit separator.
macro_rules! peek_1 {
    ($self:ident, $is_skip:ident) => {{
        // This will consume consecutive digit separators.
        let value = $self.byte.slc.get($self.byte.index)?;
        let is_digit_separator = $self.is_digit_separator(*value);
        if is_digit_separator && $is_skip!($self) {
            // Have a skippable digit separator: keep incrementing until we find
            // a non-digit separator character. Don't need any complex checks
            // here, since we've already done them above.
            let mut index = $self.byte.index + 1;
            while index < $self.length()
                && $self.byte.slc.get(index).map_or(false, |&x| $self.is_digit_separator(x))
            {
                index += 1;
            }
            $self.byte.index = index;
            $self.byte.slc.get($self.byte.index)
        } else {
            // Have 1 of 2 conditions:
            //  1. A non-digit separator character.
            //  2. A digit separator that is not valid in the context.
            Some(value)
        }
    }};
}

/// Consumes 1 or more digit separators.
/// Peeks the next token that's not a digit separator.
macro_rules! peek_n {
    ($self:ident, $is_skip:ident) => {{
        // This will consume consecutive digit separators.
        let value = $self.byte.slc.get($self.byte.index)?;
        let is_digit_separator = $self.is_digit_separator(*value);
        if is_digit_separator && $is_skip!($self) {
            // Have a skippable digit separator: keep incrementing until we find
            // a non-digit separator character. Don't need any complex checks
            // here, since we've already done them above.
            let mut index = $self.byte.index + 1;
            while index < $self.byte.slc.len()
                && $self.byte.slc.get(index).map_or(false, |&x| $self.is_digit_separator(x))
            {
                index += 1;
            }
            $self.byte.index = index;
            $self.byte.slc.get($self.byte.index)
        } else {
            // Have 1 of 2 conditions:
            //  1. A non-digit separator character.
            //  2. A digit separator that is not valid in the context.
            Some(value)
        }
    }};
}

/// Consumes no digit separators and peeks the next value.
macro_rules! peek_noskip {
    ($self:ident) => {
        $self.byte.slc.get($self.byte.index)
    };
}

/// Consumes at most 1 leading digit separator and peeks the next value.
macro_rules! peek_l {
    ($self:ident) => {
        peek_1!($self, is_l)
    };
}

/// Consumes at most 1 internal digit separator and peeks the next value.
macro_rules! peek_i {
    ($self:ident) => {
        peek_1!($self, is_i)
    };
}

/// Consumes at most 1 trailing digit separator and peeks the next value.
macro_rules! peek_t {
    ($self:ident) => {
        peek_1!($self, is_t)
    };
}

/// Consumes at most 1 internal/leading digit separator and peeks the next value.
macro_rules! peek_il {
    ($self:ident) => {
        peek_1!($self, is_il)
    };
}

/// Consumes at most 1 internal/trailing digit separator and peeks the next value.
macro_rules! peek_it {
    ($self:ident) => {
        peek_1!($self, is_it)
    };
}

/// Consumes at most 1 leading/trailing digit separator and peeks the next value.
macro_rules! peek_lt {
    ($self:ident) => {
        peek_1!($self, is_lt)
    };
}

/// Consumes at most 1 digit separator and peeks the next value.
macro_rules! peek_ilt {
    ($self:ident) => {
        peek_1!($self, is_ilt)
    };
}

/// Consumes 1 or more leading digit separators and peeks the next value.
macro_rules! peek_lc {
    ($self:ident) => {
        peek_n!($self, is_l)
    };
}

/// Consumes 1 or more internal digit separators and peeks the next value.
macro_rules! peek_ic {
    ($self:ident) => {
        peek_n!($self, is_i)
    };
}

/// Consumes 1 or more trailing digit separators and peeks the next value.
macro_rules! peek_tc {
    ($self:ident) => {
        peek_n!($self, is_t)
    };
}

/// Consumes 1 or more internal/leading digit separators and peeks the next value.
macro_rules! peek_ilc {
    ($self:ident) => {
        peek_n!($self, is_il)
    };
}

/// Consumes 1 or more internal/trailing digit separators and peeks the next value.
macro_rules! peek_itc {
    ($self:ident) => {
        peek_n!($self, is_it)
    };
}

/// Consumes 1 or more leading/trailing digit separators and peeks the next value.
macro_rules! peek_ltc {
    ($self:ident) => {
        peek_n!($self, is_lt)
    };
}

/// Consumes 1 or more digit separators and peeks the next value.
macro_rules! peek_iltc {
    ($self:ident) => {{
        loop {
            let value = $self.byte.slc.get($self.byte.index)?;
            if !$self.is_digit_separator(*value) {
                return Some(value);
            }
            $self.byte.index += 1;
        }
    }};
}

// AS DIGITS
// ---------

/// Trait to simplify creation of a `Bytes` object.
pub trait AsBytes<'a> {
    /// Create `Bytes` from object.
    fn bytes<const FORMAT: u128>(&'a self) -> Bytes<'a, FORMAT>;
}

impl<'a> AsBytes<'a> for [u8] {
    #[inline]
    fn bytes<const FORMAT: u128>(&'a self) -> Bytes<'a, FORMAT> {
        Bytes::new(self)
    }
}

// DIGITS
// ------

/// Slice iterator that skips characters matching a given value.
///
/// This wraps an iterator over a contiguous block of memory,
/// and only returns values that are not equal to skip.
///
/// The format allows us to dictate the actual behavior of
/// the iterator: in what contexts does it skip digit separators.
///
/// `FORMAT` is required to tell us what the digit separator is, and where
/// the digit separators are allowed, as well tell us the radix.
/// The radix is required to allow us to differentiate digit from
/// non-digit characters (see [DigitSeparators](/docs/DigitSeparators.md)
/// for a detailed explanation on why).
#[derive(Clone)]
pub struct Bytes<'a, const FORMAT: u128> {
    /// The raw slice for the iterator.
    slc: &'a [u8],
    /// Current index of the iterator in the slice.
    index: usize,
    /// The current count of values returned by the iterator.
    /// This is only used if the iterator is not contiguous.
    count: usize,
}

impl<'a, const FORMAT: u128> Bytes<'a, FORMAT> {
    /// If each yielded value is adjacent in memory.
    pub const IS_CONTIGUOUS: bool = NumberFormat::<{ FORMAT }>::DIGIT_SEPARATOR == 0;

    /// Create new byte object.
    #[inline]
    pub fn new(slc: &'a [u8]) -> Self {
        Self {
            slc,
            index: 0,
            count: 0,
        }
    }

    /// Get a ptr to the current start of the iterator.
    #[inline]
    pub fn as_ptr(&self) -> *const u8 {
        self.as_slice().as_ptr()
    }

    /// Get a slice to the current start of the iterator.
    #[inline]
    pub fn as_slice(&self) -> &'a [u8] {
        // SAFETY: safe since index must be in range
        unsafe { self.slc.get_unchecked(self.index..) }
    }

    /// Get the total number of elements in the underlying slice.
    #[inline]
    pub fn length(&self) -> usize {
        self.slc.len()
    }

    /// Get the current index of the iterator in the slice.
    #[inline]
    pub fn cursor(&self) -> usize {
        self.index
    }

    /// Set the current index of the iterator in the slice.
    ///
    /// # Safety
    ///
    /// Safe if `index <= self.length()`.
    #[inline]
    pub unsafe fn set_cursor(&mut self, index: usize) {
        debug_assert!(index <= self.length());
        self.index = index
    }

    /// Get the current number of values returned by the iterator.
    #[inline]
    pub fn current_count(&self) -> usize {
        // If the buffer is contiguous, then we don't need to track the
        // number of values: the current index is enough.
        if Self::IS_CONTIGUOUS {
            self.index
        } else {
            self.count
        }
    }

    /// Get if the buffer underlying the iterator is empty.
    ///
    /// This might not be the same thing as `is_consumed`: `is_consumed`
    /// checks if any more elements may be returned, which may require
    /// peeking the next value. Consumed merely checks if the
    /// iterator has an empty slice. It is effectively a cheaper,
    /// but weaker variant of `is_consumed()`.
    #[inline]
    pub fn is_done(&self) -> bool {
        self.index >= self.slc.len()
    }

    // Determine if the abstraction is contiguous.
    #[inline]
    pub fn is_contiguous(&self) -> bool {
        Self::IS_CONTIGUOUS
    }

    /// Read a value of a difference type from the iterator.
    /// This advances the internal state of the iterator.
    ///
    /// # Safety
    ///
    /// Safe as long as the number of the buffer is contains as least as
    /// many bytes as the size of V.
    #[inline]
    pub unsafe fn read_unchecked<V>(&self) -> V {
        debug_assert!(Self::IS_CONTIGUOUS);
        debug_assert!(self.as_slice().len() >= mem::size_of::<V>());

        let slc = self.as_slice();
        // SAFETY: safe as long as the slice has at least count elements.
        unsafe { ptr::read_unaligned::<V>(slc.as_ptr() as *const _) }
    }

    /// Try to read a value of a different type from the iterator.
    /// This advances the internal state of the iterator.
    #[inline]
    pub fn read<V>(&self) -> Option<V> {
        if Self::IS_CONTIGUOUS && self.as_slice().len() >= mem::size_of::<V>() {
            // SAFETY: safe since we've guaranteed the buffer is greater than
            // the number of elements read.
            unsafe { Some(self.read_unchecked()) }
        } else {
            None
        }
    }

    /// Check if the next element is a given value.
    #[inline]
    pub fn first_is(&mut self, value: u8) -> bool {
        // Don't assert not a digit separator, since this can occur when
        // a different component does not allow digit separators there.
        if let Some(&c) = self.slc.get(self.index) {
            c == value
        } else {
            false
        }
    }

    /// Check if the next element is a given value without case sensitivity.
    #[inline]
    pub fn case_insensitive_first_is(&mut self, value: u8) -> bool {
        // Don't assert not a digit separator, since this can occur when
        // a different component does not allow digit separators there.
        if let Some(&c) = self.slc.get(self.index) {
            c.to_ascii_lowercase() == value.to_ascii_lowercase()
        } else {
            false
        }
    }

    /// Get iterator over integer digits.
    #[inline]
    pub fn integer_iter<'b>(&'b mut self) -> IntegerBytesIterator<'a, 'b, FORMAT> {
        IntegerBytesIterator {
            byte: self,
        }
    }

    /// Get iterator over fraction digits.
    #[inline]
    pub fn fraction_iter<'b>(&'b mut self) -> FractionBytesIterator<'a, 'b, FORMAT> {
        FractionBytesIterator {
            byte: self,
        }
    }

    /// Get iterator over exponent digits.
    #[inline]
    pub fn exponent_iter<'b>(&'b mut self) -> ExponentBytesIterator<'a, 'b, FORMAT> {
        ExponentBytesIterator {
            byte: self,
        }
    }

    /// Get iterator over special floating point values.
    #[inline]
    pub fn special_iter<'b>(&'b mut self) -> SpecialBytesIterator<'a, 'b, FORMAT> {
        SpecialBytesIterator {
            byte: self,
        }
    }

    /// Advance the byte by `N` elements.
    ///
    /// # Safety
    ///
    /// As long as the iterator is at least `N` elements, this
    /// is safe.
    #[inline]
    pub unsafe fn step_by_unchecked(&mut self, count: usize) {
        if Self::IS_CONTIGUOUS {
            // Contiguous, can skip most of these checks.
            debug_assert!(self.as_slice().len() >= count);
        } else {
            // Since this isn't contiguous, it only works
            // if the value is in the range `[0, 1]`.
            // We also need to make sure the **current** value
            // isn't a digit separator.
            let format = NumberFormat::<{ FORMAT }> {};
            debug_assert!(self.as_slice().len() >= count);
            debug_assert!(count == 0 || count == 1);
            debug_assert!(
                count == 0 || self.slc.get(self.index) != Some(&format.digit_separator())
            );
        }
        self.index += count;
        if !Self::IS_CONTIGUOUS {
            // Only increment the count if it's not contiguous, otherwise,
            // this is an unnecessary performance penalty.
            self.count += count;
        }
    }

    /// Advance the byte by 1 element.
    ///
    /// # Safety
    ///
    /// Safe as long as the iterator is not empty.
    #[inline]
    pub unsafe fn step_unchecked(&mut self) {
        debug_assert!(!self.as_slice().is_empty());
        // SAFETY: safe if `self.index < self.length()`.
        unsafe { self.step_by_unchecked(1) };
    }
}

// ITERATOR HELPERS
// ----------------

/// Create skip iterator definition.
macro_rules! skip_iterator {
    ($iterator:ident, $doc:literal) => {
        #[doc = $doc]
        pub struct $iterator<'a: 'b, 'b, const FORMAT: u128> {
            /// The internal byte object for the skip iterator.
            byte: &'b mut Bytes<'a, FORMAT>,
        }
    };
}

macro_rules! is_digit_separator {
    ($format:ident) => {
        /// Determine if the character is a digit separator.
        pub const fn is_digit_separator(&self, value: u8) -> bool {
            let format = NumberFormat::<{ $format }> {};
            let digit_separator = format.digit_separator();
            if digit_separator == 0 {
                // Check at compile time if we have an invalid digit separator.
                // b'\x00', or the NUL character, is this invalid value.
                false
            } else {
                value == digit_separator
            }
        }
    };
}

/// Create impl block for skip iterator.
macro_rules! skip_iterator_impl {
    ($iterator:ident, $radix_cb:ident) => {
        impl<'a: 'b, 'b, const FORMAT: u128> $iterator<'a, 'b, FORMAT> {
            is_digit_separator!(FORMAT);

            /// Determine if the character is a digit.
            pub const fn is_digit(&self, value: u8) -> bool {
                let format = NumberFormat::<{ FORMAT }> {};
                char_is_digit_const(value, format.$radix_cb())
            }
        }
    };
}

/// Create impl Iterator block for skip iterator.
macro_rules! skip_iterator_iterator_impl {
    ($iterator:ident) => {
        impl<'a: 'b, 'b, const FORMAT: u128> Iterator for $iterator<'a, 'b, FORMAT> {
            type Item = &'a u8;

            #[inline]
            fn next(&mut self) -> Option<Self::Item> {
                // Peek will handle everything properly internally.
                let value = self.peek()?;
                // Increment the index so we know not to re-fetch it.
                self.byte.index += 1;
                if !Self::IS_CONTIGUOUS {
                    // Only increment the count if it's not contiguous, otherwise,
                    // this is an unnecessary performance penalty.
                    self.byte.count += 1;
                }
                Some(value)
            }
        }
    };
}

/// Create base methods for the ByteIter block of a skip iterator.
macro_rules! skip_iterator_byteiter_base {
    ($format:ident, $mask:ident) => {
        // It's contiguous if we don't skip over any values.
        // IE, the digit separator flags for the iterator over
        // the digits doesn't skip any values.
        const IS_CONTIGUOUS: bool = $format & flags::$mask == 0;

        #[inline]
        fn as_ptr(&self) -> *const u8 {
            self.byte.as_ptr()
        }

        #[inline]
        fn as_slice(&self) -> &'a [u8] {
            self.byte.as_slice()
        }

        #[inline]
        fn length(&self) -> usize {
            self.byte.length()
        }

        #[inline]
        fn cursor(&self) -> usize {
            self.byte.cursor()
        }

        #[inline]
        unsafe fn set_cursor(&mut self, index: usize) {
            debug_assert!(index <= self.length());
            // SAFETY: safe if `index <= self.length()`.
            unsafe { self.byte.set_cursor(index) };
        }

        #[inline]
        fn current_count(&self) -> usize {
            self.byte.current_count()
        }

        #[inline]
        fn is_consumed(&mut self) -> bool {
            self.peek().is_none()
        }

        #[inline]
        fn is_done(&self) -> bool {
            self.byte.is_done()
        }

        #[inline]
        fn is_contiguous(&self) -> bool {
            Self::IS_CONTIGUOUS
        }

        #[inline]
        unsafe fn peek_unchecked(&mut self) -> <Self as Iterator>::Item {
            self.peek().unwrap()
        }

        #[inline]
        unsafe fn read_unchecked<V>(&self) -> V {
            debug_assert!(self.as_slice().len() >= mem::size_of::<V>());
            // SAFETY: safe as long as the slice has at least count elements.
            unsafe { self.byte.read_unchecked() }
        }

        #[inline]
        fn read<V>(&self) -> Option<V> {
            self.byte.read()
        }

        #[inline]
        unsafe fn step_by_unchecked(&mut self, count: usize) {
            debug_assert!(self.as_slice().len() >= count);
            // SAFETY: safe as long as `slc.len() >= count`.
            unsafe { self.byte.step_by_unchecked(count) }
        }
    };
}

/// Create impl ByteIter block for skip iterator.
macro_rules! skip_iterator_byteiter_impl {
    ($iterator:ident, $mask:ident, $i:ident, $l:ident, $t:ident, $c:ident) => {
        impl<'a: 'b, 'b, const FORMAT: u128> BytesIter<'a> for $iterator<'a, 'b, FORMAT> {
            skip_iterator_byteiter_base!(FORMAT, $mask);

            /// Peek the next value of the iterator, without consuming it.
            #[inline]
            fn peek(&mut self) -> Option<<Self as Iterator>::Item> {
                let format = NumberFormat::<{ FORMAT }> {};
                const IL: u128 = flags::$i | flags::$l;
                const IT: u128 = flags::$i | flags::$t;
                const LT: u128 = flags::$l | flags::$t;
                const ILT: u128 = flags::$i | flags::$l | flags::$t;
                const IC: u128 = flags::$i | flags::$c;
                const LC: u128 = flags::$l | flags::$c;
                const TC: u128 = flags::$t | flags::$c;
                const ILC: u128 = IL | flags::$c;
                const ITC: u128 = IT | flags::$c;
                const LTC: u128 = LT | flags::$c;
                const ILTC: u128 = ILT | flags::$c;

                match format.digit_separator_flags() & flags::$mask {
                    0 => peek_noskip!(self),
                    flags::$i => peek_i!(self),
                    flags::$l => peek_l!(self),
                    flags::$t => peek_t!(self),
                    IL => peek_il!(self),
                    IT => peek_it!(self),
                    LT => peek_lt!(self),
                    ILT => peek_ilt!(self),
                    IC => peek_ic!(self),
                    LC => peek_lc!(self),
                    TC => peek_tc!(self),
                    ILC => peek_ilc!(self),
                    ITC => peek_itc!(self),
                    LTC => peek_ltc!(self),
                    ILTC => peek_iltc!(self),
                    _ => unreachable!(),
                }
            }
        }
    };
}

// INTEGER DIGITS ITERATOR
// -----------------------

skip_iterator!(IntegerBytesIterator, "Iterator that skips over digit separators in the integer.");
skip_iterator_impl!(IntegerBytesIterator, mantissa_radix);
skip_iterator_iterator_impl!(IntegerBytesIterator);
skip_iterator_byteiter_impl!(
    IntegerBytesIterator,
    INTEGER_DIGIT_SEPARATOR_FLAG_MASK,
    INTEGER_INTERNAL_DIGIT_SEPARATOR,
    INTEGER_LEADING_DIGIT_SEPARATOR,
    INTEGER_TRAILING_DIGIT_SEPARATOR,
    INTEGER_CONSECUTIVE_DIGIT_SEPARATOR
);

// FRACTION DIGITS ITERATOR
// ------------------------

skip_iterator!(FractionBytesIterator, "Iterator that skips over digit separators in the fraction.");
skip_iterator_impl!(FractionBytesIterator, mantissa_radix);
skip_iterator_iterator_impl!(FractionBytesIterator);
skip_iterator_byteiter_impl!(
    FractionBytesIterator,
    FRACTION_DIGIT_SEPARATOR_FLAG_MASK,
    FRACTION_INTERNAL_DIGIT_SEPARATOR,
    FRACTION_LEADING_DIGIT_SEPARATOR,
    FRACTION_TRAILING_DIGIT_SEPARATOR,
    FRACTION_CONSECUTIVE_DIGIT_SEPARATOR
);

// EXPONENT DIGITS ITERATOR
// ------------------------

skip_iterator!(ExponentBytesIterator, "Iterator that skips over digit separators in the exponent.");
skip_iterator_impl!(ExponentBytesIterator, exponent_radix);
skip_iterator_iterator_impl!(ExponentBytesIterator);
skip_iterator_byteiter_impl!(
    ExponentBytesIterator,
    EXPONENT_DIGIT_SEPARATOR_FLAG_MASK,
    EXPONENT_INTERNAL_DIGIT_SEPARATOR,
    EXPONENT_LEADING_DIGIT_SEPARATOR,
    EXPONENT_TRAILING_DIGIT_SEPARATOR,
    EXPONENT_CONSECUTIVE_DIGIT_SEPARATOR
);

// SPECIAL DIGITS ITERATOR
// -----------------------

skip_iterator!(
    SpecialBytesIterator,
    "Iterator that skips over digit separators in special floats."
);
skip_iterator_iterator_impl!(SpecialBytesIterator);

impl<'a: 'b, 'b, const FORMAT: u128> SpecialBytesIterator<'a, 'b, FORMAT> {
    is_digit_separator!(FORMAT);
}

impl<'a: 'b, 'b, const FORMAT: u128> BytesIter<'a> for SpecialBytesIterator<'a, 'b, FORMAT> {
    skip_iterator_byteiter_base!(FORMAT, SPECIAL_DIGIT_SEPARATOR);

    /// Peek the next value of the iterator, without consuming it.
    #[inline]
    fn peek(&mut self) -> Option<<Self as Iterator>::Item> {
        let format = NumberFormat::<{ FORMAT }> {};
        if format.special_digit_separator() {
            peek_iltc!(self)
        } else {
            peek_noskip!(self)
        }
    }
}