1
//! Numeric traits and functions for the built-in numeric types.
2

            
3
#![stable(feature = "rust1", since = "1.0.0")]
4

            
5
#[cfg(feature = "ferrocene_certified")]
6
use crate::intrinsics;
7
#[cfg(not(feature = "ferrocene_certified"))]
8
use crate::panic::const_panic;
9
#[cfg(not(feature = "ferrocene_certified"))]
10
use crate::str::FromStr;
11
#[cfg(not(feature = "ferrocene_certified"))]
12
use crate::ub_checks::assert_unsafe_precondition;
13
#[cfg(not(feature = "ferrocene_certified"))]
14
use crate::{ascii, intrinsics, mem};
15

            
16
// FIXME(const-hack): Used because the `?` operator is not allowed in a const context.
17
#[cfg(not(feature = "ferrocene_certified"))]
18
macro_rules! try_opt {
19
    ($e:expr) => {
20
        match $e {
21
            Some(x) => x,
22
            None => return None,
23
        }
24
    };
25
}
26

            
27
// Use this when the generated code should differ between signed and unsigned types.
28
#[cfg(not(feature = "ferrocene_certified"))]
29
macro_rules! sign_dependent_expr {
30
    (signed ? if signed { $signed_case:expr } if unsigned { $unsigned_case:expr } ) => {
31
        $signed_case
32
    };
33
    (unsigned ? if signed { $signed_case:expr } if unsigned { $unsigned_case:expr } ) => {
34
        $unsigned_case
35
    };
36
}
37

            
38
// All these modules are technically private and only exposed for coretests:
39
#[cfg(not(no_fp_fmt_parse))]
40
#[cfg(not(feature = "ferrocene_certified"))]
41
pub mod bignum;
42
#[cfg(not(no_fp_fmt_parse))]
43
#[cfg(not(feature = "ferrocene_certified"))]
44
pub mod dec2flt;
45
#[cfg(not(no_fp_fmt_parse))]
46
#[cfg(not(feature = "ferrocene_certified"))]
47
pub mod diy_float;
48
#[cfg(not(no_fp_fmt_parse))]
49
#[cfg(not(feature = "ferrocene_certified"))]
50
pub mod flt2dec;
51
#[cfg(not(feature = "ferrocene_certified"))]
52
pub mod fmt;
53

            
54
#[macro_use]
55
mod int_macros; // import int_impl!
56
#[macro_use]
57
mod uint_macros; // import uint_impl!
58

            
59
#[cfg(not(feature = "ferrocene_certified"))]
60
mod error;
61
#[cfg(not(feature = "ferrocene_certified"))]
62
mod int_log10;
63
#[cfg(not(feature = "ferrocene_certified"))]
64
mod int_sqrt;
65
#[cfg(not(feature = "ferrocene_certified"))]
66
pub(crate) mod libm;
67
#[cfg(not(feature = "ferrocene_certified"))]
68
mod nonzero;
69
#[cfg(not(feature = "ferrocene_certified"))]
70
mod overflow_panic;
71
#[cfg(not(feature = "ferrocene_certified"))]
72
mod saturating;
73
#[cfg(not(feature = "ferrocene_certified"))]
74
mod wrapping;
75

            
76
/// 100% perma-unstable
77
#[doc(hidden)]
78
#[cfg(not(feature = "ferrocene_certified"))]
79
pub mod niche_types;
80

            
81
#[stable(feature = "rust1", since = "1.0.0")]
82
#[cfg(not(no_fp_fmt_parse))]
83
#[cfg(not(feature = "ferrocene_certified"))]
84
pub use dec2flt::ParseFloatError;
85
#[stable(feature = "int_error_matching", since = "1.55.0")]
86
#[cfg(not(feature = "ferrocene_certified"))]
87
pub use error::IntErrorKind;
88
#[stable(feature = "rust1", since = "1.0.0")]
89
#[cfg(not(feature = "ferrocene_certified"))]
90
pub use error::ParseIntError;
91
#[stable(feature = "try_from", since = "1.34.0")]
92
#[cfg(not(feature = "ferrocene_certified"))]
93
pub use error::TryFromIntError;
94
#[stable(feature = "generic_nonzero", since = "1.79.0")]
95
#[cfg(not(feature = "ferrocene_certified"))]
96
pub use nonzero::NonZero;
97
#[unstable(
98
    feature = "nonzero_internals",
99
    reason = "implementation detail which may disappear or be replaced at any time",
100
    issue = "none"
