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
// Copyright © 2018–2024 Trevor Spiteri

// This library is free software: you can redistribute it and/or
// modify it under the terms of either
//
//   * the Apache License, Version 2.0 or
//   * the MIT License
//
// at your option.
//
// You should have recieved copies of the Apache License and the MIT
// License along with the library. If not, see
// <https://www.apache.org/licenses/LICENSE-2.0> and
// <https://opensource.org/licenses/MIT>.

// Kept trimmed: no underscores at beginning or end of slice
#[derive(Clone, Copy, Debug)]
pub struct DigitsUnds<'a> {
    bytes: &'a [u8],
    digits: usize,
}

impl<'a> DigitsUnds<'a> {
    pub const EMPTY: DigitsUnds<'a> = DigitsUnds::new(&[]);

    pub const fn new(bytes: &'a [u8]) -> DigitsUnds<'a> {
        let mut digits = 0;
        let mut leading_unds = 0;
        let mut trailing_unds = 0;
        let mut rem_bytes = bytes;
        while let Some((&byte, rem)) = rem_bytes.split_first() {
            rem_bytes = rem;

            if byte == b'_' {
                trailing_unds += 1;
            } else {
                if digits == 0 {
                    leading_unds = trailing_unds;
                }
                digits += 1;
                trailing_unds = 0;
            }
        }
        let without_trailing_unds = bytes.split_at(bytes.len() - trailing_unds).0;
        let without_leading_unds = without_trailing_unds.split_at(leading_unds).1;
        DigitsUnds {
            bytes: without_leading_unds,
            digits,
        }
    }

    #[inline]
    pub const fn len(self) -> usize {
        self.digits
    }

    #[inline]
    pub const fn is_empty(self) -> bool {
        self.digits == 0
    }

    pub const fn split_at(self, mid: usize) -> (DigitsUnds<'a>, DigitsUnds<'a>) {
        let mut remaining_digits = mid;
        let mut unds = 0;
        let mut rem_bytes = self.bytes;
        while let Some((&byte, rem)) = rem_bytes.split_first() {
            rem_bytes = rem;

            if byte == b'_' {
                unds += 1;
            } else {
                remaining_digits -= 1;
                if remaining_digits == 0 {
                    break;
                }
            }
        }
        if remaining_digits > 0 {
            panic!("index out of bounds");
        }
        let first = DigitsUnds {
            bytes: self.bytes.split_at(mid + unds).0,
            digits: mid,
        };

        // skip over underscores between first part and last part
        while let Some((&b'_', rem)) = rem_bytes.split_first() {
            rem_bytes = rem;
        }
        (
            first,
            DigitsUnds {
                bytes: rem_bytes,
                digits: self.digits - mid,
            },
        )
    }

    #[inline]
    pub const fn split_first(self) -> Option<(u8, DigitsUnds<'a>)> {
        let Some((&first, mut rem_bytes)) = self.bytes.split_first() else {
            return None;
        };

        // first byte is never underscore
        debug_assert!(first != b'_');

        // skip over underscores between first digit and last part
        while let Some((&b'_', rem)) = rem_bytes.split_first() {
            rem_bytes = rem;
        }
        Some((
            first,
            DigitsUnds {
                bytes: rem_bytes,
                digits: self.digits - 1,
            },
        ))
    }

    #[inline]
    const fn split_last(self) -> Option<(u8, DigitsUnds<'a>)> {
        let Some((&last, mut rem_bytes)) = self.bytes.split_last() else {
            return None;
        };

        // last byte is never underscore
        debug_assert!(last != b'_');

        // skip over underscores between first part and last digit
        while let Some((&b'_', rem)) = rem_bytes.split_last() {
            rem_bytes = rem;
        }
        Some((
            last,
            DigitsUnds {
                bytes: rem_bytes,
                digits: self.digits - 1,
            },
        ))
    }

    const fn split_leading_zeros(self) -> (usize, DigitsUnds<'a>) {
        let mut zeros = 0;
        let mut rem = self;
        while let Some((b'0', rest)) = rem.split_first() {
            zeros += 1;
            rem = rest;
        }
        (zeros, rem)
    }

    const fn split_trailing_zeros(self) -> (usize, DigitsUnds<'a>) {
        let mut zeros = 0;
        let mut rem = self;
        while let Some((b'0', rest)) = rem.split_last() {
            zeros += 1;
            rem = rest;
        }
        (zeros, rem)
    }
}

#[derive(Clone, Copy, Debug)]
pub struct DigitsExp<'a> {
    leading_zeros: usize,
    part1: DigitsUnds<'a>,
    part2: DigitsUnds<'a>,
    trailing_zeros: usize,
}

