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