101
)]
102
#[cfg(not(feature = "ferrocene_certified"))]
103
pub use nonzero::ZeroablePrimitive;
104
#[stable(feature = "signed_nonzero", since = "1.34.0")]
105
#[cfg(not(feature = "ferrocene_certified"))]
106
pub use nonzero::{NonZeroI8, NonZeroI16, NonZeroI32, NonZeroI64, NonZeroI128, NonZeroIsize};
107
#[stable(feature = "nonzero", since = "1.28.0")]
108
#[cfg(not(feature = "ferrocene_certified"))]
109
pub use nonzero::{NonZeroU8, NonZeroU16, NonZeroU32, NonZeroU64, NonZeroU128, NonZeroUsize};
110
#[stable(feature = "saturating_int_impl", since = "1.74.0")]
111
#[cfg(not(feature = "ferrocene_certified"))]
112
pub use saturating::Saturating;
113
#[stable(feature = "rust1", since = "1.0.0")]
114
#[cfg(not(feature = "ferrocene_certified"))]
115
pub use wrapping::Wrapping;
116

            
117
#[cfg(not(feature = "ferrocene_certified"))]
118
macro_rules! u8_xe_bytes_doc {
119
    () => {
120
        "
121

            
122
**Note**: This function is meaningless on `u8`. Byte order does not exist as a
123
concept for byte-sized integers. This function is only provided in symmetry
124
with larger integer types.
125

            
126
"
127
    };
128
}
129

            
130
#[cfg(not(feature = "ferrocene_certified"))]
131
macro_rules! i8_xe_bytes_doc {
132
    () => {
133
        "
134

            
135
**Note**: This function is meaningless on `i8`. Byte order does not exist as a
136
concept for byte-sized integers. This function is only provided in symmetry
137
with larger integer types. You can cast from and to `u8` using
138
[`cast_signed`](u8::cast_signed) and [`cast_unsigned`](Self::cast_unsigned).
139

            
140
"
141
    };
142
}
143

            
144
#[cfg(not(feature = "ferrocene_certified"))]
145
macro_rules! usize_isize_to_xe_bytes_doc {
146
    () => {
147
        "
148

            
149
**Note**: This function returns an array of length 2, 4 or 8 bytes
150
depending on the target pointer size.
151

            
152
"
153
    };
154
}
155

            
156
#[cfg(not(feature = "ferrocene_certified"))]
157
macro_rules! usize_isize_from_xe_bytes_doc {
158
    () => {
159
        "
160

            
161
**Note**: This function takes an array of length 2, 4 or 8 bytes
162
depending on the target pointer size.
163

            
164
"
165
    };
166
}
167

            
168
#[cfg(not(feature = "ferrocene_certified"))]
169
macro_rules! midpoint_impl {
170
    ($SelfT:ty, unsigned) => {
171
        /// Calculates the midpoint (average) between `self` and `rhs`.
172
        ///
173
        /// `midpoint(a, b)` is `(a + b) / 2` as if it were performed in a
174
        /// sufficiently-large unsigned integral type. This implies that the result is
175
        /// always rounded towards zero and that no overflow will ever occur.
176
        ///
177
        /// # Examples
178
        ///
179
        /// ```
180
        #[doc = concat!("assert_eq!(0", stringify!($SelfT), ".midpoint(4), 2);")]
181
        #[doc = concat!("assert_eq!(1", stringify!($SelfT), ".midpoint(4), 2);")]
182
        /// ```
183
        #[stable(feature = "num_midpoint", since = "1.85.0")]
184
        #[rustc_const_stable(feature = "num_midpoint", since = "1.85.0")]
185
        #[must_use = "this returns the result of the operation, \
186
                      without modifying the original"]
187
        #[doc(alias = "average_floor")]
188
        #[doc(alias = "average")]
189
        #[inline]
190
        pub const fn midpoint(self, rhs: $SelfT) -> $SelfT {
191
            // Use the well known branchless algorithm from Hacker's Delight to compute
192
            // `(a + b) / 2` without overflowing: `((a ^ b) >> 1) + (a & b)`.
193
            ((self ^ rhs) >> 1) + (self & rhs)
194
        }
195
    };
196
    ($SelfT:ty, signed) => {
197
        /// Calculates the midpoint (average) between `self` and `rhs`.
198
        ///
199
        /// `midpoint(a, b)` is `(a + b) / 2` as if it were performed in a
200
        /// sufficiently-large signed integral type. This implies that the result is
201
        /// always rounded towards zero and that no overflow will ever occur.
202
        ///
203
        /// # Examples
204
        ///
205
        /// ```
206
        #[doc = concat!("assert_eq!(0", stringify!($SelfT), ".midpoint(4), 2);")]
207
        #[doc = concat!("assert_eq!((-1", stringify!($SelfT), ").midpoint(2), 0);")]
208
        #[doc = concat!("assert_eq!((-7", stringify!($SelfT), ").midpoint(0), -3);")]
209
        #[doc = concat!("assert_eq!(0", stringify!($SelfT), ".midpoint(-7), -3);")]
210
        #[doc = concat!("assert_eq!(0", stringify!($SelfT), ".midpoint(7), 3);")]
211
        /// ```
212
        #[stable(feature = "num_midpoint_signed", since = "1.87.0")]
213
        #[rustc_const_stable(feature = "num_midpoint_signed", since = "1.87.0")]
214
        #[must_use = "this returns the result of the operation, \
215
                      without modifying the original"]
216
        #[doc(alias = "average_floor")]
217
        #[doc(alias = "average_ceil")]
218
        #[doc(alias = "average")]
219
        #[inline]
220
        pub const fn midpoint(self, rhs: Self) -> Self {
221
            // Use the well known branchless algorithm from Hacker's Delight to compute
222
            // `(a + b) / 2` without overflowing: `((a ^ b) >> 1) + (a & b)`.
223
            let t = ((self ^ rhs) >> 1) + (self & rhs);
224
            // Except that it fails for integers whose sum is an odd negative number as
225
            // their floor is one less than their average. So we adjust the result.
226
            t + (if t < 0 { 1 } else { 0 } & (self ^ rhs))
227
        }
228
    };
229
    ($SelfT:ty, $WideT:ty, unsigned) => {
230
        /// Calculates the midpoint (average) between `self` and `rhs`.
231
        ///
232
        /// `midpoint(a, b)` is `(a + b) / 2` as if it were performed in a
233
        /// sufficiently-large unsigned integral type. This implies that the result is
234
        /// always rounded towards zero and that no overflow will ever occur.
235
        ///
236
        /// # Examples
237
        ///
238
        /// ```
239
        #[doc = concat!("assert_eq!(0", stringify!($SelfT), ".midpoint(4), 2);")]
240
        #[doc = concat!("assert_eq!(1", stringify!($SelfT), ".midpoint(4), 2);")]
241
        /// ```
242
        #[stable(feature = "num_midpoint", since = "1.85.0")]
243
        #[rustc_const_stable(feature = "num_midpoint", since = "1.85.0")]
244
        #[must_use = "this returns the result of the operation, \
245
                      without modifying the original"]
246
        #[doc(alias = "average_floor")]
247
        #[doc(alias = "average")]
248
        #[inline]
249
        pub const fn midpoint(self, rhs: $SelfT) -> $SelfT {
250
            ((self as $WideT + rhs as $WideT) / 2) as $SelfT
251
        }
252
    };
253
    ($SelfT:ty, $WideT:ty, signed) => {
254
        /// Calculates the midpoint (average) between `self` and `rhs`.
255
        ///
256
        /// `midpoint(a, b)` is `(a + b) / 2` as if it were performed in a
257
        /// sufficiently-large signed integral type. This implies that the result is
258
        /// always rounded towards zero and that no overflow will ever occur.
259
        ///
260
        /// # Examples
261
        ///
262
        /// ```
263
        #[doc = concat!("assert_eq!(0", stringify!($SelfT), ".midpoint(4), 2);")]
264
        #[doc = concat!("assert_eq!((-1", stringify!($SelfT), ").midpoint(2), 0);")]
265
        #[doc = concat!("assert_eq!((-7", stringify!($SelfT), ").midpoint(0), -3);")]
266
        #[doc = concat!("assert_eq!(0", stringify!($SelfT), ".midpoint(-7), -3);")]
267
        #[doc = concat!("assert_eq!(0", stringify!($SelfT), ".midpoint(7), 3);")]
268
        /// ```
269
        #[stable(feature = "num_midpoint_signed", since = "1.87.0")]
270
        #[rustc_const_stable(feature = "num_midpoint_signed", since = "1.87.0")]
271
        #[must_use = "this returns the result of the operation, \
272
                      without modifying the original"]
273
        #[doc(alias = "average_floor")]
274
        #[doc(alias = "average_ceil")]
275
        #[doc(alias = "average")]
276
        #[inline]
277
        pub const fn midpoint(self, rhs: $SelfT) -> $SelfT {
278
            ((self as $WideT + rhs as $WideT) / 2) as $SelfT
279
        }
280
    };
281
}
282

            
283
impl i8 {
284
    int_impl! {
285
        Self = i8,
286
        ActualT = i8,
287
        UnsignedT = u8,
288
        BITS = 8,
289
        BITS_MINUS_ONE = 7,
290
        Min = -128,
291
        Max = 127,
292
        rot = 2,
293
        rot_op = "-0x7e",
294
        rot_result = "0xa",
295
        swap_op = "0x12",
296
        swapped = "0x12",
297
        reversed = "0x48",
298
        le_bytes = "[0x12]",
299
        be_bytes = "[0x12]",
300
        to_xe_bytes_doc = i8_xe_bytes_doc!(),
301
        from_xe_bytes_doc = i8_xe_bytes_doc!(),
302
        bound_condition = "",
303
    }
304
    #[cfg(not(feature = "ferrocene_certified"))]
305
    midpoint_impl! { i8, i16, signed }
306
}
307

            
308
impl i16 {
309
    int_impl! {
310
        Self = i16,
311
        ActualT = i16,
312
        UnsignedT = u16,
313
        BITS = 16,
314
        BITS_MINUS_ONE = 15,
315
        Min = -32768,
316
        Max = 32767,
317
        rot = 4,
318
        rot_op = "-0x5ffd",
319
        rot_result = "0x3a",
320
        swap_op = "0x1234",
321
        swapped = "0x3412",
322
        reversed = "0x2c48",
323
        le_bytes = "[0x34, 0x12]",
324
        be_bytes = "[0x12, 0x34]",
325
        to_xe_bytes_doc = "",
326
        from_xe_bytes_doc = "",
327
        bound_condition = "",
328
    }
329
    #[cfg(not(feature = "ferrocene_certified"))]
330
    midpoint_impl! { i16, i32, signed }
331
}
332

            
333
impl i32 {
334
    int_impl! {
335
        Self = i32,
336
        ActualT = i32,
337
        UnsignedT = u32,
338
        BITS = 32,
339
        BITS_MINUS_ONE = 31,
340
        Min = -2147483648,
341
        Max = 2147483647,
342
        rot = 8,
343
        rot_op = "0x10000b3",
344
        rot_result = "0xb301",
345
        swap_op = "0x12345678",
346
        swapped = "0x78563412",
347
        reversed = "0x1e6a2c48",
348
        le_bytes = "[0x78, 0x56, 0x34, 0x12]",
349
        be_bytes = "[0x12, 0x34, 0x56, 0x78]",
350
        to_xe_bytes_doc = "",
351
        from_xe_bytes_doc = "",
352
        bound_condition = "",
353
    }
354
    #[cfg(not(feature = "ferrocene_certified"))]
355
    midpoint_impl! { i32, i64, signed }
356
}
357

            
358
impl i64 {
359
    int_impl! {
360
        Self = i64,
361
        ActualT = i64,
362
        UnsignedT = u64,
363
        BITS = 64,
364
        BITS_MINUS_ONE = 63,
365
        Min = -9223372036854775808,
366
        Max = 9223372036854775807,
367
        rot = 12,
368
        rot_op = "0xaa00000000006e1",
369
        rot_result = "0x6e10aa",
370
        swap_op = "0x1234567890123456",
371
        swapped = "0x5634129078563412",
372
        reversed = "0x6a2c48091e6a2c48",
373
        le_bytes = "[0x56, 0x34, 0x12, 0x90, 0x78, 0x56, 0x34, 0x12]",
374
        be_bytes = "[0x12, 0x34, 0x56, 0x78, 0x90, 0x12, 0x34, 0x56]",
375
        to_xe_bytes_doc = "",
376
        from_xe_bytes_doc = "",
377
        bound_condition = "",
378
    }
379
    #[cfg(not(feature = "ferrocene_certified"))]
380
    midpoint_impl! { i64, signed }
381
}
382

            
383
impl i128 {
384
    int_impl! {
385
        Self = i128,
386
        ActualT = i128,
387
        UnsignedT = u128,
388
        BITS = 128,
389
        BITS_MINUS_ONE = 127,
390
        Min = -170141183460469231731687303715884105728,
391
        Max = 170141183460469231731687303715884105727,
392
        rot = 16,
393
        rot_op = "0x13f40000000000000000000000004f76",
394
        rot_result = "0x4f7613f4",
395
        swap_op = "0x12345678901234567890123456789012",
396
        swapped = "0x12907856341290785634129078563412",
397
        reversed = "0x48091e6a2c48091e6a2c48091e6a2c48",
398
        le_bytes = "[0x12, 0x90, 0x78, 0x56, 0x34, 0x12, 0x90, 0x78, \
399
            0x56, 0x34, 0x12, 0x90, 0x78, 0x56, 0x34, 0x12]",
400
        be_bytes = "[0x12, 0x34, 0x56, 0x78, 0x90, 0x12, 0x34, 0x56, \
401
            0x78, 0x90, 0x12, 0x34, 0x56, 0x78, 0x90, 0x12]",
402
        to_xe_bytes_doc = "",
403
        from_xe_bytes_doc = "",
404
        bound_condition = "",
405
    }
406
    #[cfg(not(feature = "ferrocene_certified"))]
407
    midpoint_impl! { i128, signed }
