Skip to main content

core/num/
mod.rs

1//! Numeric traits and functions for the built-in numeric types.
2
3#![stable(feature = "rust1", since = "1.0.0")]
4
5use crate::panic::const_panic;
6use crate::str::FromStr;
7use crate::ub_checks::assert_unsafe_precondition;
8use crate::{ascii, intrinsics, mem};
9
10// FIXME(const-hack): Used because the `?` operator is not allowed in a const context.
11macro_rules! try_opt {
12    ($e:expr) => {
13        match $e {
14            Some(x) => x,
15            None => return None,
16        }
17    };
18}
19
20// Use this when the generated code should differ between signed and unsigned types.
21macro_rules! sign_dependent_expr {
22    (signed ? if signed { $signed_case:expr } if unsigned { $unsigned_case:expr } ) => {
23        $signed_case
24    };
25    (unsigned ? if signed { $signed_case:expr } if unsigned { $unsigned_case:expr } ) => {
26        $unsigned_case
27    };
28}
29
30// These modules are public only for testing.
31#[doc(hidden)]
32#[unstable(
33    feature = "num_internals",
34    reason = "internal routines only exposed for testing",
35    issue = "none"
36)]
37pub mod imp;
38
39#[macro_use]
40mod int_macros; // import int_impl!
41#[macro_use]
42mod uint_macros; // import uint_impl!
43
44mod error;
45#[cfg(not(no_fp_fmt_parse))]
46mod float_parse;
47mod nonzero;
48mod saturating;
49mod traits;
50mod wrapping;
51
52/// 100% perma-unstable
53#[doc(hidden)]
54pub mod niche_types;
55
56#[stable(feature = "int_error_matching", since = "1.55.0")]
57pub use error::IntErrorKind;
58#[stable(feature = "rust1", since = "1.0.0")]
59pub use error::ParseIntError;
60#[stable(feature = "try_from", since = "1.34.0")]
61pub use error::TryFromIntError;
62#[stable(feature = "rust1", since = "1.0.0")]
63#[cfg(not(no_fp_fmt_parse))]
64pub use float_parse::ParseFloatError;
65#[stable(feature = "generic_nonzero", since = "1.79.0")]
66pub use nonzero::NonZero;
67#[unstable(
68    feature = "nonzero_internals",
69    reason = "implementation detail which may disappear or be replaced at any time",
70    issue = "none"
71)]
72pub use nonzero::ZeroablePrimitive;
73#[stable(feature = "signed_nonzero", since = "1.34.0")]
74pub use nonzero::{NonZeroI8, NonZeroI16, NonZeroI32, NonZeroI64, NonZeroI128, NonZeroIsize};
75#[stable(feature = "nonzero", since = "1.28.0")]
76pub use nonzero::{NonZeroU8, NonZeroU16, NonZeroU32, NonZeroU64, NonZeroU128, NonZeroUsize};
77#[stable(feature = "saturating_int_impl", since = "1.74.0")]
78pub use saturating::Saturating;
79#[stable(feature = "rust1", since = "1.0.0")]
80pub use wrapping::Wrapping;
81
82macro_rules! u8_xe_bytes_doc {
83    () => {
84        "
85
86**Note**: This function is meaningless on `u8`. Byte order does not exist as a
87concept for byte-sized integers. This function is only provided in symmetry
88with larger integer types.
89
90"
91    };
92}
93
94macro_rules! i8_xe_bytes_doc {
95    () => {
96        "
97
98**Note**: This function is meaningless on `i8`. Byte order does not exist as a
99concept for byte-sized integers. This function is only provided in symmetry
100with larger integer types. You can cast from and to `u8` using
101[`cast_signed`](u8::cast_signed) and [`cast_unsigned`](Self::cast_unsigned).
102
103"
104    };
105}
106
107macro_rules! usize_isize_to_xe_bytes_doc {
108    () => {
109        "
110
111**Note**: This function returns an array of length 2, 4 or 8 bytes
112depending on the target pointer size.
113
114"
115    };
116}
117
118macro_rules! usize_isize_from_xe_bytes_doc {
119    () => {
120        "
121
122**Note**: This function takes an array of length 2, 4 or 8 bytes
123depending on the target pointer size.
124
125"
126    };
127}
128
129macro_rules! midpoint_impl {
130    ($SelfT:ty, unsigned) => {
131        /// Calculates the midpoint (average) between `self` and `rhs`.
132        ///
133        /// `midpoint(a, b)` is `(a + b) / 2` as if it were performed in a
134        /// sufficiently-large unsigned integral type. This implies that the result is
135        /// always rounded towards zero and that no overflow will ever occur.
136        ///
137        /// # Examples
138        ///
139        /// ```
140        #[doc = concat!("assert_eq!(0", stringify!($SelfT), ".midpoint(4), 2);")]
141        #[doc = concat!("assert_eq!(1", stringify!($SelfT), ".midpoint(4), 2);")]
142        /// ```
143        #[stable(feature = "num_midpoint", since = "1.85.0")]
144        #[rustc_const_stable(feature = "num_midpoint", since = "1.85.0")]
145        #[must_use = "this returns the result of the operation, \
146                      without modifying the original"]
147        #[doc(alias = "average_floor")]
148        #[doc(alias = "average")]
149        #[inline]
150        pub const fn midpoint(self, rhs: $SelfT) -> $SelfT {
151            // Use the well known branchless algorithm from Hacker's Delight to compute
152            // `(a + b) / 2` without overflowing: `((a ^ b) >> 1) + (a & b)`.
153            ((self ^ rhs) >> 1) + (self & rhs)
154        }
155    };
156    ($SelfT:ty, signed) => {
157        /// Calculates the midpoint (average) between `self` and `rhs`.
158        ///
159        /// `midpoint(a, b)` is `(a + b) / 2` as if it were performed in a
160        /// sufficiently-large signed integral type. This implies that the result is
161        /// always rounded towards zero and that no overflow will ever occur.
162        ///
163        /// # Examples
164        ///
165        /// ```
166        #[doc = concat!("assert_eq!(0", stringify!($SelfT), ".midpoint(4), 2);")]
167        #[doc = concat!("assert_eq!((-1", stringify!($SelfT), ").midpoint(2), 0);")]
168        #[doc = concat!("assert_eq!((-7", stringify!($SelfT), ").midpoint(0), -3);")]
169        #[doc = concat!("assert_eq!(0", stringify!($SelfT), ".midpoint(-7), -3);")]
170        #[doc = concat!("assert_eq!(0", stringify!($SelfT), ".midpoint(7), 3);")]
171        /// ```
172        #[stable(feature = "num_midpoint_signed", since = "1.87.0")]
173        #[rustc_const_stable(feature = "num_midpoint_signed", since = "1.87.0")]
174        #[must_use = "this returns the result of the operation, \
175                      without modifying the original"]
176        #[doc(alias = "average_floor")]
177        #[doc(alias = "average_ceil")]
178        #[doc(alias = "average")]
179        #[inline]
180        pub const fn midpoint(self, rhs: Self) -> Self {
181            // Use the well known branchless algorithm from Hacker's Delight to compute
182            // `(a + b) / 2` without overflowing: `((a ^ b) >> 1) + (a & b)`.
183            let t = ((self ^ rhs) >> 1) + (self & rhs);
184            // Except that it fails for integers whose sum is an odd negative number as
185            // their floor is one less than their average. So we adjust the result.
186            t + (if t < 0 { 1 } else { 0 } & (self ^ rhs))
187        }
188    };
189    ($SelfT:ty, $WideT:ty, unsigned) => {
190        /// Calculates the midpoint (average) between `self` and `rhs`.
191        ///
192        /// `midpoint(a, b)` is `(a + b) / 2` as if it were performed in a
193        /// sufficiently-large unsigned integral type. This implies that the result is
194        /// always rounded towards zero and that no overflow will ever occur.
195        ///
196        /// # Examples
197        ///
198        /// ```
199        #[doc = concat!("assert_eq!(0", stringify!($SelfT), ".midpoint(4), 2);")]
200        #[doc = concat!("assert_eq!(1", stringify!($SelfT), ".midpoint(4), 2);")]
201        /// ```
202        #[stable(feature = "num_midpoint", since = "1.85.0")]
203        #[rustc_const_stable(feature = "num_midpoint", since = "1.85.0")]
204        #[must_use = "this returns the result of the operation, \
205                      without modifying the original"]
206        #[doc(alias = "average_floor")]
207        #[doc(alias = "average")]
208        #[inline]
209        pub const fn midpoint(self, rhs: $SelfT) -> $SelfT {
210            ((self as $WideT + rhs as $WideT) / 2) as $SelfT
211        }
212    };
213    ($SelfT:ty, $WideT:ty, signed) => {
214        /// Calculates the midpoint (average) between `self` and `rhs`.
215        ///
216        /// `midpoint(a, b)` is `(a + b) / 2` as if it were performed in a
217        /// sufficiently-large signed integral type. This implies that the result is
218        /// always rounded towards zero and that no overflow will ever occur.
219        ///
220        /// # Examples
221        ///
222        /// ```
223        #[doc = concat!("assert_eq!(0", stringify!($SelfT), ".midpoint(4), 2);")]
224        #[doc = concat!("assert_eq!((-1", stringify!($SelfT), ").midpoint(2), 0);")]
225        #[doc = concat!("assert_eq!((-7", stringify!($SelfT), ").midpoint(0), -3);")]
226        #[doc = concat!("assert_eq!(0", stringify!($SelfT), ".midpoint(-7), -3);")]
227        #[doc = concat!("assert_eq!(0", stringify!($SelfT), ".midpoint(7), 3);")]
228        /// ```
229        #[stable(feature = "num_midpoint_signed", since = "1.87.0")]
230        #[rustc_const_stable(feature = "num_midpoint_signed", since = "1.87.0")]
231        #[must_use = "this returns the result of the operation, \
232                      without modifying the original"]
233        #[doc(alias = "average_floor")]
234        #[doc(alias = "average_ceil")]
235        #[doc(alias = "average")]
236        #[inline]
237        pub const fn midpoint(self, rhs: $SelfT) -> $SelfT {
238            ((self as $WideT + rhs as $WideT) / 2) as $SelfT
239        }
240    };
241}
242
243macro_rules! widening_mul_impl {
244    ($SelfT:ty, $WideT:ty) => {
245        /// Widening multiplication. Computes `self * rhs`, widening to a larger integer.
246        ///
247        /// The returned value is always exact and can never overflow.
248        ///
249        /// Note that this method is semantically equivalent to [`carrying_mul`] with a
250        /// carry of zero, with the latter instead returning a tuple denoting the low and
251        /// high parts of the result. Consider using it instead if you need
252        /// interoperability with other big int helper functions, or if this method isn't
253        /// available for a given type.
254        ///
255        /// [`carrying_mul`]: Self::carrying_mul
256        ///
257        /// # Examples
258        ///
259        /// ```
260        /// #![feature(widening_mul)]
261        ///
262        #[doc = concat!("assert_eq!(", stringify!($SelfT), "::MAX.widening_mul(0_", stringify!($SelfT), "), 0);")]
263        #[doc = concat!("assert_eq!(", stringify!($SelfT), "::MAX.widening_mul(", stringify!($SelfT), "::MAX), ", stringify!($SelfT), "::MAX as ", stringify!($WideT), " * ", stringify!($SelfT), "::MAX as ", stringify!($WideT), ");")]
264        /// ```
265        #[unstable(feature = "widening_mul", issue = "152016")]
266        #[rustc_const_unstable(feature = "widening_mul", issue = "152016")]
267        #[must_use = "this returns the result of the operation, \
268                      without modifying the original"]
269        #[inline]
270        pub const fn widening_mul(self, rhs: Self) -> $WideT {
271            self as $WideT * rhs as $WideT
272        }
273    }
274}
275
276macro_rules! widening_carryless_mul_impl {
277    ($SelfT:ty, $WideT:ty) => {
278        /// Performs a widening carry-less multiplication.
279        ///
280        /// # Examples
281        ///
282        /// ```
283        /// #![feature(uint_carryless_mul)]
284        ///
285        #[doc = concat!("assert_eq!(", stringify!($SelfT), "::MAX.widening_carryless_mul(",
286                                stringify!($SelfT), "::MAX), ", stringify!($WideT), "::MAX / 3);")]
287        /// ```
288        #[rustc_const_unstable(feature = "uint_carryless_mul", issue = "152080")]
289        #[doc(alias = "clmul")]
290        #[unstable(feature = "uint_carryless_mul", issue = "152080")]
291        #[must_use = "this returns the result of the operation, \
292                      without modifying the original"]
293        #[inline]
294        pub const fn widening_carryless_mul(self, rhs: $SelfT) -> $WideT {
295            (self as $WideT).carryless_mul(rhs as $WideT)
296        }
297    }
298}
299
300macro_rules! carrying_carryless_mul_impl {
301    (u128, u256) => {
302        carrying_carryless_mul_impl! { @internal u128 =>
303            pub const fn carrying_carryless_mul(self, rhs: Self, carry: Self) -> (Self, Self) {
304                let x0 = self as u64;
305                let x1 = (self >> 64) as u64;
306                let y0 = rhs as u64;
307                let y1 = (rhs >> 64) as u64;
308
309                let z0 = u64::widening_carryless_mul(x0, y0);
310                let z2 = u64::widening_carryless_mul(x1, y1);
311
312                // The grade school algorithm would compute:
313                // z1 = x0y1 ^ x1y0
314
315                // Instead, Karatsuba first computes:
316                let z3 = u64::widening_carryless_mul(x0 ^ x1, y0 ^ y1);
317                // Since it distributes over XOR,
318                // z3 == x0y0 ^ x0y1 ^ x1y0 ^ x1y1
319                //       |--|   |---------|   |--|
320                //    ==  z0  ^     z1      ^  z2
321                // so we can compute z1 as
322                let z1 = z3 ^ z0 ^ z2;
323
324                let lo = z0 ^ (z1 << 64);
325                let hi = z2 ^ (z1 >> 64);
326
327                (lo ^ carry, hi)
328            }
329        }
330    };
331    ($SelfT:ty, $WideT:ty) => {
332        carrying_carryless_mul_impl! { @internal $SelfT =>
333            pub const fn carrying_carryless_mul(self, rhs: Self, carry: Self) -> (Self, Self) {
334                // Can't use widening_carryless_mul because it's not implemented for usize.
335                let p = (self as $WideT).carryless_mul(rhs as $WideT);
336
337                let lo = (p as $SelfT);
338                let hi = (p  >> Self::BITS) as $SelfT;
339
340                (lo ^ carry, hi)
341            }
342        }
343    };
344    (@internal $SelfT:ty => $($fn:tt)*) => {
345        /// Calculates the "full carryless multiplication" without the possibility to overflow.
346        ///
347        /// This returns the low-order (wrapping) bits and the high-order (overflow) bits
348        /// of the result as two separate values, in that order.
349        ///
350        /// # Examples
351        ///
352        /// Please note that this example is shared among integer types, which is why `u8` is used.
353        ///
354        /// ```
355        /// #![feature(uint_carryless_mul)]
356        ///
357        /// assert_eq!(0b1000_0000u8.carrying_carryless_mul(0b1000_0000, 0b0000), (0, 0b0100_0000));
358        /// assert_eq!(0b1000_0000u8.carrying_carryless_mul(0b1000_0000, 0b1111), (0b1111, 0b0100_0000));
359        #[doc = concat!("assert_eq!(",
360            stringify!($SelfT), "::MAX.carrying_carryless_mul(", stringify!($SelfT), "::MAX, ", stringify!($SelfT), "::MAX), ",
361            "(!(", stringify!($SelfT), "::MAX / 3), ", stringify!($SelfT), "::MAX / 3));"
362        )]
363        /// ```
364        #[rustc_const_unstable(feature = "uint_carryless_mul", issue = "152080")]
365        #[doc(alias = "clmul")]
366        #[unstable(feature = "uint_carryless_mul", issue = "152080")]
367        #[must_use = "this returns the result of the operation, \
368                      without modifying the original"]
369        #[inline]
370        $($fn)*
371    }
372}
373
374impl i8 {
375    int_impl! {
376        Self = i8,
377        ActualT = i8,
378        UnsignedT = u8,
379        BITS = 8,
380        BITS_MINUS_ONE = 7,
381        Min = -128,
382        Max = 127,
383        rot = 2,
384        rot_op = "-0x7e",
385        rot_result = "0xa",
386        swap_op = "0x12",
387        swapped = "0x12",
388        reversed = "0x48",
389        le_bytes = "[0x12]",
390        be_bytes = "[0x12]",
391        to_xe_bytes_doc = i8_xe_bytes_doc!(),
392        from_xe_bytes_doc = i8_xe_bytes_doc!(),
393        bound_condition = "",
394    }
395    midpoint_impl! { i8, i16, signed }
396    widening_mul_impl! { i8, i16 }
397}
398
399impl i16 {
400    int_impl! {
401        Self = i16,
402        ActualT = i16,
403        UnsignedT = u16,
404        BITS = 16,
405        BITS_MINUS_ONE = 15,
406        Min = -32768,
407        Max = 32767,
408        rot = 4,
409        rot_op = "-0x5ffd",
410        rot_result = "0x3a",
411        swap_op = "0x1234",
412        swapped = "0x3412",
413        reversed = "0x2c48",
414        le_bytes = "[0x34, 0x12]",
415        be_bytes = "[0x12, 0x34]",
416        to_xe_bytes_doc = "",
417        from_xe_bytes_doc = "",
418        bound_condition = "",
419    }
420    midpoint_impl! { i16, i32, signed }
421    widening_mul_impl! { i16, i32 }
422}
423
424impl i32 {
425    int_impl! {
426        Self = i32,
427        ActualT = i32,
428        UnsignedT = u32,
429        BITS = 32,
430        BITS_MINUS_ONE = 31,
431        Min = -2147483648,
432        Max = 2147483647,
433        rot = 8,
434        rot_op = "0x10000b3",
435        rot_result = "0xb301",
436        swap_op = "0x12345678",
437        swapped = "0x78563412",
438        reversed = "0x1e6a2c48",
439        le_bytes = "[0x78, 0x56, 0x34, 0x12]",
440        be_bytes = "[0x12, 0x34, 0x56, 0x78]",
441        to_xe_bytes_doc = "",
442        from_xe_bytes_doc = "",
443        bound_condition = "",
444    }
445    midpoint_impl! { i32, i64, signed }
446    widening_mul_impl! { i32, i64 }
447}
448
449impl i64 {
450    int_impl! {
451        Self = i64,
452        ActualT = i64,
453        UnsignedT = u64,
454        BITS = 64,
455        BITS_MINUS_ONE = 63,
456        Min = -9223372036854775808,
457        Max = 9223372036854775807,
458        rot = 12,
459        rot_op = "0xaa00000000006e1",
460        rot_result = "0x6e10aa",
461        swap_op = "0x1234567890123456",
462        swapped = "0x5634129078563412",
463        reversed = "0x6a2c48091e6a2c48",
464        le_bytes = "[0x56, 0x34, 0x12, 0x90, 0x78, 0x56, 0x34, 0x12]",
465        be_bytes = "[0x12, 0x34, 0x56, 0x78, 0x90, 0x12, 0x34, 0x56]",
466        to_xe_bytes_doc = "",
467        from_xe_bytes_doc = "",
468        bound_condition = "",
469    }
470    midpoint_impl! { i64, signed }
471    widening_mul_impl! { i64, i128 }
472}
473
474impl i128 {
475    int_impl! {
476        Self = i128,
477        ActualT = i128,
478        UnsignedT = u128,
479        BITS = 128,
480        BITS_MINUS_ONE = 127,
481        Min = -170141183460469231731687303715884105728,
482        Max = 170141183460469231731687303715884105727,
483        rot = 16,
484        rot_op = "0x13f40000000000000000000000004f76",
485        rot_result = "0x4f7613f4",
486        swap_op = "0x12345678901234567890123456789012",
487        swapped = "0x12907856341290785634129078563412",
488        reversed = "0x48091e6a2c48091e6a2c48091e6a2c48",
489        le_bytes = "[0x12, 0x90, 0x78, 0x56, 0x34, 0x12, 0x90, 0x78, \
490            0x56, 0x34, 0x12, 0x90, 0x78, 0x56, 0x34, 0x12]",
491        be_bytes = "[0x12, 0x34, 0x56, 0x78, 0x90, 0x12, 0x34, 0x56, \
492            0x78, 0x90, 0x12, 0x34, 0x56, 0x78, 0x90, 0x12]",
493        to_xe_bytes_doc = "",
494        from_xe_bytes_doc = "",
495        bound_condition = "",
496    }
497    midpoint_impl! { i128, signed }
498}
499
500#[doc(auto_cfg = false)]
501#[cfg(target_pointer_width = "16")]
502impl isize {
503    int_impl! {
504        Self = isize,
505        ActualT = i16,
506        UnsignedT = usize,
507        BITS = 16,
508        BITS_MINUS_ONE = 15,
509        Min = -32768,
510        Max = 32767,
511        rot = 4,
512        rot_op = "-0x5ffd",
513        rot_result = "0x3a",
514        swap_op = "0x1234",
515        swapped = "0x3412",
516        reversed = "0x2c48",
517        le_bytes = "[0x34, 0x12]",
518        be_bytes = "[0x12, 0x34]",
519        to_xe_bytes_doc = usize_isize_to_xe_bytes_doc!(),
520        from_xe_bytes_doc = usize_isize_from_xe_bytes_doc!(),
521        bound_condition = " on 16-bit targets",
522    }
523    midpoint_impl! { isize, i32, signed }
524}
525
526#[doc(auto_cfg = false)]
527#[cfg(target_pointer_width = "32")]
528impl isize {
529    int_impl! {
530        Self = isize,
531        ActualT = i32,
532        UnsignedT = usize,
533        BITS = 32,
534        BITS_MINUS_ONE = 31,
535        Min = -2147483648,
536        Max = 2147483647,
537        rot = 8,
538        rot_op = "0x10000b3",
539        rot_result = "0xb301",
540        swap_op = "0x12345678",
541        swapped = "0x78563412",
542        reversed = "0x1e6a2c48",
543        le_bytes = "[0x78, 0x56, 0x34, 0x12]",
544        be_bytes = "[0x12, 0x34, 0x56, 0x78]",
545        to_xe_bytes_doc = usize_isize_to_xe_bytes_doc!(),
546        from_xe_bytes_doc = usize_isize_from_xe_bytes_doc!(),
547        bound_condition = " on 32-bit targets",
548    }
549    midpoint_impl! { isize, i64, signed }
550}
551
552#[doc(auto_cfg = false)]
553#[cfg(target_pointer_width = "64")]
554impl isize {
555    int_impl! {
556        Self = isize,
557        ActualT = i64,
558        UnsignedT = usize,
559        BITS = 64,
560        BITS_MINUS_ONE = 63,
561        Min = -9223372036854775808,
562        Max = 9223372036854775807,
563        rot = 12,
564        rot_op = "0xaa00000000006e1",
565        rot_result = "0x6e10aa",
566        swap_op = "0x1234567890123456",
567        swapped = "0x5634129078563412",
568        reversed = "0x6a2c48091e6a2c48",
569        le_bytes = "[0x56, 0x34, 0x12, 0x90, 0x78, 0x56, 0x34, 0x12]",
570        be_bytes = "[0x12, 0x34, 0x56, 0x78, 0x90, 0x12, 0x34, 0x56]",
571        to_xe_bytes_doc = usize_isize_to_xe_bytes_doc!(),
572        from_xe_bytes_doc = usize_isize_from_xe_bytes_doc!(),
573        bound_condition = " on 64-bit targets",
574    }
575    midpoint_impl! { isize, signed }
576}
577
578/// If the bit selected by this mask is set, ascii is lower case.
579const ASCII_CASE_MASK: u8 = 0b0010_0000;
580
581impl u8 {
582    uint_impl! {
583        Self = u8,
584        ActualT = u8,
585        SignedT = i8,
586        BITS = 8,
587        BITS_MINUS_ONE = 7,
588        MAX = 255,
589        rot = 2,
590        rot_op = "0x82",
591        rot_result = "0xa",
592        fsh_op = "0x36",
593        fshl_result = "0x8",
594        fshr_result = "0x8d",
595        clmul_lhs = "0x12",
596        clmul_rhs = "0x34",
597        clmul_result = "0x28",
598        swap_op = "0x12",
599        swapped = "0x12",
600        reversed = "0x48",
601        le_bytes = "[0x12]",
602        be_bytes = "[0x12]",
603        to_xe_bytes_doc = u8_xe_bytes_doc!(),
604        from_xe_bytes_doc = u8_xe_bytes_doc!(),
605        bound_condition = "",
606    }
607    midpoint_impl! { u8, u16, unsigned }
608    widening_mul_impl! { u8, u16 }
609    widening_carryless_mul_impl! { u8, u16 }
610    carrying_carryless_mul_impl! { u8, u16 }
611
612    /// Checks if the value is within the ASCII range.
613    ///
614    /// # Examples
615    ///
616    /// ```
617    /// let ascii = 97u8;
618    /// let non_ascii = 150u8;
619    ///
620    /// assert!(ascii.is_ascii());
621    /// assert!(!non_ascii.is_ascii());
622    /// ```
623    #[must_use]
624    #[stable(feature = "ascii_methods_on_intrinsics", since = "1.23.0")]
625    #[rustc_const_stable(feature = "const_u8_is_ascii", since = "1.43.0")]
626    #[inline]
627    #[ferrocene::prevalidated]
628    pub const fn is_ascii(&self) -> bool {
629        *self <= 127
630    }
631
632    /// If the value of this byte is within the ASCII range, returns it as an
633    /// [ASCII character](ascii::Char).  Otherwise, returns `None`.
634    #[must_use]
635    #[unstable(feature = "ascii_char", issue = "110998")]
636    #[inline]
637    pub const fn as_ascii(&self) -> Option<ascii::Char> {
638        ascii::Char::from_u8(*self)
639    }
640
641    /// Converts this byte to an [ASCII character](ascii::Char), without
642    /// checking whether or not it's valid.
643    ///
644    /// # Safety
645    ///
646    /// This byte must be valid ASCII, or else this is UB.
647    #[must_use]
648    #[unstable(feature = "ascii_char", issue = "110998")]
649    #[inline]
650    pub const unsafe fn as_ascii_unchecked(&self) -> ascii::Char {
651        assert_unsafe_precondition!(
652            check_library_ub,
653            "as_ascii_unchecked requires that the byte is valid ASCII",
654            (it: &u8 = self) => it.is_ascii()
655        );
656
657        // SAFETY: the caller promised that this byte is ASCII.
658        unsafe { ascii::Char::from_u8_unchecked(*self) }
659    }
660
661    /// Makes a copy of the value in its ASCII upper case equivalent.
662    ///
663    /// ASCII letters 'a' to 'z' are mapped to 'A' to 'Z',
664    /// but non-ASCII letters are unchanged.
665    ///
666    /// To uppercase the value in-place, use [`make_ascii_uppercase`].
667    ///
668    /// # Examples
669    ///
670    /// ```
671    /// let lowercase_a = 97u8;
672    ///
673    /// assert_eq!(65, lowercase_a.to_ascii_uppercase());
674    /// ```
675    ///
676    /// [`make_ascii_uppercase`]: Self::make_ascii_uppercase
677    #[must_use = "to uppercase the value in-place, use `make_ascii_uppercase()`"]
678    #[stable(feature = "ascii_methods_on_intrinsics", since = "1.23.0")]
679    #[rustc_const_stable(feature = "const_ascii_methods_on_intrinsics", since = "1.52.0")]
680    #[inline]
681    pub const fn to_ascii_uppercase(&self) -> u8 {
682        // Toggle the 6th bit if this is a lowercase letter
683        *self ^ ((self.is_ascii_lowercase() as u8) * ASCII_CASE_MASK)
684    }
685
686    /// Makes a copy of the value in its ASCII lower case equivalent.
687    ///
688    /// ASCII letters 'A' to 'Z' are mapped to 'a' to 'z',
689    /// but non-ASCII letters are unchanged.
690    ///
691    /// To lowercase the value in-place, use [`make_ascii_lowercase`].
692    ///
693    /// # Examples
694    ///
695    /// ```
696    /// let uppercase_a = 65u8;
697    ///
698    /// assert_eq!(97, uppercase_a.to_ascii_lowercase());
699    /// ```
700    ///
701    /// [`make_ascii_lowercase`]: Self::make_ascii_lowercase
702    #[must_use = "to lowercase the value in-place, use `make_ascii_lowercase()`"]
703    #[stable(feature = "ascii_methods_on_intrinsics", since = "1.23.0")]
704    #[rustc_const_stable(feature = "const_ascii_methods_on_intrinsics", since = "1.52.0")]
705    #[inline]
706    #[ferrocene::prevalidated]
707    pub const fn to_ascii_lowercase(&self) -> u8 {
708        // Set the 6th bit if this is an uppercase letter
709        *self | (self.is_ascii_uppercase() as u8 * ASCII_CASE_MASK)
710    }
711
712    /// Assumes self is ascii
713    #[inline]
714    pub(crate) const fn ascii_change_case_unchecked(&self) -> u8 {
715        *self ^ ASCII_CASE_MASK
716    }
717
718    /// Checks that two values are an ASCII case-insensitive match.
719    ///
720    /// This is equivalent to `to_ascii_lowercase(a) == to_ascii_lowercase(b)`.
721    ///
722    /// # Examples
723    ///
724    /// ```
725    /// let lowercase_a = 97u8;
726    /// let uppercase_a = 65u8;
727    ///
728    /// assert!(lowercase_a.eq_ignore_ascii_case(&uppercase_a));
729    /// ```
730    #[stable(feature = "ascii_methods_on_intrinsics", since = "1.23.0")]
731    #[rustc_const_stable(feature = "const_ascii_methods_on_intrinsics", since = "1.52.0")]
732    #[inline]
733    #[ferrocene::prevalidated]
734    pub const fn eq_ignore_ascii_case(&self, other: &u8) -> bool {
735        self.to_ascii_lowercase() == other.to_ascii_lowercase()
736    }
737
738    /// Converts this value to its ASCII upper case equivalent in-place.
739    ///
740    /// ASCII letters 'a' to 'z' are mapped to 'A' to 'Z',
741    /// but non-ASCII letters are unchanged.
742    ///
743    /// To return a new uppercased value without modifying the existing one, use
744    /// [`to_ascii_uppercase`].
745    ///
746    /// # Examples
747    ///
748    /// ```
749    /// let mut byte = b'a';
750    ///
751    /// byte.make_ascii_uppercase();
752    ///
753    /// assert_eq!(b'A', byte);
754    /// ```
755    ///
756    /// [`to_ascii_uppercase`]: Self::to_ascii_uppercase
757    #[stable(feature = "ascii_methods_on_intrinsics", since = "1.23.0")]
758    #[rustc_const_stable(feature = "const_make_ascii", since = "1.84.0")]
759    #[inline]
760    pub const fn make_ascii_uppercase(&mut self) {
761        *self = self.to_ascii_uppercase();
762    }
763
764    /// Converts this value to its ASCII lower case equivalent in-place.
765    ///
766    /// ASCII letters 'A' to 'Z' are mapped to 'a' to 'z',
767    /// but non-ASCII letters are unchanged.
768    ///
769    /// To return a new lowercased value without modifying the existing one, use
770    /// [`to_ascii_lowercase`].
771    ///
772    /// # Examples
773    ///
774    /// ```
775    /// let mut byte = b'A';
776    ///
777    /// byte.make_ascii_lowercase();
778    ///
779    /// assert_eq!(b'a', byte);
780    /// ```
781    ///
782    /// [`to_ascii_lowercase`]: Self::to_ascii_lowercase
783    #[stable(feature = "ascii_methods_on_intrinsics", since = "1.23.0")]
784    #[rustc_const_stable(feature = "const_make_ascii", since = "1.84.0")]
785    #[inline]
786    pub const fn make_ascii_lowercase(&mut self) {
787        *self = self.to_ascii_lowercase();
788    }
789
790    /// Checks if the value is an ASCII alphabetic character:
791    ///
792    /// - U+0041 'A' ..= U+005A 'Z', or
793    /// - U+0061 'a' ..= U+007A 'z'.
794    ///
795    /// # Examples
796    ///
797    /// ```
798    /// let uppercase_a = b'A';
799    /// let uppercase_g = b'G';
800    /// let a = b'a';
801    /// let g = b'g';
802    /// let zero = b'0';
803    /// let percent = b'%';
804    /// let space = b' ';
805    /// let lf = b'\n';
806    /// let esc = b'\x1b';
807    ///
808    /// assert!(uppercase_a.is_ascii_alphabetic());
809    /// assert!(uppercase_g.is_ascii_alphabetic());
810    /// assert!(a.is_ascii_alphabetic());
811    /// assert!(g.is_ascii_alphabetic());
812    /// assert!(!zero.is_ascii_alphabetic());
813    /// assert!(!percent.is_ascii_alphabetic());
814    /// assert!(!space.is_ascii_alphabetic());
815    /// assert!(!lf.is_ascii_alphabetic());
816    /// assert!(!esc.is_ascii_alphabetic());
817    /// ```
818    #[must_use]
819    #[stable(feature = "ascii_ctype_on_intrinsics", since = "1.24.0")]
820    #[rustc_const_stable(feature = "const_ascii_ctype_on_intrinsics", since = "1.47.0")]
821    #[inline]
822    #[ferrocene::prevalidated]
823    pub const fn is_ascii_alphabetic(&self) -> bool {
824        matches!(*self, b'A'..=b'Z' | b'a'..=b'z')
825    }
826
827    /// Checks if the value is an ASCII uppercase character:
828    /// U+0041 'A' ..= U+005A 'Z'.
829    ///
830    /// # Examples
831    ///
832    /// ```
833    /// let uppercase_a = b'A';
834    /// let uppercase_g = b'G';
835    /// let a = b'a';
836    /// let g = b'g';
837    /// let zero = b'0';
838    /// let percent = b'%';
839    /// let space = b' ';
840    /// let lf = b'\n';
841    /// let esc = b'\x1b';
842    ///
843    /// assert!(uppercase_a.is_ascii_uppercase());
844    /// assert!(uppercase_g.is_ascii_uppercase());
845    /// assert!(!a.is_ascii_uppercase());
846    /// assert!(!g.is_ascii_uppercase());
847    /// assert!(!zero.is_ascii_uppercase());
848    /// assert!(!percent.is_ascii_uppercase());
849    /// assert!(!space.is_ascii_uppercase());
850    /// assert!(!lf.is_ascii_uppercase());
851    /// assert!(!esc.is_ascii_uppercase());
852    /// ```
853    #[must_use]
854    #[stable(feature = "ascii_ctype_on_intrinsics", since = "1.24.0")]
855    #[rustc_const_stable(feature = "const_ascii_ctype_on_intrinsics", since = "1.47.0")]
856    #[inline]
857    #[ferrocene::prevalidated]
858    pub const fn is_ascii_uppercase(&self) -> bool {
859        matches!(*self, b'A'..=b'Z')
860    }
861
862    /// Checks if the value is an ASCII lowercase character:
863    /// U+0061 'a' ..= U+007A 'z'.
864    ///
865    /// # Examples
866    ///
867    /// ```
868    /// let uppercase_a = b'A';
869    /// let uppercase_g = b'G';
870    /// let a = b'a';
871    /// let g = b'g';
872    /// let zero = b'0';
873    /// let percent = b'%';
874    /// let space = b' ';
875    /// let lf = b'\n';
876    /// let esc = b'\x1b';
877    ///
878    /// assert!(!uppercase_a.is_ascii_lowercase());
879    /// assert!(!uppercase_g.is_ascii_lowercase());
880    /// assert!(a.is_ascii_lowercase());
881    /// assert!(g.is_ascii_lowercase());
882    /// assert!(!zero.is_ascii_lowercase());
883    /// assert!(!percent.is_ascii_lowercase());
884    /// assert!(!space.is_ascii_lowercase());
885    /// assert!(!lf.is_ascii_lowercase());
886    /// assert!(!esc.is_ascii_lowercase());
887    /// ```
888    #[must_use]
889    #[stable(feature = "ascii_ctype_on_intrinsics", since = "1.24.0")]
890    #[rustc_const_stable(feature = "const_ascii_ctype_on_intrinsics", since = "1.47.0")]
891    #[inline]
892    pub const fn is_ascii_lowercase(&self) -> bool {
893        matches!(*self, b'a'..=b'z')
894    }
895
896    /// Checks if the value is an ASCII alphanumeric character:
897    ///
898    /// - U+0041 'A' ..= U+005A 'Z', or
899    /// - U+0061 'a' ..= U+007A 'z', or
900    /// - U+0030 '0' ..= U+0039 '9'.
901    ///
902    /// # Examples
903    ///
904    /// ```
905    /// let uppercase_a = b'A';
906    /// let uppercase_g = b'G';
907    /// let a = b'a';
908    /// let g = b'g';
909    /// let zero = b'0';
910    /// let percent = b'%';
911    /// let space = b' ';
912    /// let lf = b'\n';
913    /// let esc = b'\x1b';
914    ///
915    /// assert!(uppercase_a.is_ascii_alphanumeric());
916    /// assert!(uppercase_g.is_ascii_alphanumeric());
917    /// assert!(a.is_ascii_alphanumeric());
918    /// assert!(g.is_ascii_alphanumeric());
919    /// assert!(zero.is_ascii_alphanumeric());
920    /// assert!(!percent.is_ascii_alphanumeric());
921    /// assert!(!space.is_ascii_alphanumeric());
922    /// assert!(!lf.is_ascii_alphanumeric());
923    /// assert!(!esc.is_ascii_alphanumeric());
924    /// ```
925    #[must_use]
926    #[stable(feature = "ascii_ctype_on_intrinsics", since = "1.24.0")]
927    #[rustc_const_stable(feature = "const_ascii_ctype_on_intrinsics", since = "1.47.0")]
928    #[inline]
929    pub const fn is_ascii_alphanumeric(&self) -> bool {
930        matches!(*self, b'0'..=b'9') | matches!(*self, b'A'..=b'Z') | matches!(*self, b'a'..=b'z')
931    }
932
933    /// Checks if the value is an ASCII decimal digit:
934    /// U+0030 '0' ..= U+0039 '9'.
935    ///
936    /// # Examples
937    ///
938    /// ```
939    /// let uppercase_a = b'A';
940    /// let uppercase_g = b'G';
941    /// let a = b'a';
942    /// let g = b'g';
943    /// let zero = b'0';
944    /// let percent = b'%';
945    /// let space = b' ';
946    /// let lf = b'\n';
947    /// let esc = b'\x1b';
948    ///
949    /// assert!(!uppercase_a.is_ascii_digit());
950    /// assert!(!uppercase_g.is_ascii_digit());
951    /// assert!(!a.is_ascii_digit());
952    /// assert!(!g.is_ascii_digit());
953    /// assert!(zero.is_ascii_digit());
954    /// assert!(!percent.is_ascii_digit());
955    /// assert!(!space.is_ascii_digit());
956    /// assert!(!lf.is_ascii_digit());
957    /// assert!(!esc.is_ascii_digit());
958    /// ```
959    #[must_use]
960    #[stable(feature = "ascii_ctype_on_intrinsics", since = "1.24.0")]
961    #[rustc_const_stable(feature = "const_ascii_ctype_on_intrinsics", since = "1.47.0")]
962    #[inline]
963    pub const fn is_ascii_digit(&self) -> bool {
964        matches!(*self, b'0'..=b'9')
965    }
966
967    /// Checks if the value is an ASCII octal digit:
968    /// U+0030 '0' ..= U+0037 '7'.
969    ///
970    /// # Examples
971    ///
972    /// ```
973    /// #![feature(is_ascii_octdigit)]
974    ///
975    /// let uppercase_a = b'A';
976    /// let a = b'a';
977    /// let zero = b'0';
978    /// let seven = b'7';
979    /// let nine = b'9';
980    /// let percent = b'%';
981    /// let lf = b'\n';
982    ///
983    /// assert!(!uppercase_a.is_ascii_octdigit());
984    /// assert!(!a.is_ascii_octdigit());
985    /// assert!(zero.is_ascii_octdigit());
986    /// assert!(seven.is_ascii_octdigit());
987    /// assert!(!nine.is_ascii_octdigit());
988    /// assert!(!percent.is_ascii_octdigit());
989    /// assert!(!lf.is_ascii_octdigit());
990    /// ```
991    #[must_use]
992    #[unstable(feature = "is_ascii_octdigit", issue = "101288")]
993    #[inline]
994    pub const fn is_ascii_octdigit(&self) -> bool {
995        matches!(*self, b'0'..=b'7')
996    }
997
998    /// Checks if the value is an ASCII hexadecimal digit:
999    ///
1000    /// - U+0030 '0' ..= U+0039 '9', or
1001    /// - U+0041 'A' ..= U+0046 'F', or
1002    /// - U+0061 'a' ..= U+0066 'f'.
1003    ///
1004    /// # Examples
1005    ///
1006    /// ```
1007    /// let uppercase_a = b'A';
1008    /// let uppercase_g = b'G';
1009    /// let a = b'a';
1010    /// let g = b'g';
1011    /// let zero = b'0';
1012    /// let percent = b'%';
1013    /// let space = b' ';
1014    /// let lf = b'\n';
1015    /// let esc = b'\x1b';
1016    ///
1017    /// assert!(uppercase_a.is_ascii_hexdigit());
1018    /// assert!(!uppercase_g.is_ascii_hexdigit());
1019    /// assert!(a.is_ascii_hexdigit());
1020    /// assert!(!g.is_ascii_hexdigit());
1021    /// assert!(zero.is_ascii_hexdigit());
1022    /// assert!(!percent.is_ascii_hexdigit());
1023    /// assert!(!space.is_ascii_hexdigit());
1024    /// assert!(!lf.is_ascii_hexdigit());
1025    /// assert!(!esc.is_ascii_hexdigit());
1026    /// ```
1027    #[must_use]
1028    #[stable(feature = "ascii_ctype_on_intrinsics", since = "1.24.0")]
1029    #[rustc_const_stable(feature = "const_ascii_ctype_on_intrinsics", since = "1.47.0")]
1030    #[inline]
1031    pub const fn is_ascii_hexdigit(&self) -> bool {
1032        matches!(*self, b'0'..=b'9') | matches!(*self, b'A'..=b'F') | matches!(*self, b'a'..=b'f')
1033    }
1034
1035    /// Checks if the value is an ASCII punctuation or symbol character
1036    /// (i.e. not alphanumeric, whitespace, or control):
1037    ///
1038    /// - U+0021 ..= U+002F `! " # $ % & ' ( ) * + , - . /`, or
1039    /// - U+003A ..= U+0040 `: ; < = > ? @`, or
1040    /// - U+005B ..= U+0060 `` [ \ ] ^ _ ` ``, or
1041    /// - U+007B ..= U+007E `{ | } ~`
1042    ///
1043    /// # Examples
1044    ///
1045    /// ```
1046    /// let uppercase_a = b'A';
1047    /// let uppercase_g = b'G';
1048    /// let a = b'a';
1049    /// let g = b'g';
1050    /// let zero = b'0';
1051    /// let percent = b'%';
1052    /// let space = b' ';
1053    /// let lf = b'\n';
1054    /// let esc = b'\x1b';
1055    ///
1056    /// assert!(!uppercase_a.is_ascii_punctuation());
1057    /// assert!(!uppercase_g.is_ascii_punctuation());
1058    /// assert!(!a.is_ascii_punctuation());
1059    /// assert!(!g.is_ascii_punctuation());
1060    /// assert!(!zero.is_ascii_punctuation());
1061    /// assert!(percent.is_ascii_punctuation());
1062    /// assert!(!space.is_ascii_punctuation());
1063    /// assert!(!lf.is_ascii_punctuation());
1064    /// assert!(!esc.is_ascii_punctuation());
1065    /// ```
1066    #[must_use]
1067    #[stable(feature = "ascii_ctype_on_intrinsics", since = "1.24.0")]
1068    #[rustc_const_stable(feature = "const_ascii_ctype_on_intrinsics", since = "1.47.0")]
1069    #[inline]
1070    pub const fn is_ascii_punctuation(&self) -> bool {
1071        matches!(*self, b'!'..=b'/')
1072            | matches!(*self, b':'..=b'@')
1073            | matches!(*self, b'['..=b'`')
1074            | matches!(*self, b'{'..=b'~')
1075    }
1076
1077    /// Checks if the value is an ASCII graphic character
1078    /// (i.e. not whitespace or control):
1079    /// U+0021 '!' ..= U+007E '~'.
1080    ///
1081    /// # Examples
1082    ///
1083    /// ```
1084    /// let uppercase_a = b'A';
1085    /// let uppercase_g = b'G';
1086    /// let a = b'a';
1087    /// let g = b'g';
1088    /// let zero = b'0';
1089    /// let percent = b'%';
1090    /// let space = b' ';
1091    /// let lf = b'\n';
1092    /// let esc = b'\x1b';
1093    ///
1094    /// assert!(uppercase_a.is_ascii_graphic());
1095    /// assert!(uppercase_g.is_ascii_graphic());
1096    /// assert!(a.is_ascii_graphic());
1097    /// assert!(g.is_ascii_graphic());
1098    /// assert!(zero.is_ascii_graphic());
1099    /// assert!(percent.is_ascii_graphic());
1100    /// assert!(!space.is_ascii_graphic());
1101    /// assert!(!lf.is_ascii_graphic());
1102    /// assert!(!esc.is_ascii_graphic());
1103    /// ```
1104    #[must_use]
1105    #[stable(feature = "ascii_ctype_on_intrinsics", since = "1.24.0")]
1106    #[rustc_const_stable(feature = "const_ascii_ctype_on_intrinsics", since = "1.47.0")]
1107    #[inline]
1108    pub const fn is_ascii_graphic(&self) -> bool {
1109        matches!(*self, b'!'..=b'~')
1110    }
1111
1112    /// Checks if the value is an ASCII whitespace character:
1113    /// U+0020 SPACE, U+0009 HORIZONTAL TAB, U+000A LINE FEED,
1114    /// U+000C FORM FEED, or U+000D CARRIAGE RETURN.
1115    ///
1116    /// **Warning:** Because the list above excludes U+000B VERTICAL TAB,
1117    /// `b.is_ascii_whitespace()` is **not** equivalent to `char::from(b).is_whitespace()`.
1118    ///
1119    /// Rust uses the WhatWG Infra Standard's [definition of ASCII
1120    /// whitespace][infra-aw]. There are several other definitions in
1121    /// wide use. For instance, [the POSIX locale][pct] includes
1122    /// U+000B VERTICAL TAB as well as all the above characters,
1123    /// but—from the very same specification—[the default rule for
1124    /// "field splitting" in the Bourne shell][bfs] considers *only*
1125    /// SPACE, HORIZONTAL TAB, and LINE FEED as whitespace.
1126    ///
1127    /// If you are writing a program that will process an existing
1128    /// file format, check what that format's definition of whitespace is
1129    /// before using this function.
1130    ///
1131    /// [infra-aw]: https://infra.spec.whatwg.org/#ascii-whitespace
1132    /// [pct]: https://pubs.opengroup.org/onlinepubs/9699919799/basedefs/V1_chap07.html#tag_07_03_01
1133    /// [bfs]: https://pubs.opengroup.org/onlinepubs/9699919799/utilities/V3_chap02.html#tag_18_06_05
1134    ///
1135    /// # Examples
1136    ///
1137    /// ```
1138    /// let uppercase_a = b'A';
1139    /// let uppercase_g = b'G';
1140    /// let a = b'a';
1141    /// let g = b'g';
1142    /// let zero = b'0';
1143    /// let percent = b'%';
1144    /// let space = b' ';
1145    /// let lf = b'\n';
1146    /// let esc = b'\x1b';
1147    ///
1148    /// assert!(!uppercase_a.is_ascii_whitespace());
1149    /// assert!(!uppercase_g.is_ascii_whitespace());
1150    /// assert!(!a.is_ascii_whitespace());
1151    /// assert!(!g.is_ascii_whitespace());
1152    /// assert!(!zero.is_ascii_whitespace());
1153    /// assert!(!percent.is_ascii_whitespace());
1154    /// assert!(space.is_ascii_whitespace());
1155    /// assert!(lf.is_ascii_whitespace());
1156    /// assert!(!esc.is_ascii_whitespace());
1157    /// ```
1158    #[must_use]
1159    #[stable(feature = "ascii_ctype_on_intrinsics", since = "1.24.0")]
1160    #[rustc_const_stable(feature = "const_ascii_ctype_on_intrinsics", since = "1.47.0")]
1161    #[inline]
1162    #[ferrocene::prevalidated]
1163    pub const fn is_ascii_whitespace(&self) -> bool {
1164        matches!(*self, b'\t' | b'\n' | b'\x0C' | b'\r' | b' ')
1165    }
1166
1167    /// Checks if the value is an ASCII control character:
1168    /// U+0000 NUL ..= U+001F UNIT SEPARATOR, or U+007F DELETE.
1169    /// Note that most ASCII whitespace characters are control
1170    /// characters, but SPACE is not.
1171    ///
1172    /// # Examples
1173    ///
1174    /// ```
1175    /// let uppercase_a = b'A';
1176    /// let uppercase_g = b'G';
1177    /// let a = b'a';
1178    /// let g = b'g';
1179    /// let zero = b'0';
1180    /// let percent = b'%';
1181    /// let space = b' ';
1182    /// let lf = b'\n';
1183    /// let esc = b'\x1b';
1184    ///
1185    /// assert!(!uppercase_a.is_ascii_control());
1186    /// assert!(!uppercase_g.is_ascii_control());
1187    /// assert!(!a.is_ascii_control());
1188    /// assert!(!g.is_ascii_control());
1189    /// assert!(!zero.is_ascii_control());
1190    /// assert!(!percent.is_ascii_control());
1191    /// assert!(!space.is_ascii_control());
1192    /// assert!(lf.is_ascii_control());
1193    /// assert!(esc.is_ascii_control());
1194    /// ```
1195    #[must_use]
1196    #[stable(feature = "ascii_ctype_on_intrinsics", since = "1.24.0")]
1197    #[rustc_const_stable(feature = "const_ascii_ctype_on_intrinsics", since = "1.47.0")]
1198    #[inline]
1199    #[ferrocene::prevalidated]
1200    pub const fn is_ascii_control(&self) -> bool {
1201        matches!(*self, b'\0'..=b'\x1F' | b'\x7F')
1202    }
1203
1204    /// Returns an iterator that produces an escaped version of a `u8`,
1205    /// treating it as an ASCII character.
1206    ///
1207    /// The behavior is identical to [`ascii::escape_default`].
1208    ///
1209    /// # Examples
1210    ///
1211    /// ```
1212    /// assert_eq!("0", b'0'.escape_ascii().to_string());
1213    /// assert_eq!("\\t", b'\t'.escape_ascii().to_string());
1214    /// assert_eq!("\\r", b'\r'.escape_ascii().to_string());
1215    /// assert_eq!("\\n", b'\n'.escape_ascii().to_string());
1216    /// assert_eq!("\\'", b'\''.escape_ascii().to_string());
1217    /// assert_eq!("\\\"", b'"'.escape_ascii().to_string());
1218    /// assert_eq!("\\\\", b'\\'.escape_ascii().to_string());
1219    /// assert_eq!("\\x9d", b'\x9d'.escape_ascii().to_string());
1220    /// ```
1221    #[must_use = "this returns the escaped byte as an iterator, \
1222                  without modifying the original"]
1223    #[stable(feature = "inherent_ascii_escape", since = "1.60.0")]
1224    #[inline]
1225    #[ferrocene::prevalidated]
1226    pub fn escape_ascii(self) -> ascii::EscapeDefault {
1227        ascii::escape_default(self)
1228    }
1229
1230    #[inline]
1231    #[ferrocene::prevalidated]
1232    pub(crate) const fn is_utf8_char_boundary(self) -> bool {
1233        // This is bit magic equivalent to: b < 128 || b >= 192
1234        (self as i8) >= -0x40
1235    }
1236}
1237
1238impl u16 {
1239    uint_impl! {
1240        Self = u16,
1241        ActualT = u16,
1242        SignedT = i16,
1243        BITS = 16,
1244        BITS_MINUS_ONE = 15,
1245        MAX = 65535,
1246        rot = 4,
1247        rot_op = "0xa003",
1248        rot_result = "0x3a",
1249        fsh_op = "0x2de",
1250        fshl_result = "0x30",
1251        fshr_result = "0x302d",
1252        clmul_lhs = "0x9012",
1253        clmul_rhs = "0xcd34",
1254        clmul_result = "0x928",
1255        swap_op = "0x1234",
1256        swapped = "0x3412",
1257        reversed = "0x2c48",
1258        le_bytes = "[0x34, 0x12]",
1259        be_bytes = "[0x12, 0x34]",
1260        to_xe_bytes_doc = "",
1261        from_xe_bytes_doc = "",
1262        bound_condition = "",
1263    }
1264    midpoint_impl! { u16, u32, unsigned }
1265    widening_mul_impl! { u16, u32 }
1266    widening_carryless_mul_impl! { u16, u32 }
1267    carrying_carryless_mul_impl! { u16, u32 }
1268
1269    /// Checks if the value is a Unicode surrogate code point, which are disallowed values for [`char`].
1270    ///
1271    /// # Examples
1272    ///
1273    /// ```
1274    /// #![feature(utf16_extra)]
1275    ///
1276    /// let low_non_surrogate = 0xA000u16;
1277    /// let low_surrogate = 0xD800u16;
1278    /// let high_surrogate = 0xDC00u16;
1279    /// let high_non_surrogate = 0xE000u16;
1280    ///
1281    /// assert!(!low_non_surrogate.is_utf16_surrogate());
1282    /// assert!(low_surrogate.is_utf16_surrogate());
1283    /// assert!(high_surrogate.is_utf16_surrogate());
1284    /// assert!(!high_non_surrogate.is_utf16_surrogate());
1285    /// ```
1286    #[must_use]
1287    #[unstable(feature = "utf16_extra", issue = "94919")]
1288    #[inline]
1289    #[ferrocene::prevalidated]
1290    pub const fn is_utf16_surrogate(self) -> bool {
1291        matches!(self, 0xD800..=0xDFFF)
1292    }
1293}
1294
1295impl u32 {
1296    uint_impl! {
1297        Self = u32,
1298        ActualT = u32,
1299        SignedT = i32,
1300        BITS = 32,
1301        BITS_MINUS_ONE = 31,
1302        MAX = 4294967295,
1303        rot = 8,
1304        rot_op = "0x10000b3",
1305        rot_result = "0xb301",
1306        fsh_op = "0x2fe78e45",
1307        fshl_result = "0xb32f",
1308        fshr_result = "0xb32fe78e",
1309        clmul_lhs = "0x56789012",
1310        clmul_rhs = "0xf52ecd34",
1311        clmul_result = "0x9b980928",
1312        swap_op = "0x12345678",
1313        swapped = "0x78563412",
1314        reversed = "0x1e6a2c48",
1315        le_bytes = "[0x78, 0x56, 0x34, 0x12]",
1316        be_bytes = "[0x12, 0x34, 0x56, 0x78]",
1317        to_xe_bytes_doc = "",
1318        from_xe_bytes_doc = "",
1319        bound_condition = "",
1320    }
1321    midpoint_impl! { u32, u64, unsigned }
1322    widening_mul_impl! { u32, u64 }
1323    widening_carryless_mul_impl! { u32, u64 }
1324    carrying_carryless_mul_impl! { u32, u64 }
1325}
1326
1327impl u64 {
1328    uint_impl! {
1329        Self = u64,
1330        ActualT = u64,
1331        SignedT = i64,
1332        BITS = 64,
1333        BITS_MINUS_ONE = 63,
1334        MAX = 18446744073709551615,
1335        rot = 12,
1336        rot_op = "0xaa00000000006e1",
1337        rot_result = "0x6e10aa",
1338        fsh_op = "0x2fe78e45983acd98",
1339        fshl_result = "0x6e12fe",
1340        fshr_result = "0x6e12fe78e45983ac",
1341        clmul_lhs = "0x7890123456789012",
1342        clmul_rhs = "0xdd358416f52ecd34",
1343        clmul_result = "0xa6299579b980928",
1344        swap_op = "0x1234567890123456",
1345        swapped = "0x5634129078563412",
1346        reversed = "0x6a2c48091e6a2c48",
1347        le_bytes = "[0x56, 0x34, 0x12, 0x90, 0x78, 0x56, 0x34, 0x12]",
1348        be_bytes = "[0x12, 0x34, 0x56, 0x78, 0x90, 0x12, 0x34, 0x56]",
1349        to_xe_bytes_doc = "",
1350        from_xe_bytes_doc = "",
1351        bound_condition = "",
1352    }
1353    midpoint_impl! { u64, u128, unsigned }
1354    widening_mul_impl! { u64, u128 }
1355    widening_carryless_mul_impl! { u64, u128 }
1356    carrying_carryless_mul_impl! { u64, u128 }
1357}
1358
1359impl u128 {
1360    uint_impl! {
1361        Self = u128,
1362        ActualT = u128,
1363        SignedT = i128,
1364        BITS = 128,
1365        BITS_MINUS_ONE = 127,
1366        MAX = 340282366920938463463374607431768211455,
1367        rot = 16,
1368        rot_op = "0x13f40000000000000000000000004f76",
1369        rot_result = "0x4f7613f4",
1370        fsh_op = "0x2fe78e45983acd98039000008736273",
1371        fshl_result = "0x4f7602fe",
1372        fshr_result = "0x4f7602fe78e45983acd9803900000873",
1373        clmul_lhs = "0x12345678901234567890123456789012",
1374        clmul_rhs = "0x4317e40ab4ddcf05dd358416f52ecd34",
1375        clmul_result = "0xb9cf660de35d0c170a6299579b980928",
1376        swap_op = "0x12345678901234567890123456789012",
1377        swapped = "0x12907856341290785634129078563412",
1378        reversed = "0x48091e6a2c48091e6a2c48091e6a2c48",
1379        le_bytes = "[0x12, 0x90, 0x78, 0x56, 0x34, 0x12, 0x90, 0x78, \
1380            0x56, 0x34, 0x12, 0x90, 0x78, 0x56, 0x34, 0x12]",
1381        be_bytes = "[0x12, 0x34, 0x56, 0x78, 0x90, 0x12, 0x34, 0x56, \
1382            0x78, 0x90, 0x12, 0x34, 0x56, 0x78, 0x90, 0x12]",
1383        to_xe_bytes_doc = "",
1384        from_xe_bytes_doc = "",
1385        bound_condition = "",
1386    }
1387    midpoint_impl! { u128, unsigned }
1388    carrying_carryless_mul_impl! { u128, u256 }
1389}
1390
1391#[doc(auto_cfg = false)]
1392#[cfg(target_pointer_width = "16")]
1393impl usize {
1394    uint_impl! {
1395        Self = usize,
1396        ActualT = u16,
1397        SignedT = isize,
1398        BITS = 16,
1399        BITS_MINUS_ONE = 15,
1400        MAX = 65535,
1401        rot = 4,
1402        rot_op = "0xa003",
1403        rot_result = "0x3a",
1404        fsh_op = "0x2de",
1405        fshl_result = "0x30",
1406        fshr_result = "0x302d",
1407        clmul_lhs = "0x9012",
1408        clmul_rhs = "0xcd34",
1409        clmul_result = "0x928",
1410        swap_op = "0x1234",
1411        swapped = "0x3412",
1412        reversed = "0x2c48",
1413        le_bytes = "[0x34, 0x12]",
1414        be_bytes = "[0x12, 0x34]",
1415        to_xe_bytes_doc = usize_isize_to_xe_bytes_doc!(),
1416        from_xe_bytes_doc = usize_isize_from_xe_bytes_doc!(),
1417        bound_condition = " on 16-bit targets",
1418    }
1419    midpoint_impl! { usize, u32, unsigned }
1420    carrying_carryless_mul_impl! { usize, u32 }
1421}
1422
1423#[doc(auto_cfg = false)]
1424#[cfg(target_pointer_width = "32")]
1425impl usize {
1426    uint_impl! {
1427        Self = usize,
1428        ActualT = u32,
1429        SignedT = isize,
1430        BITS = 32,
1431        BITS_MINUS_ONE = 31,
1432        MAX = 4294967295,
1433        rot = 8,
1434        rot_op = "0x10000b3",
1435        rot_result = "0xb301",
1436        fsh_op = "0x2fe78e45",
1437        fshl_result = "0xb32f",
1438        fshr_result = "0xb32fe78e",
1439        clmul_lhs = "0x56789012",
1440        clmul_rhs = "0xf52ecd34",
1441        clmul_result = "0x9b980928",
1442        swap_op = "0x12345678",
1443        swapped = "0x78563412",
1444        reversed = "0x1e6a2c48",
1445        le_bytes = "[0x78, 0x56, 0x34, 0x12]",
1446        be_bytes = "[0x12, 0x34, 0x56, 0x78]",
1447        to_xe_bytes_doc = usize_isize_to_xe_bytes_doc!(),
1448        from_xe_bytes_doc = usize_isize_from_xe_bytes_doc!(),
1449        bound_condition = " on 32-bit targets",
1450    }
1451    midpoint_impl! { usize, u64, unsigned }
1452    carrying_carryless_mul_impl! { usize, u64 }
1453}
1454
1455#[doc(auto_cfg = false)]
1456#[cfg(target_pointer_width = "64")]
1457impl usize {
1458    uint_impl! {
1459        Self = usize,
1460        ActualT = u64,
1461        SignedT = isize,
1462        BITS = 64,
1463        BITS_MINUS_ONE = 63,
1464        MAX = 18446744073709551615,
1465        rot = 12,
1466        rot_op = "0xaa00000000006e1",
1467        rot_result = "0x6e10aa",
1468        fsh_op = "0x2fe78e45983acd98",
1469        fshl_result = "0x6e12fe",
1470        fshr_result = "0x6e12fe78e45983ac",
1471        clmul_lhs = "0x7890123456789012",
1472        clmul_rhs = "0xdd358416f52ecd34",
1473        clmul_result = "0xa6299579b980928",
1474        swap_op = "0x1234567890123456",
1475        swapped = "0x5634129078563412",
1476        reversed = "0x6a2c48091e6a2c48",
1477        le_bytes = "[0x56, 0x34, 0x12, 0x90, 0x78, 0x56, 0x34, 0x12]",
1478        be_bytes = "[0x12, 0x34, 0x56, 0x78, 0x90, 0x12, 0x34, 0x56]",
1479        to_xe_bytes_doc = usize_isize_to_xe_bytes_doc!(),
1480        from_xe_bytes_doc = usize_isize_from_xe_bytes_doc!(),
1481        bound_condition = " on 64-bit targets",
1482    }
1483    midpoint_impl! { usize, u128, unsigned }
1484    carrying_carryless_mul_impl! { usize, u128 }
1485}
1486
1487impl usize {
1488    /// Returns an `usize` where every byte is equal to `x`.
1489    #[inline]
1490    #[ferrocene::prevalidated]
1491    pub(crate) const fn repeat_u8(x: u8) -> usize {
1492        usize::from_ne_bytes([x; size_of::<usize>()])
1493    }
1494
1495    /// Returns an `usize` where every byte pair is equal to `x`.
1496    #[inline]
1497    #[ferrocene::annotation("This function is only being used in constants and cannot be covered")]
1498    #[ferrocene::prevalidated]
1499    pub(crate) const fn repeat_u16(x: u16) -> usize {
1500        let mut r = 0usize;
1501        let mut i = 0;
1502        while i < size_of::<usize>() {
1503            // Use `wrapping_shl` to make it work on targets with 16-bit `usize`
1504            r = r.wrapping_shl(16) | (x as usize);
1505            i += 2;
1506        }
1507        r
1508    }
1509}
1510
1511/// A classification of floating point numbers.
1512///
1513/// This `enum` is used as the return type for [`f32::classify`] and [`f64::classify`]. See
1514/// their documentation for more.
1515///
1516/// # Examples
1517///
1518/// ```
1519/// use std::num::FpCategory;
1520///
1521/// let num = 12.4_f32;
1522/// let inf = f32::INFINITY;
1523/// let zero = 0f32;
1524/// let sub: f32 = 1.1754942e-38;
1525/// let nan = f32::NAN;
1526///
1527/// assert_eq!(num.classify(), FpCategory::Normal);
1528/// assert_eq!(inf.classify(), FpCategory::Infinite);
1529/// assert_eq!(zero.classify(), FpCategory::Zero);
1530/// assert_eq!(sub.classify(), FpCategory::Subnormal);
1531/// assert_eq!(nan.classify(), FpCategory::Nan);
1532/// ```
1533#[derive(Copy, Clone, PartialEq, Eq, Debug)]
1534#[stable(feature = "rust1", since = "1.0.0")]
1535#[ferrocene::prevalidated]
1536pub enum FpCategory {
1537    /// NaN (not a number): this value results from calculations like `(-1.0).sqrt()`.
1538    ///
1539    /// See [the documentation for `f32`](f32) for more information on the unusual properties
1540    /// of NaN.
1541    #[stable(feature = "rust1", since = "1.0.0")]
1542    Nan,
1543
1544    /// Positive or negative infinity, which often results from dividing a nonzero number
1545    /// by zero.
1546    #[stable(feature = "rust1", since = "1.0.0")]
1547    Infinite,
1548
1549    /// Positive or negative zero.
1550    ///
1551    /// See [the documentation for `f32`](f32) for more information on the signedness of zeroes.
1552    #[stable(feature = "rust1", since = "1.0.0")]
1553    Zero,
1554
1555    /// “Subnormal” or “denormal” floating point representation (less precise, relative to
1556    /// their magnitude, than [`Normal`]).
1557    ///
1558    /// Subnormal numbers are larger in magnitude than [`Zero`] but smaller in magnitude than all
1559    /// [`Normal`] numbers.
1560    ///
1561    /// [`Normal`]: Self::Normal
1562    /// [`Zero`]: Self::Zero
1563    #[stable(feature = "rust1", since = "1.0.0")]
1564    Subnormal,
1565
1566    /// A regular floating point number, not any of the exceptional categories.
1567    ///
1568    /// The smallest positive normal numbers are [`f32::MIN_POSITIVE`] and [`f64::MIN_POSITIVE`],
1569    /// and the largest positive normal numbers are [`f32::MAX`] and [`f64::MAX`]. (Unlike signed
1570    /// integers, floating point numbers are symmetric in their range, so negating any of these
1571    /// constants will produce their negative counterpart.)
1572    #[stable(feature = "rust1", since = "1.0.0")]
1573    Normal,
1574}
1575
1576/// Determines if a string of text of that length of that radix could be guaranteed to be
1577/// stored in the given type T.
1578/// Note that if the radix is known to the compiler, it is just the check of digits.len that
1579/// is done at runtime.
1580#[doc(hidden)]
1581#[inline(always)]
1582#[unstable(issue = "none", feature = "std_internals")]
1583#[ferrocene::prevalidated]
1584pub const fn can_not_overflow<T>(radix: u32, is_signed_ty: bool, digits: &[u8]) -> bool {
1585    radix <= 16 && digits.len() <= size_of::<T>() * 2 - is_signed_ty as usize
1586}
1587
1588#[cfg_attr(not(panic = "immediate-abort"), inline(never))]
1589#[cfg_attr(panic = "immediate-abort", inline)]
1590#[cold]
1591#[track_caller]
1592#[ferrocene::prevalidated]
1593const fn from_ascii_radix_panic(radix: u32) -> ! {
1594    const_panic!(
1595        "from_ascii_radix: radix must lie in the range `[2, 36]`",
1596        "from_ascii_radix: radix must lie in the range `[2, 36]` - found {radix}",
1597        radix: u32 = radix,
1598    )
1599}
1600
1601macro_rules! from_str_int_impl {
1602    ($signedness:ident $($int_ty:ty)+) => {$(
1603        #[stable(feature = "rust1", since = "1.0.0")]
1604        #[rustc_const_unstable(feature = "const_convert", issue = "143773")]
1605        impl const FromStr for $int_ty {
1606            type Err = ParseIntError;
1607
1608            /// Parses an integer from a string slice with decimal digits.
1609            ///
1610            /// The characters are expected to be an optional
1611            #[doc = sign_dependent_expr!{
1612                $signedness ?
1613                if signed {
1614                    " `+` or `-` "
1615                }
1616                if unsigned {
1617                    " `+` "
1618                }
1619            }]
1620            /// sign followed by only digits. Leading and trailing non-digit characters (including
1621            /// whitespace) represent an error. Underscores (which are accepted in Rust literals)
1622            /// also represent an error.
1623            ///
1624            /// # See also
1625            /// For parsing numbers in other bases, such as binary or hexadecimal,
1626            /// see [`from_str_radix`][Self::from_str_radix].
1627            ///
1628            /// # Examples
1629            ///
1630            /// ```
1631            /// use std::str::FromStr;
1632            ///
1633            #[doc = concat!("assert_eq!(", stringify!($int_ty), "::from_str(\"+10\"), Ok(10));")]
1634            /// ```
1635            /// Trailing space returns error:
1636            /// ```
1637            /// # use std::str::FromStr;
1638            /// #
1639            #[doc = concat!("assert!(", stringify!($int_ty), "::from_str(\"1 \").is_err());")]
1640            /// ```
1641            #[inline]
1642            fn from_str(src: &str) -> Result<$int_ty, ParseIntError> {
1643                <$int_ty>::from_str_radix(src, 10)
1644            }
1645        }
1646
1647        impl $int_ty {
1648            /// Parses an integer from a string slice with digits in a given base.
1649            ///
1650            /// The string is expected to be an optional
1651            #[doc = sign_dependent_expr!{
1652                $signedness ?
1653                if signed {
1654                    " `+` or `-` "
1655                }
1656                if unsigned {
1657                    " `+` "
1658                }
1659            }]
1660            /// sign followed by only digits. Leading and trailing non-digit characters (including
1661            /// whitespace) represent an error. Underscores (which are accepted in Rust literals)
1662            /// also represent an error.
1663            ///
1664            /// Digits are a subset of these characters, depending on `radix`:
1665            /// * `0-9`
1666            /// * `a-z`
1667            /// * `A-Z`
1668            ///
1669            /// # Panics
1670            ///
1671            /// This function panics if `radix` is not in the range from 2 to 36.
1672            ///
1673            /// # See also
1674            /// If the string to be parsed is in base 10 (decimal),
1675            /// [`from_str`] or [`str::parse`] can also be used.
1676            ///
1677            // FIXME(#122566): These HTML links work around a rustdoc-json test failure.
1678            /// [`from_str`]: #method.from_str
1679            /// [`str::parse`]: primitive.str.html#method.parse
1680            ///
1681            /// # Examples
1682            ///
1683            /// ```
1684            #[doc = concat!("assert_eq!(", stringify!($int_ty), "::from_str_radix(\"A\", 16), Ok(10));")]
1685            /// ```
1686            /// Trailing space returns error:
1687            /// ```
1688            #[doc = concat!("assert!(", stringify!($int_ty), "::from_str_radix(\"1 \", 10).is_err());")]
1689            /// ```
1690            #[stable(feature = "rust1", since = "1.0.0")]
1691            #[rustc_const_stable(feature = "const_int_from_str", since = "1.82.0")]
1692            #[inline]
1693            #[ferrocene::prevalidated]
1694            pub const fn from_str_radix(src: &str, radix: u32) -> Result<$int_ty, ParseIntError> {
1695                <$int_ty>::from_ascii_radix(src.as_bytes(), radix)
1696            }
1697
1698            /// Parses an integer from an ASCII-byte slice with decimal digits.
1699            ///
1700            /// The characters are expected to be an optional
1701            #[doc = sign_dependent_expr!{
1702                $signedness ?
1703                if signed {
1704                    " `+` or `-` "
1705                }
1706                if unsigned {
1707                    " `+` "
1708                }
1709            }]
1710            /// sign followed by only digits. Leading and trailing non-digit characters (including
1711            /// whitespace) represent an error. Underscores (which are accepted in Rust literals)
1712            /// also represent an error.
1713            ///
1714            /// # Examples
1715            ///
1716            /// ```
1717            /// #![feature(int_from_ascii)]
1718            ///
1719            #[doc = concat!("assert_eq!(", stringify!($int_ty), "::from_ascii(b\"+10\"), Ok(10));")]
1720            /// ```
1721            /// Trailing space returns error:
1722            /// ```
1723            /// # #![feature(int_from_ascii)]
1724            /// #
1725            #[doc = concat!("assert!(", stringify!($int_ty), "::from_ascii(b\"1 \").is_err());")]
1726            /// ```
1727            #[unstable(feature = "int_from_ascii", issue = "134821")]
1728            #[inline]
1729            pub const fn from_ascii(src: &[u8]) -> Result<$int_ty, ParseIntError> {
1730                <$int_ty>::from_ascii_radix(src, 10)
1731            }
1732
1733            /// Parses an integer from an ASCII-byte slice with digits in a given base.
1734            ///
1735            /// The characters are expected to be an optional
1736            #[doc = sign_dependent_expr!{
1737                $signedness ?
1738                if signed {
1739                    " `+` or `-` "
1740                }
1741                if unsigned {
1742                    " `+` "
1743                }
1744            }]
1745            /// sign followed by only digits. Leading and trailing non-digit characters (including
1746            /// whitespace) represent an error. Underscores (which are accepted in Rust literals)
1747            /// also represent an error.
1748            ///
1749            /// Digits are a subset of these characters, depending on `radix`:
1750            /// * `0-9`
1751            /// * `a-z`
1752            /// * `A-Z`
1753            ///
1754            /// # Panics
1755            ///
1756            /// This function panics if `radix` is not in the range from 2 to 36.
1757            ///
1758            /// # Examples
1759            ///
1760            /// ```
1761            /// #![feature(int_from_ascii)]
1762            ///
1763            #[doc = concat!("assert_eq!(", stringify!($int_ty), "::from_ascii_radix(b\"A\", 16), Ok(10));")]
1764            /// ```
1765            /// Trailing space returns error:
1766            /// ```
1767            /// # #![feature(int_from_ascii)]
1768            /// #
1769            #[doc = concat!("assert!(", stringify!($int_ty), "::from_ascii_radix(b\"1 \", 10).is_err());")]
1770            /// ```
1771            #[unstable(feature = "int_from_ascii", issue = "134821")]
1772            #[inline]
1773            #[ferrocene::prevalidated]
1774            pub const fn from_ascii_radix(src: &[u8], radix: u32) -> Result<$int_ty, ParseIntError> {
1775                use self::IntErrorKind::*;
1776                use self::ParseIntError as PIE;
1777
1778                if 2 > radix || radix > 36 {
1779                    from_ascii_radix_panic(radix);
1780                }
1781
1782                if src.is_empty() {
1783                    return Err(PIE { kind: Empty });
1784                }
1785
1786                #[allow(unused_comparisons)]
1787                let is_signed_ty = 0 > <$int_ty>::MIN;
1788
1789                let (is_positive, mut digits) = match src {
1790                    [b'+' | b'-'] => {
1791                        return Err(PIE { kind: InvalidDigit });
1792                    }
1793                    [b'+', rest @ ..] => (true, rest),
1794                    [b'-', rest @ ..] if is_signed_ty => (false, rest),
1795                    _ => (true, src),
1796                };
1797
1798                let mut result = 0;
1799
1800                macro_rules! unwrap_or_PIE {
1801                    ($option:expr, $kind:ident) => {
1802                        match $option {
1803                            Some(value) => value,
1804                            None => return Err(PIE { kind: $kind }),
1805                        }
1806                    };
1807                }
1808
1809                if can_not_overflow::<$int_ty>(radix, is_signed_ty, digits) {
1810                    // If the len of the str is short compared to the range of the type
1811                    // we are parsing into, then we can be certain that an overflow will not occur.
1812                    // This bound is when `radix.pow(digits.len()) - 1 <= T::MAX` but the condition
1813                    // above is a faster (conservative) approximation of this.
1814                    //
1815                    // Consider radix 16 as it has the highest information density per digit and will thus overflow the earliest:
1816                    // `u8::MAX` is `ff` - any str of len 2 is guaranteed to not overflow.
1817                    // `i8::MAX` is `7f` - only a str of len 1 is guaranteed to not overflow.
1818                    macro_rules! run_unchecked_loop {
1819                        ($unchecked_additive_op:tt) => {{
1820                            while let [c, rest @ ..] = digits {
1821                                result = result * (radix as $int_ty);
1822                                let x = unwrap_or_PIE!((*c as char).to_digit(radix), InvalidDigit);
1823                                result = result $unchecked_additive_op (x as $int_ty);
1824                                digits = rest;
1825                            }
1826                        }};
1827                    }
1828                    if is_positive {
1829                        run_unchecked_loop!(+)
1830                    } else {
1831                        run_unchecked_loop!(-)
1832                    };
1833                } else {
1834                    macro_rules! run_checked_loop {
1835                        ($checked_additive_op:ident, $overflow_err:ident) => {{
1836                            while let [c, rest @ ..] = digits {
1837                                // When `radix` is passed in as a literal, rather than doing a slow `imul`
1838                                // the compiler can use shifts if `radix` can be expressed as a
1839                                // sum of powers of 2 (x*10 can be written as x*8 + x*2).
1840                                // When the compiler can't use these optimisations,
1841                                // the latency of the multiplication can be hidden by issuing it
1842                                // before the result is needed to improve performance on
1843                                // modern out-of-order CPU as multiplication here is slower
1844                                // than the other instructions, we can get the end result faster
1845                                // doing multiplication first and let the CPU spends other cycles
1846                                // doing other computation and get multiplication result later.
1847                                let mul = result.checked_mul(radix as $int_ty);
1848                                let x = unwrap_or_PIE!((*c as char).to_digit(radix), InvalidDigit) as $int_ty;
1849                                result = unwrap_or_PIE!(mul, $overflow_err);
1850                                result = unwrap_or_PIE!(<$int_ty>::$checked_additive_op(result, x), $overflow_err);
1851                                digits = rest;
1852                            }
1853                        }};
1854                    }
1855                    if is_positive {
1856                        run_checked_loop!(checked_add, PosOverflow)
1857                    } else {
1858                        run_checked_loop!(checked_sub, NegOverflow)
1859                    };
1860                }
1861                Ok(result)
1862            }
1863        }
1864    )*}
1865}
1866
1867from_str_int_impl! { signed isize i8 i16 i32 i64 i128 }
1868from_str_int_impl! { unsigned usize u8 u16 u32 u64 u128 }
1869
1870macro_rules! impl_sealed {
1871    ($($t:ty)*) => {$(
1872        /// Allows extension traits within `core`.
1873        #[unstable(feature = "sealed", issue = "none")]
1874        impl crate::sealed::Sealed for $t {}
1875    )*}
1876}
1877impl_sealed! { isize i8 i16 i32 i64 i128 usize u8 u16 u32 u64 u128 }