proctor-libc 0.3.0

Safe, Rust-idiomatic equivalents of libc functions
Documentation
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
//! Safe equivalents of functions declared in C's `stdlib.h` header.

use num_bigint::BigUint;

/// An integer conversion error.
#[derive(Clone, Copy, Debug, Eq, PartialEq)]
pub enum StrtoIntError {
    /// The requested base is not supported.
    InvalidBase,
    /// The converted value is outside the range of the return type.
    OutOfRange,
}

/// A floating-point conversion error.
#[derive(Clone, Copy, Debug, Eq, PartialEq)]
pub enum StrtoFloatError {
    /// The converted value overflowed or underflowed the return type.
    OutOfRange,
}

const F32_FORMAT: FloatFormat = FloatFormat {
    precision: 24,
    fraction_bits: 23,
    exponent_bits: 8,
    exponent_bias: 127,
    minimum_exponent: -126,
    maximum_exponent: 127,
    minimum_decimal_order: -45,
    maximum_decimal_order: 38,
};
const F64_FORMAT: FloatFormat = FloatFormat {
    precision: 53,
    fraction_bits: 52,
    exponent_bits: 11,
    exponent_bias: 1023,
    minimum_exponent: -1022,
    maximum_exponent: 1023,
    minimum_decimal_order: -324,
    maximum_decimal_order: 308,
};
const F128_FORMAT: FloatFormat = FloatFormat {
    precision: 113,
    fraction_bits: 112,
    exponent_bits: 15,
    exponent_bias: 16383,
    minimum_exponent: -16382,
    maximum_exponent: 16383,
    minimum_decimal_order: -4966,
    maximum_decimal_order: 4932,
};

#[derive(Clone, Copy)]
struct FloatFormat {
    precision: u32,
    fraction_bits: u32,
    exponent_bits: u32,
    exponent_bias: i32,
    minimum_exponent: i64,
    maximum_exponent: i64,
    minimum_decimal_order: i64,
    maximum_decimal_order: i64,
}

enum FloatSubject {
    None,
    Infinity {
        negative: bool,
        end: usize,
    },
    Nan {
        negative: bool,
        end: usize,
    },
    Finite {
        negative: bool,
        digits: Vec<u8>,
        scale: FloatScale,
        end: usize,
    },
}

enum FloatScale {
    Decimal(i64),
    Binary(i64),
}

struct Mantissa {
    digits: Vec<u8>,
    saw_digit: bool,
    fraction_digits: usize,
    trailing_zeros: usize,
    end: usize,
}

/// Converts the initial floating-point number in `buf` to an `f64`.
pub fn atof(buf: &[i8]) -> f64 {
    strtod(buf).0.0
}

/// Converts the initial floating-point number in `buf` to an `f64`.
///
/// Returns the converted value, the unconsumed suffix, and the conversion
/// status. `StrtoFloatError::OutOfRange` is the only error variant and reports
/// overflow or inexact underflow.
pub fn strtod(buf: &[i8]) -> ((f64, &[i8]), Result<(), StrtoFloatError>) {
    let ((bits, suffix), status) = strto_float_bits(buf, F64_FORMAT);
    ((f64::from_bits(bits as u64), suffix), status)
}

/// Converts the initial floating-point number in `buf` to an `f64`.
///
/// Returns the converted value, the mutable unconsumed suffix, and the
/// conversion status. `StrtoFloatError::OutOfRange` is the only error variant
/// and reports overflow or inexact underflow.
pub fn strtod_mut(buf: &mut [i8]) -> ((f64, &mut [i8]), Result<(), StrtoFloatError>) {
    let buf_len = buf.len();
    let ((value, suffix), status) = strtod(buf);
    let suffix_start = buf_len - suffix.len();
    ((value, &mut buf[suffix_start..]), status)
}

/// Converts the initial floating-point number in `buf` to an `f32`.
///
/// Returns the converted value, the unconsumed suffix, and the conversion
/// status. `StrtoFloatError::OutOfRange` is the only error variant and reports
/// overflow or inexact underflow.
pub fn strtof(buf: &[i8]) -> ((f32, &[i8]), Result<(), StrtoFloatError>) {
    let ((bits, suffix), status) = strto_float_bits(buf, F32_FORMAT);
    ((f32::from_bits(bits as u32), suffix), status)
}