408
}
409

            
410
#[cfg(target_pointer_width = "16")]
411
impl isize {
412
    int_impl! {
413
        Self = isize,
414
        ActualT = i16,
415
        UnsignedT = usize,
416
        BITS = 16,
417
        BITS_MINUS_ONE = 15,
418
        Min = -32768,
419
        Max = 32767,
420
        rot = 4,
421
        rot_op = "-0x5ffd",
422
        rot_result = "0x3a",
423
        swap_op = "0x1234",
424
        swapped = "0x3412",
425
        reversed = "0x2c48",
426
        le_bytes = "[0x34, 0x12]",
427
        be_bytes = "[0x12, 0x34]",
428
        to_xe_bytes_doc = usize_isize_to_xe_bytes_doc!(),
429
        from_xe_bytes_doc = usize_isize_from_xe_bytes_doc!(),
430
        bound_condition = " on 16-bit targets",
431
    }
432
    #[cfg(not(feature = "ferrocene_certified"))]
433
    midpoint_impl! { isize, i32, signed }
434
}
435

            
436
#[cfg(target_pointer_width = "32")]
437
impl isize {
438
    int_impl! {
439
        Self = isize,
440
        ActualT = i32,
441
        UnsignedT = usize,
442
        BITS = 32,
443
        BITS_MINUS_ONE = 31,
444
        Min = -2147483648,
445
        Max = 2147483647,
446
        rot = 8,
447
        rot_op = "0x10000b3",
448
        rot_result = "0xb301",
449
        swap_op = "0x12345678",
450
        swapped = "0x78563412",
451
        reversed = "0x1e6a2c48",
452
        le_bytes = "[0x78, 0x56, 0x34, 0x12]",
453
        be_bytes = "[0x12, 0x34, 0x56, 0x78]",
454
        to_xe_bytes_doc = usize_isize_to_xe_bytes_doc!(),
455
        from_xe_bytes_doc = usize_isize_from_xe_bytes_doc!(),
456
        bound_condition = " on 32-bit targets",
457
    }
458
    #[cfg(not(feature = "ferrocene_certified"))]
459
    midpoint_impl! { isize, i64, signed }
460
}
461

            
462
#[cfg(target_pointer_width = "64")]
463
impl isize {
464
    int_impl! {
465
        Self = isize,
466
        ActualT = i64,
467
        UnsignedT = usize,
468
        BITS = 64,
469
        BITS_MINUS_ONE = 63,
470
        Min = -9223372036854775808,
471
        Max = 9223372036854775807,
472
        rot = 12,
473
        rot_op = "0xaa00000000006e1",
474
        rot_result = "0x6e10aa",
475
        swap_op = "0x1234567890123456",
476
        swapped = "0x5634129078563412",
477
        reversed = "0x6a2c48091e6a2c48",
478
        le_bytes = "[0x56, 0x34, 0x12, 0x90, 0x78, 0x56, 0x34, 0x12]",
479
        be_bytes = "[0x12, 0x34, 0x56, 0x78, 0x90, 0x12, 0x34, 0x56]",
480
        to_xe_bytes_doc = usize_isize_to_xe_bytes_doc!(),
481
        from_xe_bytes_doc = usize_isize_from_xe_bytes_doc!(),
482
        bound_condition = " on 64-bit targets",
483
    }
484
    #[cfg(not(feature = "ferrocene_certified"))]
485
    midpoint_impl! { isize, signed }
486
}
487

            
488
/// If the bit selected by this mask is set, ascii is lower case.
489
#[cfg(not(feature = "ferrocene_certified"))]
490
const ASCII_CASE_MASK: u8 = 0b0010_0000;
491

            
492
impl u8 {
493
    uint_impl! {
494
        Self = u8,
495
        ActualT = u8,
496
        SignedT = i8,
497
        BITS = 8,
498
        BITS_MINUS_ONE = 7,
499
        MAX = 255,
500
        rot = 2,
501
        rot_op = "0x82",
502
        rot_result = "0xa",
503
        fsh_op = "0x36",
504
        fshl_result = "0x8",
505
        fshr_result = "0x8d",
506
        swap_op = "0x12",
507
        swapped = "0x12",
508
        reversed = "0x48",
509
        le_bytes = "[0x12]",
510
        be_bytes = "[0x12]",
511
        to_xe_bytes_doc = u8_xe_bytes_doc!(),
512
        from_xe_bytes_doc = u8_xe_bytes_doc!(),
513
        bound_condition = "",
514
    }
515
    #[cfg(not(feature = "ferrocene_certified"))]
516
    midpoint_impl! { u8, u16, unsigned }
517

            
518
    /// Checks if the value is within the ASCII range.
519
    ///
520
    /// # Examples
521
    ///
522
    /// ```
523
    /// let ascii = 97u8;
524
    /// let non_ascii = 150u8;
525
    ///
526
    /// assert!(ascii.is_ascii());
527
    /// assert!(!non_ascii.is_ascii());
528
    /// ```
529
    #[must_use]
530
    #[stable(feature = "ascii_methods_on_intrinsics", since = "1.23.0")]
531
    #[rustc_const_stable(feature = "const_u8_is_ascii", since = "1.43.0")]
532
    #[inline]
533
    #[cfg(not(feature = "ferrocene_certified"))]
534
    pub const fn is_ascii(&self) -> bool {
535
        *self <= 127
536
    }
537

            
538
    /// If the value of this byte is within the ASCII range, returns it as an
539
    /// [ASCII character](ascii::Char).  Otherwise, returns `None`.
540
    #[must_use]
541
    #[unstable(feature = "ascii_char", issue = "110998")]
542
    #[inline]
543
    #[cfg(not(feature = "ferrocene_certified"))]
544
    pub const fn as_ascii(&self) -> Option<ascii::Char> {
545
        ascii::Char::from_u8(*self)
546
    }
547

            
548
    /// Converts this byte to an [ASCII character](ascii::Char), without
549
    /// checking whether or not it's valid.
550
    ///
551
    /// # Safety
552
    ///
553
    /// This byte must be valid ASCII, or else this is UB.
554
    #[must_use]
555
    #[unstable(feature = "ascii_char", issue = "110998")]
556
    #[inline]
557
    #[cfg(not(feature = "ferrocene_certified"))]
558
    pub const unsafe fn as_ascii_unchecked(&self) -> ascii::Char {
559
        assert_unsafe_precondition!(
560
            check_library_ub,
561
            "as_ascii_unchecked requires that the byte is valid ASCII",
562
            (it: &u8 = self) => it.is_ascii()
563
        );
564

            
565
        // SAFETY: the caller promised that this byte is ASCII.
566
        unsafe { ascii::Char::from_u8_unchecked(*self) }
567
    }
568

            
569
    /// Makes a copy of the value in its ASCII upper case equivalent.
570
    ///
571
    /// ASCII letters 'a' to 'z' are mapped to 'A' to 'Z',
572
    /// but non-ASCII letters are unchanged.
573
    ///
574
    /// To uppercase the value in-place, use [`make_ascii_uppercase`].
575
    ///
576
    /// # Examples
577
    ///
578
    /// ```
579
    /// let lowercase_a = 97u8;
580
    ///
581
    /// assert_eq!(65, lowercase_a.to_ascii_uppercase());
582
    /// ```
583
    ///
584
    /// [`make_ascii_uppercase`]: Self::make_ascii_uppercase
585
    #[must_use = "to uppercase the value in-place, use `make_ascii_uppercase()`"]
586
    #[stable(feature = "ascii_methods_on_intrinsics", since = "1.23.0")]
587
    #[rustc_const_stable(feature = "const_ascii_methods_on_intrinsics", since = "1.52.0")]
588
    #[inline]
589
    #[cfg(not(feature = "ferrocene_certified"))]
590
    pub const fn to_ascii_uppercase(&self) -> u8 {
591
        // Toggle the 6th bit if this is a lowercase letter
592
        *self ^ ((self.is_ascii_lowercase() as u8) * ASCII_CASE_MASK)
593
    }
594

            
595
    /// Makes a copy of the value in its ASCII lower case equivalent.
596
    ///
597
    /// ASCII letters 'A' to 'Z' are mapped to 'a' to 'z',
598
    /// but non-ASCII letters are unchanged.
599
    ///
600
    /// To lowercase the value in-place, use [`make_ascii_lowercase`].
601
    ///
602
    /// # Examples
603
    ///
604
    /// ```
605
    /// let uppercase_a = 65u8;
606
    ///
607
    /// assert_eq!(97, uppercase_a.to_ascii_lowercase());
608
    /// ```
609
    ///
610
    /// [`make_ascii_lowercase`]: Self::make_ascii_lowercase
611
    #[must_use = "to lowercase the value in-place, use `make_ascii_lowercase()`"]
612
    #[stable(feature = "ascii_methods_on_intrinsics", since = "1.23.0")]
613
    #[rustc_const_stable(feature = "const_ascii_methods_on_intrinsics", since = "1.52.0")]
614
    #[inline]
615
    #[cfg(not(feature = "ferrocene_certified"))]
616
    pub const fn to_ascii_lowercase(&self) -> u8 {
617
        // Set the 6th bit if this is an uppercase letter
618
        *self | (self.is_ascii_uppercase() as u8 * ASCII_CASE_MASK)
619
    }
620

            
621
    /// Assumes self is ascii
622
    #[inline]
623
    #[cfg(not(feature = "ferrocene_certified"))]
624
    pub(crate) const fn ascii_change_case_unchecked(&self) -> u8 {
625
        *self ^ ASCII_CASE_MASK
626
    }
627

            
628
    /// Checks that two values are an ASCII case-insensitive match.
629
    ///
630
    /// This is equivalent to `to_ascii_lowercase(a) == to_ascii_lowercase(b)`.
631
    ///
632
    /// # Examples
633
    ///
634
    /// ```
635
    /// let lowercase_a = 97u8;
636
    /// let uppercase_a = 65u8;
637
    ///
638
    /// assert!(lowercase_a.eq_ignore_ascii_case(&uppercase_a));
639
    /// ```
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
    #[cfg(not(feature = "ferrocene_certified"))]
644
    pub const fn eq_ignore_ascii_case(&self, other: &u8) -> bool {
645
        self.to_ascii_lowercase() == other.to_ascii_lowercase()
646
    }
647

            
648
    /// Converts this value to its ASCII upper case equivalent in-place.
649
    ///
650
    /// ASCII letters 'a' to 'z' are mapped to 'A' to 'Z',
651
    /// but non-ASCII letters are unchanged.
652
    ///
653
    /// To return a new uppercased value without modifying the existing one, use
654
    /// [`to_ascii_uppercase`].
655
    ///
656
    /// # Examples
657
    ///
658
    /// ```
659
    /// let mut byte = b'a';
660
    ///
661
    /// byte.make_ascii_uppercase();
662
    ///
663
    /// assert_eq!(b'A', byte);
664
    /// ```
665
    ///
666
    /// [`to_ascii_uppercase`]: Self::to_ascii_uppercase
667
    #[stable(feature = "ascii_methods_on_intrinsics", since = "1.23.0")]
668
    #[rustc_const_stable(feature = "const_make_ascii", since = "1.84.0")]
669
    #[inline]
670
    #[cfg(not(feature = "ferrocene_certified"))]
671
    pub const fn make_ascii_uppercase(&mut self) {
672
        *self = self.to_ascii_uppercase();
673
    }
674

            
675
    /// Converts this value to its ASCII lower case equivalent in-place.
676
    ///
677
    /// ASCII letters 'A' to 'Z' are mapped to 'a' to 'z',
678
    /// but non-ASCII letters are unchanged.
679
    ///
680
    /// To return a new lowercased value without modifying the existing one, use
681
    /// [`to_ascii_lowercase`].
682
    ///
683
    /// # Examples
684
    ///
685
    /// ```
686
    /// let mut byte = b'A';
687
    ///
688
    /// byte.make_ascii_lowercase();
689
    ///
690
    /// assert_eq!(b'a', byte);
691
    /// ```
692
    ///
693
    /// [`to_ascii_lowercase`]: Self::to_ascii_lowercase
694
    #[stable(feature = "ascii_methods_on_intrinsics", since = "1.23.0")]
695
    #[rustc_const_stable(feature = "const_make_ascii", since = "1.84.0")]
696
    #[inline]
697
    #[cfg(not(feature = "ferrocene_certified"))]
698
    pub const fn make_ascii_lowercase(&mut self) {
699
        *self = self.to_ascii_lowercase();
700
    }
701

            
702
    /// Checks if the value is an ASCII alphabetic character:
703
    ///
704
    /// - U+0041 'A' ..= U+005A 'Z', or
705
    /// - U+0061 'a' ..= U+007A 'z'.
706
    ///
707
    /// # Examples
708
    ///
709
    /// ```
710
    /// let uppercase_a = b'A';
711
    /// let uppercase_g = b'G';
712
    /// let a = b'a';
713
    /// let g = b'g';
714
    /// let zero = b'0';
715
    /// let percent = b'%';
716
    /// let space = b' ';
717
    /// let lf = b'\n';
718
    /// let esc = b'\x1b';
719
    ///
720
    /// assert!(uppercase_a.is_ascii_alphabetic());
721
    /// assert!(uppercase_g.is_ascii_alphabetic());
722
    /// assert!(a.is_ascii_alphabetic());
723
    /// assert!(g.is_ascii_alphabetic());
724
    /// assert!(!zero.is_ascii_alphabetic());
725
    /// assert!(!percent.is_ascii_alphabetic());
726
    /// assert!(!space.is_ascii_alphabetic());
727
    /// assert!(!lf.is_ascii_alphabetic());
728
    /// assert!(!esc.is_ascii_alphabetic());
729
    /// ```
730
    #[must_use]
731
    #[stable(feature = "ascii_ctype_on_intrinsics", since = "1.24.0")]
732
    #[rustc_const_stable(feature = "const_ascii_ctype_on_intrinsics", since = "1.47.0")]
733
    #[inline]
734
    #[cfg(not(feature = "ferrocene_certified"))]
735
    pub const fn is_ascii_alphabetic(&self) -> bool {
736
        matches!(*self, b'A'..=b'Z' | b'a'..=b'z')
737
    }
738

            
739
    /// Checks if the value is an ASCII uppercase character:
740
    /// U+0041 'A' ..= U+005A 'Z'.
741
    ///
742
    /// # Examples
743
    ///
744
    /// ```
745
    /// let uppercase_a = b'A';
746
    /// let uppercase_g = b'G';
747
    /// let a = b'a';
748
    /// let g = b'g';
749
    /// let zero = b'0';
750
    /// let percent = b'%';
751
    /// let space = b' ';
752
    /// let lf = b'\n';
753
    /// let esc = b'\x1b';
754
    ///
755
    /// assert!(uppercase_a.is_ascii_uppercase());
756
    /// assert!(uppercase_g.is_ascii_uppercase());
757
    /// assert!(!a.is_ascii_uppercase());
758
    /// assert!(!g.is_ascii_uppercase());
759
    /// assert!(!zero.is_ascii_uppercase());
760
    /// assert!(!percent.is_ascii_uppercase());
761
    /// assert!(!space.is_ascii_uppercase());
762
    /// assert!(!lf.is_ascii_uppercase());
763
    /// assert!(!esc.is_ascii_uppercase());
764
    /// ```
765
    #[must_use]
766
    #[stable(feature = "ascii_ctype_on_intrinsics", since = "1.24.0")]
767
    #[rustc_const_stable(feature = "const_ascii_ctype_on_intrinsics", since = "1.47.0")]
768
    #[inline]
769
    #[cfg(not(feature = "ferrocene_certified"))]
770
    pub const fn is_ascii_uppercase(&self) -> bool {
771
        matches!(*self, b'A'..=b'Z')
772
    }
773

            
774
    /// Checks if the value is an ASCII lowercase character:
775
    /// U+0061 'a' ..= U+007A 'z'.
776
    ///
777
    /// # Examples
778
    ///
779
    /// ```
780
    /// let uppercase_a = b'A';
781
    /// let uppercase_g = b'G';
782
    /// let a = b'a';
783
    /// let g = b'g';
784
    /// let zero = b'0';
785
    /// let percent = b'%';
786
    /// let space = b' ';
787
    /// let lf = b'\n';
788
    /// let esc = b'\x1b';
789
    ///
790
    /// assert!(!uppercase_a.is_ascii_lowercase());
791
    /// assert!(!uppercase_g.is_ascii_lowercase());
792
    /// assert!(a.is_ascii_lowercase());
793
    /// assert!(g.is_ascii_lowercase());
794
    /// assert!(!zero.is_ascii_lowercase());
795
    /// assert!(!percent.is_ascii_lowercase());
796
    /// assert!(!space.is_ascii_lowercase());
797
    /// assert!(!lf.is_ascii_lowercase());
798
    /// assert!(!esc.is_ascii_lowercase());
799
    /// ```
800
    #[must_use]
801
    #[stable(feature = "ascii_ctype_on_intrinsics", since = "1.24.0")]
802
    #[rustc_const_stable(feature = "const_ascii_ctype_on_intrinsics", since = "1.47.0")]
803
    #[inline]
804
    #[cfg(not(feature = "ferrocene_certified"))]
805
    pub const fn is_ascii_lowercase(&self) -> bool {
806
        matches!(*self, b'a'..=b'z')
807
    }
808

            
809
    /// Checks if the value is an ASCII alphanumeric character:
810
    ///
811
    /// - U+0041 'A' ..= U+005A 'Z', or
812
    /// - U+0061 'a' ..= U+007A 'z', or
813
    /// - U+0030 '0' ..= U+0039 '9'.
814
    ///
815
    /// # Examples
816
    ///
817
    /// ```
818
    /// let uppercase_a = b'A';
819
    /// let uppercase_g = b'G';
820
    /// let a = b'a';
821
    /// let g = b'g';
822
    /// let zero = b'0';
823
    /// let percent = b'%';
824
    /// let space = b' ';
825
    /// let lf = b'\n';
826
    /// let esc = b'\x1b';
827
    ///
828
    /// assert!(uppercase_a.is_ascii_alphanumeric());
829
    /// assert!(uppercase_g.is_ascii_alphanumeric());
830
    /// assert!(a.is_ascii_alphanumeric());
831
    /// assert!(g.is_ascii_alphanumeric());
832
    /// assert!(zero.is_ascii_alphanumeric());
833
    /// assert!(!percent.is_ascii_alphanumeric());
834
    /// assert!(!space.is_ascii_alphanumeric());
835
    /// assert!(!lf.is_ascii_alphanumeric());
836
    /// assert!(!esc.is_ascii_alphanumeric());
837
    /// ```
838
    #[must_use]
839
    #[stable(feature = "ascii_ctype_on_intrinsics", since = "1.24.0")]
840
    #[rustc_const_stable(feature = "const_ascii_ctype_on_intrinsics", since = "1.47.0")]
841
    #[inline]
842
    #[cfg(not(feature = "ferrocene_certified"))]
843
    pub const fn is_ascii_alphanumeric(&self) -> bool {
844
        matches!(*self, b'0'..=b'9') | matches!(*self, b'A'..=b'Z') | matches!(*self, b'a'..=b'z')
845
    }
846

            
847
    /// Checks if the value is an ASCII decimal digit:
848
    /// U+0030 '0' ..= U+0039 '9'.
849
    ///
850
    /// # Examples
851
    ///
852
    /// ```
853
    /// let uppercase_a = b'A';
854
    /// let uppercase_g = b'G';
855
    /// let a = b'a';
856
    /// let g = b'g';
857
    /// let zero = b'0';
858
    /// let percent = b'%';
859
    /// let space = b' ';
860
    /// let lf = b'\n';
861
    /// let esc = b'\x1b';
862
    ///
863
    /// assert!(!uppercase_a.is_ascii_digit());
864
    /// assert!(!uppercase_g.is_ascii_digit());
865
    /// assert!(!a.is_ascii_digit());
866
    /// assert!(!g.is_ascii_digit());
867
    /// assert!(zero.is_ascii_digit());
868
    /// assert!(!percent.is_ascii_digit());
869
    /// assert!(!space.is_ascii_digit());
870
    /// assert!(!lf.is_ascii_digit());
871
    /// assert!(!esc.is_ascii_digit());
872
    /// ```
873
    #[must_use]
874
    #[stable(feature = "ascii_ctype_on_intrinsics", since = "1.24.0")]
875
    #[rustc_const_stable(feature = "const_ascii_ctype_on_intrinsics", since = "1.47.0")]
876
    #[inline]
877
    #[cfg(not(feature = "ferrocene_certified"))]
878
    pub const fn is_ascii_digit(&self) -> bool {
879
        matches!(*self, b'0'..=b'9')
880
    }
881

            
882
    /// Checks if the value is an ASCII octal digit:
883
    /// U+0030 '0' ..= U+0037 '7'.
884
    ///
885
    /// # Examples
886
    ///
887
    /// ```
888
    /// #![feature(is_ascii_octdigit)]
889
    ///
890
    /// let uppercase_a = b'A';
891
    /// let a = b'a';
892
    /// let zero = b'0';
893
    /// let seven = b'7';
894
    /// let nine = b'9';
895
    /// let percent = b'%';
896
    /// let lf = b'\n';
897
    ///
898
    /// assert!(!uppercase_a.is_ascii_octdigit());
899
    /// assert!(!a.is_ascii_octdigit());
900
    /// assert!(zero.is_ascii_octdigit());
901
    /// assert!(seven.is_ascii_octdigit());
902
    /// assert!(!nine.is_ascii_octdigit());
903
    /// assert!(!percent.is_ascii_octdigit());
904
    /// assert!(!lf.is_ascii_octdigit());
905
    /// ```
906
    #[must_use]
907
    #[unstable(feature = "is_ascii_octdigit", issue = "101288")]
908
    #[inline]
909
    #[cfg(not(feature = "ferrocene_certified"))]
910
    pub const fn is_ascii_octdigit(&self) -> bool {
911
        matches!(*self, b'0'..=b'7')
912
    }
913

            
914
    /// Checks if the value is an ASCII hexadecimal digit:
915
    ///
916
    /// - U+0030 '0' ..= U+0039 '9', or
917
    /// - U+0041 'A' ..= U+0046 'F', or
918
    /// - U+0061 'a' ..= U+0066 'f'.
919
    ///
920
    /// # Examples
921
    ///
922
    /// ```
923
    /// let uppercase_a = b'A';
924
    /// let uppercase_g = b'G';
925
    /// let a = b'a';
926
    /// let g = b'g';
927
    /// let zero = b'0';
928
    /// let percent = b'%';
929
    /// let space = b' ';
930
    /// let lf = b'\n';
931
    /// let esc = b'\x1b';
932
    ///
933
    /// assert!(uppercase_a.is_ascii_hexdigit());
934
    /// assert!(!uppercase_g.is_ascii_hexdigit());
935
    /// assert!(a.is_ascii_hexdigit());
936
    /// assert!(!g.is_ascii_hexdigit());
937
    /// assert!(zero.is_ascii_hexdigit());
938
    /// assert!(!percent.is_ascii_hexdigit());
939
    /// assert!(!space.is_ascii_hexdigit());
940
    /// assert!(!lf.is_ascii_hexdigit());
941
    /// assert!(!esc.is_ascii_hexdigit());
942
    /// ```
943
    #[must_use]
944
    #[stable(feature = "ascii_ctype_on_intrinsics", since = "1.24.0")]
945
    #[rustc_const_stable(feature = "const_ascii_ctype_on_intrinsics", since = "1.47.0")]
946
    #[inline]
947
    #[cfg(not(feature = "ferrocene_certified"))]
948
    pub const fn is_ascii_hexdigit(&self) -> bool {
949
        matches!(*self, b'0'..=b'9') | matches!(*self, b'A'..=b'F') | matches!(*self, b'a'..=b'f')
950
    }
951

            
952
    /// Checks if the value is an ASCII punctuation character:
953
    ///
954
    /// - U+0021 ..= U+002F `! " # $ % & ' ( ) * + , - . /`, or
955
    /// - U+003A ..= U+0040 `: ; < = > ? @`, or
956
    /// - U+005B ..= U+0060 `` [ \ ] ^ _ ` ``, or
957
    /// - U+007B ..= U+007E `{ | } ~`
958
    ///
959
    /// # Examples
960
    ///
961
    /// ```
962
    /// let uppercase_a = b'A';
963
    /// let uppercase_g = b'G';
964
    /// let a = b'a';
965
    /// let g = b'g';
966
    /// let zero = b'0';
967
    /// let percent = b'%';
968
    /// let space = b' ';
969
    /// let lf = b'\n';
970
    /// let esc = b'\x1b';
971
    ///
972
    /// assert!(!uppercase_a.is_ascii_punctuation());
973
    /// assert!(!uppercase_g.is_ascii_punctuation());
974
    /// assert!(!a.is_ascii_punctuation());
975
    /// assert!(!g.is_ascii_punctuation());
976
    /// assert!(!zero.is_ascii_punctuation());
977
    /// assert!(percent.is_ascii_punctuation());
978
    /// assert!(!space.is_ascii_punctuation());
979
    /// assert!(!lf.is_ascii_punctuation());
980
    /// assert!(!esc.is_ascii_punctuation());
981
    /// ```
982
    #[must_use]
983
    #[stable(feature = "ascii_ctype_on_intrinsics", since = "1.24.0")]
984
    #[rustc_const_stable(feature = "const_ascii_ctype_on_intrinsics", since = "1.47.0")]
985
    #[inline]
986
    #[cfg(not(feature = "ferrocene_certified"))]
987
    pub const fn is_ascii_punctuation(&self) -> bool {
988
        matches!(*self, b'!'..=b'/')
989
            | matches!(*self, b':'..=b'@')
990
            | matches!(*self, b'['..=b'`')
991
            | matches!(*self, b'{'..=b'~')
992
    }
993

            
994
    /// Checks if the value is an ASCII graphic character:
995
    /// U+0021 '!' ..= U+007E '~'.
996
    ///
997
    /// # Examples
998
    ///
999
    /// ```
    /// let uppercase_a = b'A';
    /// let uppercase_g = b'G';
    /// let a = b'a';
    /// let g = b'g';
    /// let zero = b'0';
    /// let percent = b'%';
    /// let space = b' ';
    /// let lf = b'\n';
    /// let esc = b'\x1b';
    ///
    /// assert!(uppercase_a.is_ascii_graphic());
    /// assert!(uppercase_g.is_ascii_graphic());
    /// assert!(a.is_ascii_graphic());
    /// assert!(g.is_ascii_graphic());
    /// assert!(zero.is_ascii_graphic());
    /// assert!(percent.is_ascii_graphic());
    /// assert!(!space.is_ascii_graphic());
    /// assert!(!lf.is_ascii_graphic());
    /// assert!(!esc.is_ascii_graphic());
    /// ```
    #[must_use]
    #[stable(feature = "ascii_ctype_on_intrinsics", since = "1.24.0")]
    #[rustc_const_stable(feature = "const_ascii_ctype_on_intrinsics", since = "1.47.0")]
    #[inline]
    #[cfg(not(feature = "ferrocene_certified"))]
    pub const fn is_ascii_graphic(&self) -> bool {
        matches!(*self, b'!'..=b'~')
    }
    /// Checks if the value is an ASCII whitespace character:
    /// U+0020 SPACE, U+0009 HORIZONTAL TAB, U+000A LINE FEED,
    /// U+000C FORM FEED, or U+000D CARRIAGE RETURN.
    ///
    /// Rust uses the WhatWG Infra Standard's [definition of ASCII
    /// whitespace][infra-aw]. There are several other definitions in
    /// wide use. For instance, [the POSIX locale][pct] includes
    /// U+000B VERTICAL TAB as well as all the above characters,
    /// but—from the very same specification—[the default rule for
    /// "field splitting" in the Bourne shell][bfs] considers *only*
    /// SPACE, HORIZONTAL TAB, and LINE FEED as whitespace.
    ///
    /// If you are writing a program that will process an existing
    /// file format, check what that format's definition of whitespace is
    /// before using this function.
    ///
    /// [infra-aw]: https://infra.spec.whatwg.org/#ascii-whitespace
    /// [pct]: https://pubs.opengroup.org/onlinepubs/9699919799/basedefs/V1_chap07.html#tag_07_03_01
    /// [bfs]: https://pubs.opengroup.org/onlinepubs/9699919799/utilities/V3_chap02.html#tag_18_06_05
    ///
    /// # Examples
    ///
    /// ```
    /// let uppercase_a = b'A';
    /// let uppercase_g = b'G';
    /// let a = b'a';
    /// let g = b'g';
    /// let zero = b'0';
    /// let percent = b'%';
    /// let space = b' ';
    /// let lf = b'\n';
    /// let esc = b'\x1b';
    ///
    /// assert!(!uppercase_a.is_ascii_whitespace());
    /// assert!(!uppercase_g.is_ascii_whitespace());
    /// assert!(!a.is_ascii_whitespace());
    /// assert!(!g.is_ascii_whitespace());
    /// assert!(!zero.is_ascii_whitespace());
    /// assert!(!percent.is_ascii_whitespace());
    /// assert!(space.is_ascii_whitespace());
    /// assert!(lf.is_ascii_whitespace());
    /// assert!(!esc.is_ascii_whitespace());
    /// ```
    #[must_use]
    #[stable(feature = "ascii_ctype_on_intrinsics", since = "1.24.0")]
    #[rustc_const_stable(feature = "const_ascii_ctype_on_intrinsics", since = "1.47.0")]
    #[inline]
    #[cfg(not(feature = "ferrocene_certified"))]
    pub const fn is_ascii_whitespace(&self) -> bool {
        matches!(*self, b'\t' | b'\n' | b'\x0C' | b'\r' | b' ')
    }
    /// Checks if the value is an ASCII control character:
    /// U+0000 NUL ..= U+001F UNIT SEPARATOR, or U+007F DELETE.
    /// Note that most ASCII whitespace characters are control
    /// characters, but SPACE is not.
    ///
    /// # Examples
    ///
    /// ```
    /// let uppercase_a = b'A';
    /// let uppercase_g = b'G';
    /// let a = b'a';
    /// let g = b'g';
    /// let zero = b'0';
    /// let percent = b'%';
    /// let space = b' ';
    /// let lf = b'\n';
    /// let esc = b'\x1b';
    ///
    /// assert!(!uppercase_a.is_ascii_control());
    /// assert!(!uppercase_g.is_ascii_control());
    /// assert!(!a.is_ascii_control());
    /// assert!(!g.is_ascii_control());
    /// assert!(!zero.is_ascii_control());
    /// assert!(!percent.is_ascii_control());
    /// assert!(!space.is_ascii_control());
    /// assert!(lf.is_ascii_control());
    /// assert!(esc.is_ascii_control());
    /// ```
    #[must_use]
    #[stable(feature = "ascii_ctype_on_intrinsics", since = "1.24.0")]
    #[rustc_const_stable(feature = "const_ascii_ctype_on_intrinsics", since = "1.47.0")]
    #[inline]
    #[cfg(not(feature = "ferrocene_certified"))]
    pub const fn is_ascii_control(&self) -> bool {
        matches!(*self, b'\0'..=b'\x1F' | b'\x7F')
    }
    /// Returns an iterator that produces an escaped version of a `u8`,
    /// treating it as an ASCII character.
    ///
    /// The behavior is identical to [`ascii::escape_default`].
    ///
    /// # Examples
    ///
    /// ```
    /// assert_eq!("0", b'0'.escape_ascii().to_string());
    /// assert_eq!("\\t", b'\t'.escape_ascii().to_string());
    /// assert_eq!("\\r", b'\r'.escape_ascii().to_string());
    /// assert_eq!("\\n", b'\n'.escape_ascii().to_string());
    /// assert_eq!("\\'", b'\''.escape_ascii().to_string());
    /// assert_eq!("\\\"", b'"'.escape_ascii().to_string());
    /// assert_eq!("\\\\", b'\\'.escape_ascii().to_string());
    /// assert_eq!("\\x9d", b'\x9d'.escape_ascii().to_string());
    /// ```
    #[must_use = "this returns the escaped byte as an iterator, \
                  without modifying the original"]
    #[stable(feature = "inherent_ascii_escape", since = "1.60.0")]
    #[inline]
    #[cfg(not(feature = "ferrocene_certified"))]
    pub fn escape_ascii(self) -> ascii::EscapeDefault {
        ascii::escape_default(self)
    }
    #[inline]
    #[cfg(not(feature = "ferrocene_certified"))]
    pub(crate) const fn is_utf8_char_boundary(self) -> bool {
        // This is bit magic equivalent to: b < 128 || b >= 192
        (self as i8) >= -0x40
    }
}
impl u16 {
    uint_impl! {
        Self = u16,
        ActualT = u16,
        SignedT = i16,
        BITS = 16,
        BITS_MINUS_ONE = 15,
        MAX = 65535,
        rot = 4,
        rot_op = "0xa003",
        rot_result = "0x3a",
        fsh_op = "0x2de",
        fshl_result = "0x30",
        fshr_result = "0x302d",
        swap_op = "0x1234",
        swapped = "0x3412",
        reversed = "0x2c48",
        le_bytes = "[0x34, 0x12]",
        be_bytes = "[0x12, 0x34]",
        to_xe_bytes_doc = "",
        from_xe_bytes_doc = "",
        bound_condition = "",
    }
    #[cfg(not(feature = "ferrocene_certified"))]
    midpoint_impl! { u16, u32, unsigned }
    /// Checks if the value is a Unicode surrogate code point, which are disallowed values for [`char`].
    ///
    /// # Examples
    ///
    /// ```
    /// #![feature(utf16_extra)]
    ///
    /// let low_non_surrogate = 0xA000u16;
    /// let low_surrogate = 0xD800u16;
    /// let high_surrogate = 0xDC00u16;
    /// let high_non_surrogate = 0xE000u16;
    ///
    /// assert!(!low_non_surrogate.is_utf16_surrogate());
    /// assert!(low_surrogate.is_utf16_surrogate());
    /// assert!(high_surrogate.is_utf16_surrogate());
    /// assert!(!high_non_surrogate.is_utf16_surrogate());
    /// ```
    #[must_use]
    #[unstable(feature = "utf16_extra", issue = "94919")]
    #[inline]
    #[cfg(not(feature = "ferrocene_certified"))]
    pub const fn is_utf16_surrogate(self) -> bool {
        matches!(self, 0xD800..=0xDFFF)
    }
}
impl u32 {
    uint_impl! {
        Self = u32,
        ActualT = u32,
        SignedT = i32,
        BITS = 32,
        BITS_MINUS_ONE = 31,
        MAX = 4294967295,
        rot = 8,
        rot_op = "0x10000b3",
        rot_result = "0xb301",
        fsh_op = "0x2fe78e45",
        fshl_result = "0xb32f",
        fshr_result = "0xb32fe78e",
        swap_op = "0x12345678",
        swapped = "0x78563412",
        reversed = "0x1e6a2c48",
        le_bytes = "[0x78, 0x56, 0x34, 0x12]",
        be_bytes = "[0x12, 0x34, 0x56, 0x78]",
        to_xe_bytes_doc = "",
        from_xe_bytes_doc = "",
        bound_condition = "",
    }
    #[cfg(not(feature = "ferrocene_certified"))]
    midpoint_impl! { u32, u64, unsigned }
}
impl u64 {
    uint_impl! {
        Self = u64,
        ActualT = u64,
        SignedT = i64,
        BITS = 64,
        BITS_MINUS_ONE = 63,
        MAX = 18446744073709551615,
        rot = 12,
        rot_op = "0xaa00000000006e1",
        rot_result = "0x6e10aa",
        fsh_op = "0x2fe78e45983acd98",
        fshl_result = "0x6e12fe",
        fshr_result = "0x6e12fe78e45983ac",
        swap_op = "0x1234567890123456",
        swapped = "0x5634129078563412",
        reversed = "0x6a2c48091e6a2c48",
        le_bytes = "[0x56, 0x34, 0x12, 0x90, 0x78, 0x56, 0x34, 0x12]",
        be_bytes = "[0x12, 0x34, 0x56, 0x78, 0x90, 0x12, 0x34, 0x56]",
        to_xe_bytes_doc = "",
        from_xe_bytes_doc = "",
        bound_condition = "",
    }
    #[cfg(not(feature = "ferrocene_certified"))]
    midpoint_impl! { u64, u128, unsigned }
}
impl u128 {
    uint_impl! {
        Self = u128,
        ActualT = u128,
        SignedT = i128,
        BITS = 128,
        BITS_MINUS_ONE = 127,
        MAX = 340282366920938463463374607431768211455,
        rot = 16,
        rot_op = "0x13f40000000000000000000000004f76",
        rot_result = "0x4f7613f4",
        fsh_op = "0x2fe78e45983acd98039000008736273",
        fshl_result = "0x4f7602fe",
        fshr_result = "0x4f7602fe78e45983acd9803900000873",
        swap_op = "0x12345678901234567890123456789012",
        swapped = "0x12907856341290785634129078563412",
        reversed = "0x48091e6a2c48091e6a2c48091e6a2c48",
        le_bytes = "[0x12, 0x90, 0x78, 0x56, 0x34, 0x12, 0x90, 0x78, \
            0x56, 0x34, 0x12, 0x90, 0x78, 0x56, 0x34, 0x12]",
        be_bytes = "[0x12, 0x34, 0x56, 0x78, 0x90, 0x12, 0x34, 0x56, \
            0x78, 0x90, 0x12, 0x34, 0x56, 0x78, 0x90, 0x12]",
        to_xe_bytes_doc = "",
        from_xe_bytes_doc = "",
        bound_condition = "",
    }
    #[cfg(not(feature = "ferrocene_certified"))]
    midpoint_impl! { u128, unsigned }
}
#[cfg(target_pointer_width = "16")]
impl usize {
    uint_impl! {
        Self = usize,
        ActualT = u16,
        SignedT = isize,
        BITS = 16,
        BITS_MINUS_ONE = 15,
        MAX = 65535,
        rot = 4,
        rot_op = "0xa003",
        rot_result = "0x3a",
        fsh_op = "0x2fe78e45983acd98039000008736273",
        fshl_result = "0x4f7602fe",
        fshr_result = "0x4f7602fe78e45983acd9803900000873",
        swap_op = "0x1234",
        swapped = "0x3412",
        reversed = "0x2c48",
        le_bytes = "[0x34, 0x12]",
        be_bytes = "[0x12, 0x34]",
        to_xe_bytes_doc = usize_isize_to_xe_bytes_doc!(),
        from_xe_bytes_doc = usize_isize_from_xe_bytes_doc!(),
        bound_condition = " on 16-bit targets",
    }
    #[cfg(not(feature = "ferrocene_certified"))]
    midpoint_impl! { usize, u32, unsigned }
}
#[cfg(target_pointer_width = "32")]
impl usize {
    uint_impl! {
        Self = usize,
        ActualT = u32,
        SignedT = isize,
        BITS = 32,
        BITS_MINUS_ONE = 31,
        MAX = 4294967295,
        rot = 8,
        rot_op = "0x10000b3",
        rot_result = "0xb301",
        fsh_op = "0x2fe78e45",
        fshl_result = "0xb32f",
        fshr_result = "0xb32fe78e",
        swap_op = "0x12345678",
        swapped = "0x78563412",
        reversed = "0x1e6a2c48",
        le_bytes = "[0x78, 0x56, 0x34, 0x12]",
        be_bytes = "[0x12, 0x34, 0x56, 0x78]",
        to_xe_bytes_doc = usize_isize_to_xe_bytes_doc!(),
        from_xe_bytes_doc = usize_isize_from_xe_bytes_doc!(),
        bound_condition = " on 32-bit targets",
    }
    #[cfg(not(feature = "ferrocene_certified"))]
    midpoint_impl! { usize, u64, unsigned }
}
#[cfg(target_pointer_width = "64")]
impl usize {
    uint_impl! {
        Self = usize,
        ActualT = u64,
        SignedT = isize,
        BITS = 64,
        BITS_MINUS_ONE = 63,
        MAX = 18446744073709551615,
        rot = 12,
        rot_op = "0xaa00000000006e1",
        rot_result = "0x6e10aa",
        fsh_op = "0x2fe78e45983acd98",
        fshl_result = "0x6e12fe",
        fshr_result = "0x6e12fe78e45983ac",
        swap_op = "0x1234567890123456",
        swapped = "0x5634129078563412",
        reversed = "0x6a2c48091e6a2c48",
        le_bytes = "[0x56, 0x34, 0x12, 0x90, 0x78, 0x56, 0x34, 0x12]",
        be_bytes = "[0x12, 0x34, 0x56, 0x78, 0x90, 0x12, 0x34, 0x56]",
        to_xe_bytes_doc = usize_isize_to_xe_bytes_doc!(),
        from_xe_bytes_doc = usize_isize_from_xe_bytes_doc!(),
        bound_condition = " on 64-bit targets",
    }
    #[cfg(not(feature = "ferrocene_certified"))]
    midpoint_impl! { usize, u128, unsigned }
}
impl usize {
    /// Returns an `usize` where every byte is equal to `x`.
    #[inline]
    #[cfg(not(feature = "ferrocene_certified"))]
    pub(crate) const fn repeat_u8(x: u8) -> usize {
        usize::from_ne_bytes([x; size_of::<usize>()])
    }
    /// Returns an `usize` where every byte pair is equal to `x`.
    #[inline]
    #[cfg(not(feature = "ferrocene_certified"))]
    pub(crate) const fn repeat_u16(x: u16) -> usize {
        let mut r = 0usize;
        let mut i = 0;
        while i < size_of::<usize>() {
            // Use `wrapping_shl` to make it work on targets with 16-bit `usize`
            r = r.wrapping_shl(16) | (x as usize);
            i += 2;
        }
        r
    }
}
/// A classification of floating point numbers.
///
/// This `enum` is used as the return type for [`f32::classify`] and [`f64::classify`]. See
/// their documentation for more.
///
/// # Examples
///
/// ```
/// use std::num::FpCategory;
///
/// let num = 12.4_f32;
/// let inf = f32::INFINITY;
/// let zero = 0f32;
/// let sub: f32 = 1.1754942e-38;
/// let nan = f32::NAN;
///
/// assert_eq!(num.classify(), FpCategory::Normal);
/// assert_eq!(inf.classify(), FpCategory::Infinite);
/// assert_eq!(zero.classify(), FpCategory::Zero);
/// assert_eq!(sub.classify(), FpCategory::Subnormal);
/// assert_eq!(nan.classify(), FpCategory::Nan);
/// ```
#[derive(Copy, Clone, PartialEq, Eq, Debug)]
#[stable(feature = "rust1", since = "1.0.0")]
#[cfg(not(feature = "ferrocene_certified"))]
pub enum FpCategory {
    /// NaN (not a number): this value results from calculations like `(-1.0).sqrt()`.
    ///
    /// See [the documentation for `f32`](f32) for more information on the unusual properties
    /// of NaN.
    #[stable(feature = "rust1", since = "1.0.0")]
    Nan,
    /// Positive or negative infinity, which often results from dividing a nonzero number
    /// by zero.
    #[stable(feature = "rust1", since = "1.0.0")]
    Infinite,
    /// Positive or negative zero.
    ///
    /// See [the documentation for `f32`](f32) for more information on the signedness of zeroes.
    #[stable(feature = "rust1", since = "1.0.0")]
    Zero,
    /// “Subnormal” or “denormal” floating point representation (less precise, relative to
    /// their magnitude, than [`Normal`]).
    ///
    /// Subnormal numbers are larger in magnitude than [`Zero`] but smaller in magnitude than all
    /// [`Normal`] numbers.
    ///
    /// [`Normal`]: Self::Normal
    /// [`Zero`]: Self::Zero
    #[stable(feature = "rust1", since = "1.0.0")]
    Subnormal,
    /// A regular floating point number, not any of the exceptional categories.
    ///
    /// The smallest positive normal numbers are [`f32::MIN_POSITIVE`] and [`f64::MIN_POSITIVE`],
    /// and the largest positive normal numbers are [`f32::MAX`] and [`f64::MAX`]. (Unlike signed
    /// integers, floating point numbers are symmetric in their range, so negating any of these
    /// constants will produce their negative counterpart.)
    #[stable(feature = "rust1", since = "1.0.0")]
    Normal,
}
/// Determines if a string of text of that length of that radix could be guaranteed to be
/// stored in the given type T.
/// Note that if the radix is known to the compiler, it is just the check of digits.len that
/// is done at runtime.
#[doc(hidden)]
#[inline(always)]
#[unstable(issue = "none", feature = "std_internals")]
#[cfg(not(feature = "ferrocene_certified"))]
pub const fn can_not_overflow<T>(radix: u32, is_signed_ty: bool, digits: &[u8]) -> bool {
    radix <= 16 && digits.len() <= size_of::<T>() * 2 - is_signed_ty as usize
}
#[cfg_attr(not(feature = "panic_immediate_abort"), inline(never))]
#[cfg_attr(feature = "panic_immediate_abort", inline)]
#[cold]
#[track_caller]
#[cfg(not(feature = "ferrocene_certified"))]
const fn from_ascii_radix_panic(radix: u32) -> ! {
    const_panic!(
        "from_ascii_radix: radix must lie in the range `[2, 36]`",
        "from_ascii_radix: radix must lie in the range `[2, 36]` - found {radix}",
        radix: u32 = radix,
    )
}
#[cfg(not(feature = "ferrocene_certified"))]
macro_rules! from_str_int_impl {
    ($signedness:ident $($int_ty:ty)+) => {$(
        #[stable(feature = "rust1", since = "1.0.0")]
        #[rustc_const_unstable(feature = "const_convert", issue = "143773")]
        impl const FromStr for $int_ty {
            type Err = ParseIntError;
            /// Parses an integer from a string slice with decimal digits.
            ///
            /// The characters are expected to be an optional
            #[doc = sign_dependent_expr!{
                $signedness ?
                if signed {
                    " `+` or `-` "
                }
                if unsigned {
                    " `+` "
                }
            }]
            /// sign followed by only digits. Leading and trailing non-digit characters (including
            /// whitespace) represent an error. Underscores (which are accepted in Rust literals)
            /// also represent an error.
            ///
            /// # Examples
            ///
            /// ```
            /// use std::str::FromStr;
            ///
            #[doc = concat!("assert_eq!(", stringify!($int_ty), "::from_str(\"+10\"), Ok(10));")]
            /// ```
            /// Trailing space returns error:
            /// ```
            /// # use std::str::FromStr;
            /// #
            #[doc = concat!("assert!(", stringify!($int_ty), "::from_str(\"1 \").is_err());")]
            /// ```
            #[inline]
40132
            fn from_str(src: &str) -> Result<$int_ty, ParseIntError> {
40132
                <$int_ty>::from_str_radix(src, 10)
40132
            }
        }
        impl $int_ty {
            /// Parses an integer from a string slice with digits in a given base.
            ///
            /// The string is expected to be an optional
            #[doc = sign_dependent_expr!{
                $signedness ?
                if signed {
                    " `+` or `-` "
                }
                if unsigned {
                    " `+` "
                }
            }]
            /// sign followed by only digits. Leading and trailing non-digit characters (including
            /// whitespace) represent an error. Underscores (which are accepted in Rust literals)
            /// also represent an error.
            ///
            /// Digits are a subset of these characters, depending on `radix`:
            /// * `0-9`
            /// * `a-z`
            /// * `A-Z`
            ///
            /// # Panics
            ///
            /// This function panics if `radix` is not in the range from 2 to 36.
            ///
            /// # Examples
            ///
            /// ```
            #[doc = concat!("assert_eq!(", stringify!($int_ty), "::from_str_radix(\"A\", 16), Ok(10));")]
            /// ```
            /// Trailing space returns error:
            /// ```
            #[doc = concat!("assert!(", stringify!($int_ty), "::from_str_radix(\"1 \", 10).is_err());")]
            /// ```
            #[stable(feature = "rust1", since = "1.0.0")]
            #[rustc_const_stable(feature = "const_int_from_str", since = "1.82.0")]
            #[inline]