impl<'a> DigitsExp<'a> {
    const EMPTY: DigitsExp<'a> = DigitsExp {
        leading_zeros: 0,
        part1: DigitsUnds::EMPTY,
        part2: DigitsUnds::EMPTY,
        trailing_zeros: 0,
    };

    const fn new1(digits: DigitsUnds<'a>) -> DigitsExp<'a> {
        let (leading_zeros, rest) = digits.split_leading_zeros();
        let (trailing_zeros, rest) = rest.split_trailing_zeros();
        DigitsExp {
            leading_zeros,
            part1: rest,
            part2: DigitsUnds::EMPTY,
            trailing_zeros,
        }
    }

    const fn new2(digits1: DigitsUnds<'a>, digits2: DigitsUnds<'a>) -> DigitsExp<'a> {
        let (mut leading_zeros, mut digits1) = digits1.split_leading_zeros();
        let digits2 = if digits1.is_empty() {
            let (more_leading_zeros, new_digits1) = digits2.split_leading_zeros();
            leading_zeros += more_leading_zeros;
            digits1 = new_digits1;
            DigitsUnds::EMPTY
        } else {
            digits2
        };
        let (mut trailing_zeros, digits2) = digits2.split_trailing_zeros();
        if digits2.is_empty() {
            let (more_trailing_zeros, new_digits1) = digits1.split_trailing_zeros();
            trailing_zeros += more_trailing_zeros;
            digits1 = new_digits1;
        }
        DigitsExp {
            leading_zeros,
            part1: digits1,
            part2: digits2,
            trailing_zeros,
        }
    }

    // exp.unsigned_abs() must fit in usize, and results must have lengths that fit in usize
    pub const fn new_int_frac(
        int: DigitsUnds<'a>,
        frac: DigitsUnds<'a>,
        exp: i32,
    ) -> Option<(DigitsExp<'a>, DigitsExp<'a>)> {
        if int.len() > usize::MAX - frac.len() {
            return None;
        }
        let abs_exp = exp.unsigned_abs() as usize;
        if abs_exp as u32 != exp.unsigned_abs() {
            return None;
        }

        let (mut int, mut frac) = if exp == 0 {
            (DigitsExp::new1(int), DigitsExp::new1(frac))
        } else if exp < 0 {
            if let Some(extra_zeros) = abs_exp.checked_sub(int.len()) {
                let mut frac = DigitsExp::new2(int, frac);
                frac.trailing_zeros = 0;
                if extra_zeros > usize::MAX - frac.len() {
                    return None;
                }
                frac.leading_zeros += extra_zeros;
                (DigitsExp::EMPTY, frac)
            } else {
                let int = int.split_at(int.len() - abs_exp);
                (DigitsExp::new1(int.0), DigitsExp::new2(int.1, frac))
            }
        } else {
            // exp > 0
            if let Some(extra_zeros) = abs_exp.checked_sub(frac.len()) {
                let mut int = DigitsExp::new2(int, frac);
                int.leading_zeros = 0;
                if extra_zeros > usize::MAX - int.len() {
                    return None;
                }
                int.trailing_zeros += extra_zeros;
                (int, DigitsExp::EMPTY)
            } else {
                let frac = frac.split_at(abs_exp);
                (DigitsExp::new2(int, frac.0), DigitsExp::new1(frac.1))
            }
        };
        int.leading_zeros = 0;
        if int.part1.is_empty() && int.part2.is_empty() {
            int.trailing_zeros = 0;
        }
        frac.trailing_zeros = 0;
        if frac.part2.is_empty() && frac.part1.is_empty() {
            frac.leading_zeros = 0;
        }
        Some((int, frac))
    }

    #[inline]
    pub const fn len(self) -> usize {
        self.leading_zeros + self.part1.len() + self.part2.len() + self.trailing_zeros
    }

    #[inline]
    pub const fn is_empty(self) -> bool {
        self.len() == 0
    }

    pub const fn split_at(self, mut mid: usize) -> (DigitsExp<'a>, DigitsExp<'a>) {
        let mut first = DigitsExp::EMPTY;
        let mut last = self;
        if mid == 0 {
            return (first, last);
        }

        if mid < self.leading_zeros {
            (first.leading_zeros, last.leading_zeros) = (mid, self.leading_zeros - mid);
            return (first, last);
        }

        (first.leading_zeros, last.leading_zeros) = (self.leading_zeros, 0);
        mid -= self.leading_zeros;
        if mid == 0 {
            return (first, last);
        }

        if mid < self.part1.len() {
            (first.part1, last.part1) = self.part1.split_at(mid);
            return (first, last);
        }

        first.part1 = self.part1;
        last.part1 = self.part2;
        last.part2 = DigitsUnds::EMPTY;
        mid -= self.part1.len();
        if mid == 0 {
            return (first, last);
        }

        if mid < self.part2.len() {
            (first.part2, last.part1) = self.part2.split_at(mid);
            return (first, last);
        }

        first.part2 = self.part2;
        last.leading_zeros = self.trailing_zeros;
        last.part1 = DigitsUnds::EMPTY;
        last.trailing_zeros = 0;
        mid -= self.part2.len();
        if mid == 0 {
            return (first, last);
        }

        if mid < self.trailing_zeros {
            (first.trailing_zeros, last.leading_zeros) = (mid, self.trailing_zeros - mid);
            return (first, last);
        }

        (first.trailing_zeros, last.leading_zeros) = (self.trailing_zeros, 0);
        mid -= self.trailing_zeros;
        if mid == 0 {
            return (first, last);
        }

        panic!("index out of bounds");
    }

    // no automatic renormalization done after split_first
    #[inline]
    pub const fn split_first(self) -> Option<(u8, DigitsExp<'a>)> {
        if self.leading_zeros > 0 {
            return Some((
                b'0',
                DigitsExp {
                    leading_zeros: self.leading_zeros - 1,
                    ..self
                },
            ));
        }
        if let Some((first, rest)) = self.part1.split_first() {
            return Some((
                first,
                DigitsExp {
                    part1: rest,
                    ..self
                },
            ));
        }
        if let Some((first, rest)) = self.part2.split_first() {
            return Some((
                first,
                DigitsExp {
                    part2: rest,
                    ..self
                },
            ));
        }
        if self.trailing_zeros > 0 {
            return Some((
                b'0',
                DigitsExp {
                    trailing_zeros: self.trailing_zeros - 1,
                    ..self
                },
            ));
        }
        None
    }
}