/// Converts the initial floating-point number in `buf` to an `f32`.
///
/// Returns the converted value, the mutable unconsumed suffix, and the
/// conversion status. `StrtoFloatError::OutOfRange` is the only error variant
/// and reports overflow or inexact underflow.
pub fn strtof_mut(buf: &mut [i8]) -> ((f32, &mut [i8]), Result<(), StrtoFloatError>) {
    let buf_len = buf.len();
    let ((value, suffix), status) = strtof(buf);
    let suffix_start = buf_len - suffix.len();
    ((value, &mut buf[suffix_start..]), status)
}

/// Converts the initial floating-point number in `buf` to [`struct@f128::f128`].
///
/// Returns the converted value, the unconsumed suffix, and the conversion
/// status. `StrtoFloatError::OutOfRange` is the only error variant and reports
/// overflow or inexact underflow.
pub fn strtold(buf: &[i8]) -> ((f128::f128, &[i8]), Result<(), StrtoFloatError>) {
    let ((bits, suffix), status) = strto_float_bits(buf, F128_FORMAT);
    ((f128_from_bits(bits), suffix), status)
}

/// Converts the initial floating-point number in `buf` to [`struct@f128::f128`].
///
/// Returns the converted value, the mutable unconsumed suffix, and the
/// conversion status. `StrtoFloatError::OutOfRange` is the only error variant
/// and reports overflow or inexact underflow.
pub fn strtold_mut(buf: &mut [i8]) -> ((f128::f128, &mut [i8]), Result<(), StrtoFloatError>) {
    let buf_len = buf.len();
    let ((value, suffix), status) = strtold(buf);
    let suffix_start = buf_len - suffix.len();
    ((value, &mut buf[suffix_start..]), status)
}

fn strto_float_bits(
    buf: &[i8],
    format: FloatFormat,
) -> ((u128, &[i8]), Result<(), StrtoFloatError>) {
    match scan_float_subject(buf) {
        FloatSubject::None => ((0, buf), Ok(())),
        FloatSubject::Infinity { negative, end } => {
            ((infinity_bits(format, negative), &buf[end..]), Ok(()))
        }
        FloatSubject::Nan { negative, end } => ((nan_bits(format, negative), &buf[end..]), Ok(())),
        FloatSubject::Finite {
            negative,
            digits,
            scale,
            end,
        } => {
            if digits.is_empty() {
                return ((zero_bits(format, negative), &buf[end..]), Ok(()));
            }

            let (bits, out_of_range) = convert_finite_float(negative, &digits, scale, format);
            let status = if out_of_range {
                Err(StrtoFloatError::OutOfRange)
            } else {
                Ok(())
            };
            ((bits, &buf[end..]), status)
        }
    }
}

fn scan_float_subject(buf: &[i8]) -> FloatSubject {
    let limit = buf.iter().position(|&byte| byte == 0).unwrap_or(buf.len());
    let mut index = 0;
    while index < limit && is_c_whitespace(buf[index]) {
        index += 1;
    }

    let negative = match buf.get(index).copied() {
        Some(byte) if byte == b'-' as i8 => {
            index += 1;
            true
        }
        Some(byte) if byte == b'+' as i8 => {
            index += 1;
            false
        }
        _ => false,
    };
    let number_start = index;

    if starts_ignore_ascii_case(buf, index, limit, b"inf") {
        index += 3;
        if starts_ignore_ascii_case(buf, index, limit, b"inity") {
            index += 5;
        }
        return FloatSubject::Infinity {
            negative,
            end: index,
        };
    }

    if starts_ignore_ascii_case(buf, index, limit, b"nan") {
        index += 3;
        if buf.get(index).copied() == Some(b'(' as i8) {
            let mut payload_end = index + 1;
            while payload_end < limit && is_nan_character(buf[payload_end]) {
                payload_end += 1;
            }
            if buf.get(payload_end).copied() == Some(b')' as i8) {
                index = payload_end + 1;
            }
        }
        return FloatSubject::Nan {
            negative,
            end: index,
        };
    }

    if index + 2 <= limit
        && buf.get(index).copied() == Some(b'0' as i8)
        && matches!(buf.get(index + 1).copied(), Some(byte) if byte == b'x' as i8 || byte == b'X' as i8)
    {
        let mantissa = scan_mantissa(buf, index + 2, limit, 16);
        if mantissa.saw_digit {
            let (end, exponent) = scan_float_exponent(buf, mantissa.end, limit, b'p', b'P');
            let scale = adjust_scale(
                exponent,
                mantissa.fraction_digits,
                mantissa.trailing_zeros,
                4,
            );
            return FloatSubject::Finite {
                negative,
                digits: mantissa.digits,
                scale: FloatScale::Binary(scale),
                end,
            };
        }
    }

    let mantissa = scan_mantissa(buf, number_start, limit, 10);
    if !mantissa.saw_digit {
        return FloatSubject::None;
    }
    let (end, exponent) = scan_float_exponent(buf, mantissa.end, limit, b'e', b'E');
    FloatSubject::Finite {
        negative,
        digits: mantissa.digits,
        scale: FloatScale::Decimal(adjust_scale(
            exponent,
            mantissa.fraction_digits,
            mantissa.trailing_zeros,
            1,
        )),
        end,
    }
}