200265
            pub const fn from_str_radix(src: &str, radix: u32) -> Result<$int_ty, ParseIntError> {
200265
                <$int_ty>::from_ascii_radix(src.as_bytes(), radix)
200265
            }
            /// Parses an integer from an ASCII-byte slice with decimal digits.
            ///
            /// The characters are expected to be an optional
            #[doc = sign_dependent_expr!{
                $signedness ?
                if signed {
                    " `+` or `-` "
                }
                if unsigned {
                    " `+` "
                }
            }]
            /// sign followed by only digits. Leading and trailing non-digit characters (including
            /// whitespace) represent an error. Underscores (which are accepted in Rust literals)
            /// also represent an error.
            ///
            /// # Examples
            ///
            /// ```
            /// #![feature(int_from_ascii)]
            ///
            #[doc = concat!("assert_eq!(", stringify!($int_ty), "::from_ascii(b\"+10\"), Ok(10));")]
            /// ```
            /// Trailing space returns error:
            /// ```
            /// # #![feature(int_from_ascii)]
            /// #
            #[doc = concat!("assert!(", stringify!($int_ty), "::from_ascii(b\"1 \").is_err());")]
            /// ```
            #[unstable(feature = "int_from_ascii", issue = "134821")]
            #[inline]
            pub const fn from_ascii(src: &[u8]) -> Result<$int_ty, ParseIntError> {
                <$int_ty>::from_ascii_radix(src, 10)
            }
            /// Parses an integer from an ASCII-byte slice with digits in a given base.
            ///
            /// The characters are expected to be an optional
            #[doc = sign_dependent_expr!{
                $signedness ?
                if signed {
                    " `+` or `-` "
                }
                if unsigned {
                    " `+` "
                }
            }]
            /// sign followed by only digits. Leading and trailing non-digit characters (including
            /// whitespace) represent an error. Underscores (which are accepted in Rust literals)
            /// also represent an error.
            ///
            /// Digits are a subset of these characters, depending on `radix`:
            /// * `0-9`
            /// * `a-z`
            /// * `A-Z`
            ///
            /// # Panics
            ///
            /// This function panics if `radix` is not in the range from 2 to 36.
            ///
            /// # Examples
            ///
            /// ```
            /// #![feature(int_from_ascii)]
            ///
            #[doc = concat!("assert_eq!(", stringify!($int_ty), "::from_ascii_radix(b\"A\", 16), Ok(10));")]
            /// ```
            /// Trailing space returns error:
            /// ```
            /// # #![feature(int_from_ascii)]
            /// #
            #[doc = concat!("assert!(", stringify!($int_ty), "::from_ascii_radix(b\"1 \", 10).is_err());")]
            /// ```
            #[unstable(feature = "int_from_ascii", issue = "134821")]
            #[inline]
