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