fn scan_mantissa(buf: &[i8], start: usize, limit: usize, radix: u32) -> Mantissa {
    let mut digits = Vec::new();
    let mut saw_digit = false;
    let mut fraction_digits = 0;
    let mut pending_zeros = 0;
    let mut index = start;
    let mut after_point = false;

    while index < limit {
        if !after_point && buf[index] == b'.' as i8 {
            after_point = true;
            index += 1;
            continue;
        }
        let Some(digit) = digit_value(buf[index]).filter(|&digit| digit < radix) else {
            break;
        };
        saw_digit = true;
        if digit == 0 {
            if !digits.is_empty() {
                pending_zeros += 1;
            }
        } else {
            digits.extend(std::iter::repeat_n(0, pending_zeros));
            pending_zeros = 0;
            digits.push(digit as u8);
        }
        if after_point {
            fraction_digits += 1;
        }
        index += 1;
    }

    Mantissa {
        digits,
        saw_digit,
        fraction_digits,
        trailing_zeros: pending_zeros,
        end: index,
    }
}

fn scan_float_exponent(
    buf: &[i8],
    marker: usize,
    limit: usize,
    lower: u8,
    upper: u8,
) -> (usize, i64) {
    if marker >= limit || !matches!(buf[marker] as u8, byte if byte == lower || byte == upper) {
        return (marker, 0);
    }

    let mut index = marker + 1;
    let negative = match buf.get(index).copied() {
        Some(byte) if byte == b'-' as i8 => {
            index += 1;
            true
        }
        Some(byte) if byte == b'+' as i8 => {
            index += 1;
            false
        }
        _ => false,
    };
    let first_digit = index;
    let mut exponent = 0_i64;
    while index < limit {
        let byte = buf[index] as u8;
        if !byte.is_ascii_digit() {
            break;
        }
        exponent = exponent
            .saturating_mul(10)
            .saturating_add(i64::from(byte - b'0'));
        index += 1;
    }
    if index == first_digit {
        return (marker, 0);
    }
    if negative {
        exponent = -exponent;
    }
    (index, exponent)
}

fn convert_finite_float(
    negative: bool,
    digits: &[u8],
    scale: FloatScale,
    format: FloatFormat,
) -> (u128, bool) {
    let (numerator, denominator, binary_exponent) = match scale {
        FloatScale::Decimal(decimal_exponent) => {
            let decimal_order = usize_to_i64(digits.len())
                .saturating_sub(1)
                .saturating_add(decimal_exponent);
            if decimal_order > format.maximum_decimal_order {
                return (infinity_bits(format, negative), true);
            }
            if decimal_order < format.minimum_decimal_order.saturating_sub(1) {
                return (zero_bits(format, negative), true);
            }

            let significand = BigUint::from_radix_be(digits, 10).expect("decimal digits are valid");
            if decimal_exponent >= 0 {
                let five = BigUint::from(5_u8).pow(decimal_exponent as u32);
                (significand * five, BigUint::from(1_u8), decimal_exponent)
            } else {
                let magnitude = decimal_exponent.unsigned_abs() as u32;
                (
                    significand,
                    BigUint::from(5_u8).pow(magnitude),
                    decimal_exponent,
                )
            }
        }
        FloatScale::Binary(binary_exponent) => {
            let leading_digit_exponent = i64::from(7 - digits[0].leading_zeros());
            let exponent = usize_to_i64(digits.len().saturating_sub(1))
                .saturating_mul(4)
                .saturating_add(leading_digit_exponent)
                .saturating_add(binary_exponent);
            let minimum_subnormal_exponent =
                format.minimum_exponent - i64::from(format.precision - 1);
            if exponent > format.maximum_exponent {
                return (infinity_bits(format, negative), true);
            }
            if exponent < minimum_subnormal_exponent - 1 {
                return (zero_bits(format, negative), true);
            }

            (
                BigUint::from_radix_be(digits, 16).expect("hexadecimal digits are valid"),
                BigUint::from(1_u8),
                binary_exponent,
            )
        }
    };

    encode_ratio(negative, &numerator, &denominator, binary_exponent, format)
}

