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