200265
            pub const fn from_ascii_radix(src: &[u8], radix: u32) -> Result<$int_ty, ParseIntError> {
                use self::IntErrorKind::*;
                use self::ParseIntError as PIE;
200265
                if 2 > radix || radix > 36 {
1
                    from_ascii_radix_panic(radix);
200264
                }
200264
                if src.is_empty() {
9
                    return Err(PIE { kind: Empty });
200255
                }
                #[allow(unused_comparisons)]
200255
                let is_signed_ty = 0 > <$int_ty>::MIN;
200253
                let (is_positive, mut digits) = match src {
200255
                    [b'+' | b'-'] => {
2
                        return Err(PIE { kind: InvalidDigit });
                    }
3
                    [b'+', rest @ ..] => (true, rest),
42158
                    [b'-', rest @ ..] if is_signed_ty => (false, rest),
179139
                    _ => (true, src),
                };
200253
                let mut result = 0;
                macro_rules! unwrap_or_PIE {
                    ($option:expr, $kind:ident) => {
                        match $option {
                            Some(value) => value,
                            None => return Err(PIE { kind: $kind }),
                        }
                    };
                }
200253
                if can_not_overflow::<$int_ty>(radix, is_signed_ty, digits) {
                    // If the len of the str is short compared to the range of the type
                    // we are parsing into, then we can be certain that an overflow will not occur.
                    // This bound is when `radix.pow(digits.len()) - 1 <= T::MAX` but the condition
                    // above is a faster (conservative) approximation of this.
                    //
                    // Consider radix 16 as it has the highest information density per digit and will thus overflow the earliest:
                    // `u8::MAX` is `ff` - any str of len 2 is guaranteed to not overflow.
                    // `i8::MAX` is `7f` - only a str of len 1 is guaranteed to not overflow.
                    macro_rules! run_unchecked_loop {
                        ($unchecked_additive_op:tt) => {{
                            while let [c, rest @ ..] = digits {
                                result = result * (radix as $int_ty);
                                let x = unwrap_or_PIE!((*c as char).to_digit(radix), InvalidDigit);
                                result = result $unchecked_additive_op (x as $int_ty);
                                digits = rest;
                            }
                        }};
                    }
107566
                    if is_positive {
263532
                        run_unchecked_loop!(+)
                    } else {
31180
                        run_unchecked_loop!(-)
                    };
                } else {
                    macro_rules! run_checked_loop {
                        ($checked_additive_op:ident, $overflow_err:ident) => {{
                            while let [c, rest @ ..] = digits {
                                // When `radix` is passed in as a literal, rather than doing a slow `imul`
                                // the compiler can use shifts if `radix` can be expressed as a
                                // sum of powers of 2 (x*10 can be written as x*8 + x*2).
                                // When the compiler can't use these optimisations,
                                // the latency of the multiplication can be hidden by issuing it
                                // before the result is needed to improve performance on
                                // modern out-of-order CPU as multiplication here is slower
                                // than the other instructions, we can get the end result faster
                                // doing multiplication first and let the CPU spends other cycles
                                // doing other computation and get multiplication result later.
                                let mul = result.checked_mul(radix as $int_ty);
                                let x = unwrap_or_PIE!((*c as char).to_digit(radix), InvalidDigit) as $int_ty;
                                result = unwrap_or_PIE!(mul, $overflow_err);
                                result = unwrap_or_PIE!(<$int_ty>::$checked_additive_op(result, x), $overflow_err);
                                digits = rest;
                            }
                        }};
                    }
92687
                    if is_positive {
261260
                        run_checked_loop!(checked_add, PosOverflow)
                    } else {
25166
                        run_checked_loop!(checked_sub, NegOverflow)
                    };
                }
85254
                Ok(result)
200264
            }
        }
    )*}
}
#[cfg(not(feature = "ferrocene_certified"))]
from_str_int_impl! { signed isize i8 i16 i32 i64 i128 }
#[cfg(not(feature = "ferrocene_certified"))]
from_str_int_impl! { unsigned usize u8 u16 u32 u64 u128 }