fn encode_ratio(
    negative: bool,
    numerator: &BigUint,
    denominator: &BigUint,
    binary_exponent: i64,
    format: FloatFormat,
) -> (u128, bool) {
    let ratio_exponent = floor_log2_ratio(numerator, denominator);
    let mut exponent = ratio_exponent.saturating_add(binary_exponent);
    let minimum_subnormal_exponent = format.minimum_exponent - i64::from(format.precision - 1);

    if exponent >= format.minimum_exponent {
        let shift = i64::from(format.precision - 1) - ratio_exponent;
        let (mut significand, _) = round_ratio(numerator, denominator, shift);
        if significand.bits() > u64::from(format.precision) {
            significand >>= 1_usize;
            exponent += 1;
        }
        if exponent > format.maximum_exponent {
            return (infinity_bits(format, negative), true);
        }

        let significand = biguint_to_u128(&significand);
        let implicit_bit = 1_u128 << (format.precision - 1);
        let exponent_field = (exponent + i64::from(format.exponent_bias)) as u128;
        let bits = sign_bits(format, negative)
            | (exponent_field << format.fraction_bits)
            | (significand - implicit_bit);
        return (bits, false);
    }

    let shift = binary_exponent.saturating_sub(minimum_subnormal_exponent);
    let (significand, inexact) = round_ratio(numerator, denominator, shift);
    let significand = biguint_to_u128(&significand);
    let minimum_normal_significand = 1_u128 << (format.precision - 1);
    if significand >= minimum_normal_significand {
        let bits = sign_bits(format, negative) | (1_u128 << format.fraction_bits);
        return (bits, inexact);
    }

    (sign_bits(format, negative) | significand, inexact)
}

fn floor_log2_ratio(numerator: &BigUint, denominator: &BigUint) -> i64 {
    let exponent = (numerator.bits() as i64) - (denominator.bits() as i64);
    let below_power = if exponent >= 0 {
        numerator < &(denominator << exponent as usize)
    } else {
        &(numerator << exponent.unsigned_abs() as usize) < denominator
    };
    if below_power { exponent - 1 } else { exponent }
}

fn round_ratio(numerator: &BigUint, denominator: &BigUint, shift: i64) -> (BigUint, bool) {
    let (scaled_numerator, scaled_denominator) = if shift >= 0 {
        (numerator << shift as usize, denominator.clone())
    } else {
        (
            numerator.clone(),
            denominator << shift.unsigned_abs() as usize,
        )
    };
    let mut quotient = &scaled_numerator / &scaled_denominator;
    let remainder = &scaled_numerator % &scaled_denominator;
    let inexact = remainder.bits() != 0;
    let doubled_remainder = &remainder << 1_usize;
    if doubled_remainder > scaled_denominator
        || (doubled_remainder == scaled_denominator && quotient.bit(0))
    {
        quotient += 1_u8;
    }
    (quotient, inexact)
}

fn biguint_to_u128(value: &BigUint) -> u128 {
    let digits = value.to_u64_digits();
    u128::from(digits.first().copied().unwrap_or(0))
        | (u128::from(digits.get(1).copied().unwrap_or(0)) << 64)
}

fn zero_bits(format: FloatFormat, negative: bool) -> u128 {
    sign_bits(format, negative)
}

fn infinity_bits(format: FloatFormat, negative: bool) -> u128 {
    sign_bits(format, negative) | (maximum_exponent_field(format) << format.fraction_bits)
}

fn nan_bits(format: FloatFormat, negative: bool) -> u128 {
    infinity_bits(format, negative) | (1_u128 << (format.fraction_bits - 1))
}

fn sign_bits(format: FloatFormat, negative: bool) -> u128 {
    u128::from(negative) << (format.fraction_bits + format.exponent_bits)
}

fn maximum_exponent_field(format: FloatFormat) -> u128 {
    (1_u128 << format.exponent_bits) - 1
}

fn f128_from_bits(bits: u128) -> f128::f128 {
    // SAFETY: `f128::f128` is a `repr(C)` wrapper around `[u8; 16]`, and all
    // IEEE binary128 bit patterns are valid values of that type.
    unsafe { std::mem::transmute(bits.to_ne_bytes()) }
}

fn starts_ignore_ascii_case(buf: &[i8], start: usize, limit: usize, expected: &[u8]) -> bool {
    start + expected.len() <= limit
        && buf[start..start + expected.len()]
            .iter()
            .zip(expected)
            .all(|(&actual, &expected)| (actual as u8).eq_ignore_ascii_case(&expected))
}

fn is_nan_character(byte: i8) -> bool {
    matches!(byte as u8, b'0'..=b'9' | b'a'..=b'z' | b'A'..=b'Z' | b'_')
}

fn usize_to_i64(value: usize) -> i64 {
    i64::try_from(value).unwrap_or(i64::MAX)
}

fn adjust_scale(
    exponent: i64,
    fractional_digits: usize,
    trailing_zeros: usize,
    radix_bits: i64,
) -> i64 {
    if fractional_digits >= trailing_zeros {
        let adjustment =
            usize_to_i64(fractional_digits - trailing_zeros).saturating_mul(radix_bits);
        exponent.saturating_sub(adjustment)
    } else {
        let adjustment =
            usize_to_i64(trailing_zeros - fractional_digits).saturating_mul(radix_bits);
        exponent.saturating_add(adjustment)
    }
}

enum IntegerSubject {
    None,
    Digits {
        negative: bool,
        radix: u32,
        start: usize,
    },
}

struct IntegerMagnitude {
    magnitude: u64,
    end: usize,
    out_of_range: bool,
}

/// Converts the initial decimal integer in `buf` to an `i32`.
pub fn atoi(buf: &[i8]) -> i32 {
    strtol(buf, 10).0.0 as i32
}

/// Converts the initial decimal integer in `buf` to an `i64`.
pub fn atol(buf: &[i8]) -> i64 {
    strtol(buf, 10).0.0
}

/// Converts the initial integer in `buf` using `base`.
///
/// Returns the converted value, the unconsumed suffix, and the conversion
/// status. `StrtoIntError::InvalidBase` and `StrtoIntError::OutOfRange` are the
/// only error variants; they report an unsupported base and overflow,
/// respectively.
pub fn strtol(buf: &[i8], base: i32) -> ((i64, &[i8]), Result<(), StrtoIntError>) {
    let subject = match scan_integer_subject(buf, base) {
        Ok(subject) => subject,
        Err(error) => return ((0, buf), Err(error)),
    };
    let IntegerSubject::Digits {
        negative,
        radix,
        start,
    } = subject
    else {
        return ((0, buf), Ok(()));
    };
    let limit = if negative {
        i64::MAX as u64 + 1
    } else {
        i64::MAX as u64
    };
    let conversion = convert_integer_magnitude(buf, start, radix, limit);

    if conversion.out_of_range {
        let value = if negative { i64::MIN } else { i64::MAX };
        return (
            (value, &buf[conversion.end..]),
            Err(StrtoIntError::OutOfRange),
        );
    }

    let value = if negative {
        if conversion.magnitude == i64::MAX as u64 + 1 {
            i64::MIN
        } else {
            -(conversion.magnitude as i64)
        }
    } else {
        conversion.magnitude as i64
    };

    ((value, &buf[conversion.end..]), Ok(()))
}

/// Converts the initial integer in `buf` using `base`.
///
/// Returns the converted value, the mutable unconsumed suffix, and the
/// conversion status. `StrtoIntError::InvalidBase` and
/// `StrtoIntError::OutOfRange` are the only error variants; they report an
/// unsupported base and overflow, respectively.
pub fn strtol_mut(buf: &mut [i8], base: i32) -> ((i64, &mut [i8]), Result<(), StrtoIntError>) {
    let buf_len = buf.len();
    let ((value, suffix), status) = strtol(buf, base);
    let suffix_start = buf_len - suffix.len();
    ((value, &mut buf[suffix_start..]), status)
}

/// Converts the initial unsigned integer in `buf` using `base`.
///
/// Returns the converted value, the unconsumed suffix, and the conversion
/// status. `StrtoIntError::InvalidBase` and `StrtoIntError::OutOfRange` are the
/// only error variants; they report an unsupported base and overflow,
/// respectively.
pub fn strtoul(buf: &[i8], base: i32) -> ((u64, &[i8]), Result<(), StrtoIntError>) {
    let subject = match scan_integer_subject(buf, base) {
        Ok(subject) => subject,
        Err(error) => return ((0, buf), Err(error)),
    };
    let IntegerSubject::Digits {
        negative,
        radix,
        start,
    } = subject
    else {
        return ((0, buf), Ok(()));
    };
    let conversion = convert_integer_magnitude(buf, start, radix, u64::MAX);

    if conversion.out_of_range {
        return (
            (u64::MAX, &buf[conversion.end..]),
            Err(StrtoIntError::OutOfRange),
        );
    }

    let value = if negative {
        0_u64.wrapping_sub(conversion.magnitude)
    } else {
        conversion.magnitude
    };

    ((value, &buf[conversion.end..]), Ok(()))
}

/// Converts the initial unsigned integer in `buf` using `base`.
///
/// Returns the converted value, the mutable unconsumed suffix, and the
/// conversion status. `StrtoIntError::InvalidBase` and
/// `StrtoIntError::OutOfRange` are the only error variants; they report an
/// unsupported base and overflow, respectively.
pub fn strtoul_mut(buf: &mut [i8], base: i32) -> ((u64, &mut [i8]), Result<(), StrtoIntError>) {
    let buf_len = buf.len();
    let ((value, suffix), status) = strtoul(buf, base);
    let suffix_start = buf_len - suffix.len();
    ((value, &mut buf[suffix_start..]), status)
}

fn scan_integer_subject(buf: &[i8], base: i32) -> Result<IntegerSubject, StrtoIntError> {
    if base != 0 && !(2..=36).contains(&base) {
        return Err(StrtoIntError::InvalidBase);
    }

    let mut index = 0;
    while index < buf.len() && is_c_whitespace(buf[index]) {
        index += 1;
    }

    let negative = match buf.get(index).copied() {
        Some(byte) if byte == b'-' as i8 => {
            index += 1;
            true
        }
        Some(byte) if byte == b'+' as i8 => {
            index += 1;
            false
        }
        _ => false,
    };

    let radix = if base == 0 {
        if has_hex_prefix(buf, index) {
            index += 2;
            16
        } else if buf.get(index).copied() == Some(b'0' as i8) {
            8
        } else {
            10
        }
    } else {
        let radix = base as u32;
        if radix == 16 && has_hex_prefix(buf, index) {
            index += 2;
        }
        radix
    };

    if !matches!(buf.get(index).copied().and_then(digit_value), Some(digit) if digit < radix) {
        return Ok(IntegerSubject::None);
    }

    Ok(IntegerSubject::Digits {
        negative,
        radix,
        start: index,
    })
}

fn convert_integer_magnitude(buf: &[i8], start: usize, radix: u32, limit: u64) -> IntegerMagnitude {
    let cutoff = limit / u64::from(radix);
    let cutlim = limit % u64::from(radix);
    let mut magnitude = 0_u64;
    let mut out_of_range = false;
    let mut index = start;

    while let Some(digit) = buf.get(index).copied().and_then(digit_value) {
        if digit >= radix {
            break;
        }

        if !out_of_range {
            let digit = u64::from(digit);
            if magnitude > cutoff || (magnitude == cutoff && digit > cutlim) {
                out_of_range = true;
            } else {
                magnitude = magnitude * u64::from(radix) + digit;
            }
        }
        index += 1;
    }

    IntegerMagnitude {
        magnitude,
        end: index,
        out_of_range,
    }
}

fn is_c_whitespace(byte: i8) -> bool {
    matches!(byte as u8, b' ' | b'\t' | b'\n' | b'\x0b' | b'\x0c' | b'\r')
}

fn has_hex_prefix(buf: &[i8], index: usize) -> bool {
    buf.get(index).copied() == Some(b'0' as i8)
        && matches!(buf.get(index + 1).copied(), Some(byte) if byte == b'x' as i8 || byte == b'X' as i8)
        && matches!(
            buf.get(index + 2).copied().and_then(digit_value),
            Some(0..=15)
        )
}

fn digit_value(byte: i8) -> Option<u32> {
    match byte as u8 {
        byte @ b'0'..=b'9' => Some(u32::from(byte - b'0')),
        byte @ b'a'..=b'z' => Some(u32::from(byte - b'a') + 10),
        byte @ b'A'..=b'Z' => Some(u32::from(byte - b'A') + 10),
        _ => None,
    }
}

#[cfg(test)]
mod tests;