1 //! Numeric traits and functions for the built-in numeric types.
3 #![stable(feature = "rust1", since = "1.0.0")]
8 use crate::ops::{Add, Mul, Sub};
9 use crate::str::FromStr;
11 // Used because the `?` operator is not allowed in a const context.
12 macro_rules! try_opt {
21 #[allow_internal_unstable(const_likely)]
22 macro_rules! unlikely {
24 intrinsics::unlikely($e)
28 // All these modules are technically private and only exposed for coretests:
29 #[cfg(not(no_fp_fmt_parse))]
31 #[cfg(not(no_fp_fmt_parse))]
33 #[cfg(not(no_fp_fmt_parse))]
35 #[cfg(not(no_fp_fmt_parse))]
40 mod int_macros; // import int_impl!
42 mod uint_macros; // import uint_impl!
47 #[unstable(feature = "saturating_int_impl", issue = "87920")]
51 #[unstable(feature = "saturating_int_impl", issue = "87920")]
52 pub use saturating::Saturating;
53 #[stable(feature = "rust1", since = "1.0.0")]
54 pub use wrapping::Wrapping;
56 #[stable(feature = "rust1", since = "1.0.0")]
57 #[cfg(not(no_fp_fmt_parse))]
58 pub use dec2flt::ParseFloatError;
60 #[stable(feature = "rust1", since = "1.0.0")]
61 pub use error::ParseIntError;
63 #[stable(feature = "nonzero", since = "1.28.0")]
64 pub use nonzero::{NonZeroU128, NonZeroU16, NonZeroU32, NonZeroU64, NonZeroU8, NonZeroUsize};
66 #[stable(feature = "signed_nonzero", since = "1.34.0")]
67 pub use nonzero::{NonZeroI128, NonZeroI16, NonZeroI32, NonZeroI64, NonZeroI8, NonZeroIsize};
69 #[stable(feature = "try_from", since = "1.34.0")]
70 pub use error::TryFromIntError;
72 #[stable(feature = "int_error_matching", since = "1.55.0")]
73 pub use error::IntErrorKind;
75 macro_rules! usize_isize_to_xe_bytes_doc {
79 **Note**: This function returns an array of length 2, 4 or 8 bytes
80 depending on the target pointer size.
86 macro_rules! usize_isize_from_xe_bytes_doc {
90 **Note**: This function takes an array of length 2, 4 or 8 bytes
91 depending on the target pointer size.
97 macro_rules! widening_impl {
98 ($SelfT:ty, $WideT:ty, $BITS:literal, unsigned) => {
99 /// Calculates the complete product `self * rhs` without the possibility to overflow.
101 /// This returns the low-order (wrapping) bits and the high-order (overflow) bits
102 /// of the result as two separate values, in that order.
108 /// Please note that this example is shared between integer types.
109 /// Which explains why `u32` is used here.
112 /// #![feature(bigint_helper_methods)]
113 /// assert_eq!(5u32.widening_mul(2), (10, 0));
114 /// assert_eq!(1_000_000_000u32.widening_mul(10), (1410065408, 2));
116 #[unstable(feature = "bigint_helper_methods", issue = "85532")]
117 #[rustc_const_unstable(feature = "const_bigint_helper_methods", issue = "85532")]
118 #[must_use = "this returns the result of the operation, \
119 without modifying the original"]
121 pub const fn widening_mul(self, rhs: Self) -> (Self, Self) {
122 // note: longer-term this should be done via an intrinsic,
123 // but for now we can deal without an impl for u128/i128
124 // SAFETY: overflow will be contained within the wider types
125 let wide = unsafe { (self as $WideT).unchecked_mul(rhs as $WideT) };
126 (wide as $SelfT, (wide >> $BITS) as $SelfT)
129 /// Calculates the "full multiplication" `self * rhs + carry`
130 /// without the possibility to overflow.
132 /// This returns the low-order (wrapping) bits and the high-order (overflow) bits
133 /// of the result as two separate values, in that order.
135 /// Performs "long multiplication" which takes in an extra amount to add, and may return an
136 /// additional amount of overflow. This allows for chaining together multiple
137 /// multiplications to create "big integers" which represent larger values.
143 /// Please note that this example is shared between integer types.
144 /// Which explains why `u32` is used here.
147 /// #![feature(bigint_helper_methods)]
148 /// assert_eq!(5u32.carrying_mul(2, 0), (10, 0));
149 /// assert_eq!(5u32.carrying_mul(2, 10), (20, 0));
150 /// assert_eq!(1_000_000_000u32.carrying_mul(10, 0), (1410065408, 2));
151 /// assert_eq!(1_000_000_000u32.carrying_mul(10, 10), (1410065418, 2));
152 #[doc = concat!("assert_eq!(",
153 stringify!($SelfT), "::MAX.carrying_mul(", stringify!($SelfT), "::MAX, ", stringify!($SelfT), "::MAX), ",
154 "(0, ", stringify!($SelfT), "::MAX));"
158 /// If `carry` is zero, this is similar to [`overflowing_mul`](Self::overflowing_mul),
159 /// except that it gives the value of the overflow instead of just whether one happened:
162 /// #![feature(bigint_helper_methods)]
163 /// let r = u8::carrying_mul(7, 13, 0);
164 /// assert_eq!((r.0, r.1 != 0), u8::overflowing_mul(7, 13));
165 /// let r = u8::carrying_mul(13, 42, 0);
166 /// assert_eq!((r.0, r.1 != 0), u8::overflowing_mul(13, 42));
169 /// The value of the first field in the returned tuple matches what you'd get
170 /// by combining the [`wrapping_mul`](Self::wrapping_mul) and
171 /// [`wrapping_add`](Self::wrapping_add) methods:
174 /// #![feature(bigint_helper_methods)]
176 /// 789_u16.carrying_mul(456, 123).0,
177 /// 789_u16.wrapping_mul(456).wrapping_add(123),
180 #[unstable(feature = "bigint_helper_methods", issue = "85532")]
181 #[rustc_const_unstable(feature = "bigint_helper_methods", issue = "85532")]
182 #[must_use = "this returns the result of the operation, \
183 without modifying the original"]
185 pub const fn carrying_mul(self, rhs: Self, carry: Self) -> (Self, Self) {
186 // note: longer-term this should be done via an intrinsic,
187 // but for now we can deal without an impl for u128/i128
188 // SAFETY: overflow will be contained within the wider types
190 (self as $WideT).unchecked_mul(rhs as $WideT).unchecked_add(carry as $WideT)
192 (wide as $SelfT, (wide >> $BITS) as $SelfT)
198 int_impl! { i8, i8, u8, 8, 7, -128, 127, 2, "-0x7e", "0xa", "0x12", "0x12", "0x48",
199 "[0x12]", "[0x12]", "", "", "" }
203 int_impl! { i16, i16, u16, 16, 15, -32768, 32767, 4, "-0x5ffd", "0x3a", "0x1234", "0x3412",
204 "0x2c48", "[0x34, 0x12]", "[0x12, 0x34]", "", "", "" }
208 int_impl! { i32, i32, u32, 32, 31, -2147483648, 2147483647, 8, "0x10000b3", "0xb301",
209 "0x12345678", "0x78563412", "0x1e6a2c48", "[0x78, 0x56, 0x34, 0x12]",
210 "[0x12, 0x34, 0x56, 0x78]", "", "", "" }
214 int_impl! { i64, i64, u64, 64, 63, -9223372036854775808, 9223372036854775807, 12,
215 "0xaa00000000006e1", "0x6e10aa", "0x1234567890123456", "0x5634129078563412",
216 "0x6a2c48091e6a2c48", "[0x56, 0x34, 0x12, 0x90, 0x78, 0x56, 0x34, 0x12]",
217 "[0x12, 0x34, 0x56, 0x78, 0x90, 0x12, 0x34, 0x56]", "", "", "" }
221 int_impl! { i128, i128, u128, 128, 127, -170141183460469231731687303715884105728,
222 170141183460469231731687303715884105727, 16,
223 "0x13f40000000000000000000000004f76", "0x4f7613f4", "0x12345678901234567890123456789012",
224 "0x12907856341290785634129078563412", "0x48091e6a2c48091e6a2c48091e6a2c48",
225 "[0x12, 0x90, 0x78, 0x56, 0x34, 0x12, 0x90, 0x78, \
226 0x56, 0x34, 0x12, 0x90, 0x78, 0x56, 0x34, 0x12]",
227 "[0x12, 0x34, 0x56, 0x78, 0x90, 0x12, 0x34, 0x56, \
228 0x78, 0x90, 0x12, 0x34, 0x56, 0x78, 0x90, 0x12]", "", "", "" }
231 #[cfg(target_pointer_width = "16")]
233 int_impl! { isize, i16, usize, 16, 15, -32768, 32767, 4, "-0x5ffd", "0x3a", "0x1234",
234 "0x3412", "0x2c48", "[0x34, 0x12]", "[0x12, 0x34]",
235 usize_isize_to_xe_bytes_doc!(), usize_isize_from_xe_bytes_doc!(),
236 " on 16-bit targets" }
239 #[cfg(target_pointer_width = "32")]
241 int_impl! { isize, i32, usize, 32, 31, -2147483648, 2147483647, 8, "0x10000b3", "0xb301",
242 "0x12345678", "0x78563412", "0x1e6a2c48", "[0x78, 0x56, 0x34, 0x12]",
243 "[0x12, 0x34, 0x56, 0x78]",
244 usize_isize_to_xe_bytes_doc!(), usize_isize_from_xe_bytes_doc!(),
245 " on 32-bit targets" }
248 #[cfg(target_pointer_width = "64")]
250 int_impl! { isize, i64, usize, 64, 63, -9223372036854775808, 9223372036854775807,
251 12, "0xaa00000000006e1", "0x6e10aa", "0x1234567890123456", "0x5634129078563412",
252 "0x6a2c48091e6a2c48", "[0x56, 0x34, 0x12, 0x90, 0x78, 0x56, 0x34, 0x12]",
253 "[0x12, 0x34, 0x56, 0x78, 0x90, 0x12, 0x34, 0x56]",
254 usize_isize_to_xe_bytes_doc!(), usize_isize_from_xe_bytes_doc!(),
255 " on 64-bit targets" }
258 /// If 6th bit set ascii is upper case.
259 const ASCII_CASE_MASK: u8 = 0b0010_0000;
262 uint_impl! { u8, u8, i8, NonZeroU8, 8, 255, 2, "0x82", "0xa", "0x12", "0x12", "0x48", "[0x12]",
263 "[0x12]", "", "", "" }
264 widening_impl! { u8, u16, 8, unsigned }
266 /// Checks if the value is within the ASCII range.
271 /// let ascii = 97u8;
272 /// let non_ascii = 150u8;
274 /// assert!(ascii.is_ascii());
275 /// assert!(!non_ascii.is_ascii());
278 #[stable(feature = "ascii_methods_on_intrinsics", since = "1.23.0")]
279 #[rustc_const_stable(feature = "const_u8_is_ascii", since = "1.43.0")]
281 pub const fn is_ascii(&self) -> bool {
285 /// Makes a copy of the value in its ASCII upper case equivalent.
287 /// ASCII letters 'a' to 'z' are mapped to 'A' to 'Z',
288 /// but non-ASCII letters are unchanged.
290 /// To uppercase the value in-place, use [`make_ascii_uppercase`].
295 /// let lowercase_a = 97u8;
297 /// assert_eq!(65, lowercase_a.to_ascii_uppercase());
300 /// [`make_ascii_uppercase`]: Self::make_ascii_uppercase
301 #[must_use = "to uppercase the value in-place, use `make_ascii_uppercase()`"]
302 #[stable(feature = "ascii_methods_on_intrinsics", since = "1.23.0")]
303 #[rustc_const_stable(feature = "const_ascii_methods_on_intrinsics", since = "1.52.0")]
305 pub const fn to_ascii_uppercase(&self) -> u8 {
306 // Toggle the fifth bit if this is a lowercase letter
307 *self ^ ((self.is_ascii_lowercase() as u8) * ASCII_CASE_MASK)
310 /// Makes a copy of the value in its ASCII lower case equivalent.
312 /// ASCII letters 'A' to 'Z' are mapped to 'a' to 'z',
313 /// but non-ASCII letters are unchanged.
315 /// To lowercase the value in-place, use [`make_ascii_lowercase`].
320 /// let uppercase_a = 65u8;
322 /// assert_eq!(97, uppercase_a.to_ascii_lowercase());
325 /// [`make_ascii_lowercase`]: Self::make_ascii_lowercase
326 #[must_use = "to lowercase the value in-place, use `make_ascii_lowercase()`"]
327 #[stable(feature = "ascii_methods_on_intrinsics", since = "1.23.0")]
328 #[rustc_const_stable(feature = "const_ascii_methods_on_intrinsics", since = "1.52.0")]
330 pub const fn to_ascii_lowercase(&self) -> u8 {
331 // Set the fifth bit if this is an uppercase letter
332 *self | (self.is_ascii_uppercase() as u8 * ASCII_CASE_MASK)
335 /// Assumes self is ascii
337 pub(crate) const fn ascii_change_case_unchecked(&self) -> u8 {
338 *self ^ ASCII_CASE_MASK
341 /// Checks that two values are an ASCII case-insensitive match.
343 /// This is equivalent to `to_ascii_lowercase(a) == to_ascii_lowercase(b)`.
348 /// let lowercase_a = 97u8;
349 /// let uppercase_a = 65u8;
351 /// assert!(lowercase_a.eq_ignore_ascii_case(&uppercase_a));
353 #[stable(feature = "ascii_methods_on_intrinsics", since = "1.23.0")]
354 #[rustc_const_stable(feature = "const_ascii_methods_on_intrinsics", since = "1.52.0")]
356 pub const fn eq_ignore_ascii_case(&self, other: &u8) -> bool {
357 self.to_ascii_lowercase() == other.to_ascii_lowercase()
360 /// Converts this value to its ASCII upper case equivalent in-place.
362 /// ASCII letters 'a' to 'z' are mapped to 'A' to 'Z',
363 /// but non-ASCII letters are unchanged.
365 /// To return a new uppercased value without modifying the existing one, use
366 /// [`to_ascii_uppercase`].
371 /// let mut byte = b'a';
373 /// byte.make_ascii_uppercase();
375 /// assert_eq!(b'A', byte);
378 /// [`to_ascii_uppercase`]: Self::to_ascii_uppercase
379 #[stable(feature = "ascii_methods_on_intrinsics", since = "1.23.0")]
381 pub fn make_ascii_uppercase(&mut self) {
382 *self = self.to_ascii_uppercase();
385 /// Converts this value to its ASCII lower case equivalent in-place.
387 /// ASCII letters 'A' to 'Z' are mapped to 'a' to 'z',
388 /// but non-ASCII letters are unchanged.
390 /// To return a new lowercased value without modifying the existing one, use
391 /// [`to_ascii_lowercase`].
396 /// let mut byte = b'A';
398 /// byte.make_ascii_lowercase();
400 /// assert_eq!(b'a', byte);
403 /// [`to_ascii_lowercase`]: Self::to_ascii_lowercase
404 #[stable(feature = "ascii_methods_on_intrinsics", since = "1.23.0")]
406 pub fn make_ascii_lowercase(&mut self) {
407 *self = self.to_ascii_lowercase();
410 /// Checks if the value is an ASCII alphabetic character:
412 /// - U+0041 'A' ..= U+005A 'Z', or
413 /// - U+0061 'a' ..= U+007A 'z'.
418 /// let uppercase_a = b'A';
419 /// let uppercase_g = b'G';
423 /// let percent = b'%';
424 /// let space = b' ';
426 /// let esc = b'\x1b';
428 /// assert!(uppercase_a.is_ascii_alphabetic());
429 /// assert!(uppercase_g.is_ascii_alphabetic());
430 /// assert!(a.is_ascii_alphabetic());
431 /// assert!(g.is_ascii_alphabetic());
432 /// assert!(!zero.is_ascii_alphabetic());
433 /// assert!(!percent.is_ascii_alphabetic());
434 /// assert!(!space.is_ascii_alphabetic());
435 /// assert!(!lf.is_ascii_alphabetic());
436 /// assert!(!esc.is_ascii_alphabetic());
439 #[stable(feature = "ascii_ctype_on_intrinsics", since = "1.24.0")]
440 #[rustc_const_stable(feature = "const_ascii_ctype_on_intrinsics", since = "1.47.0")]
442 pub const fn is_ascii_alphabetic(&self) -> bool {
443 matches!(*self, b'A'..=b'Z' | b'a'..=b'z')
446 /// Checks if the value is an ASCII uppercase character:
447 /// U+0041 'A' ..= U+005A 'Z'.
452 /// let uppercase_a = b'A';
453 /// let uppercase_g = b'G';
457 /// let percent = b'%';
458 /// let space = b' ';
460 /// let esc = b'\x1b';
462 /// assert!(uppercase_a.is_ascii_uppercase());
463 /// assert!(uppercase_g.is_ascii_uppercase());
464 /// assert!(!a.is_ascii_uppercase());
465 /// assert!(!g.is_ascii_uppercase());
466 /// assert!(!zero.is_ascii_uppercase());
467 /// assert!(!percent.is_ascii_uppercase());
468 /// assert!(!space.is_ascii_uppercase());
469 /// assert!(!lf.is_ascii_uppercase());
470 /// assert!(!esc.is_ascii_uppercase());
473 #[stable(feature = "ascii_ctype_on_intrinsics", since = "1.24.0")]
474 #[rustc_const_stable(feature = "const_ascii_ctype_on_intrinsics", since = "1.47.0")]
476 pub const fn is_ascii_uppercase(&self) -> bool {
477 matches!(*self, b'A'..=b'Z')
480 /// Checks if the value is an ASCII lowercase character:
481 /// U+0061 'a' ..= U+007A 'z'.
486 /// let uppercase_a = b'A';
487 /// let uppercase_g = b'G';
491 /// let percent = b'%';
492 /// let space = b' ';
494 /// let esc = b'\x1b';
496 /// assert!(!uppercase_a.is_ascii_lowercase());
497 /// assert!(!uppercase_g.is_ascii_lowercase());
498 /// assert!(a.is_ascii_lowercase());
499 /// assert!(g.is_ascii_lowercase());
500 /// assert!(!zero.is_ascii_lowercase());
501 /// assert!(!percent.is_ascii_lowercase());
502 /// assert!(!space.is_ascii_lowercase());
503 /// assert!(!lf.is_ascii_lowercase());
504 /// assert!(!esc.is_ascii_lowercase());
507 #[stable(feature = "ascii_ctype_on_intrinsics", since = "1.24.0")]
508 #[rustc_const_stable(feature = "const_ascii_ctype_on_intrinsics", since = "1.47.0")]
510 pub const fn is_ascii_lowercase(&self) -> bool {
511 matches!(*self, b'a'..=b'z')
514 /// Checks if the value is an ASCII alphanumeric character:
516 /// - U+0041 'A' ..= U+005A 'Z', or
517 /// - U+0061 'a' ..= U+007A 'z', or
518 /// - U+0030 '0' ..= U+0039 '9'.
523 /// let uppercase_a = b'A';
524 /// let uppercase_g = b'G';
528 /// let percent = b'%';
529 /// let space = b' ';
531 /// let esc = b'\x1b';
533 /// assert!(uppercase_a.is_ascii_alphanumeric());
534 /// assert!(uppercase_g.is_ascii_alphanumeric());
535 /// assert!(a.is_ascii_alphanumeric());
536 /// assert!(g.is_ascii_alphanumeric());
537 /// assert!(zero.is_ascii_alphanumeric());
538 /// assert!(!percent.is_ascii_alphanumeric());
539 /// assert!(!space.is_ascii_alphanumeric());
540 /// assert!(!lf.is_ascii_alphanumeric());
541 /// assert!(!esc.is_ascii_alphanumeric());
544 #[stable(feature = "ascii_ctype_on_intrinsics", since = "1.24.0")]
545 #[rustc_const_stable(feature = "const_ascii_ctype_on_intrinsics", since = "1.47.0")]
547 pub const fn is_ascii_alphanumeric(&self) -> bool {
548 matches!(*self, b'0'..=b'9' | b'A'..=b'Z' | b'a'..=b'z')
551 /// Checks if the value is an ASCII decimal digit:
552 /// U+0030 '0' ..= U+0039 '9'.
557 /// let uppercase_a = b'A';
558 /// let uppercase_g = b'G';
562 /// let percent = b'%';
563 /// let space = b' ';
565 /// let esc = b'\x1b';
567 /// assert!(!uppercase_a.is_ascii_digit());
568 /// assert!(!uppercase_g.is_ascii_digit());
569 /// assert!(!a.is_ascii_digit());
570 /// assert!(!g.is_ascii_digit());
571 /// assert!(zero.is_ascii_digit());
572 /// assert!(!percent.is_ascii_digit());
573 /// assert!(!space.is_ascii_digit());
574 /// assert!(!lf.is_ascii_digit());
575 /// assert!(!esc.is_ascii_digit());
578 #[stable(feature = "ascii_ctype_on_intrinsics", since = "1.24.0")]
579 #[rustc_const_stable(feature = "const_ascii_ctype_on_intrinsics", since = "1.47.0")]
581 pub const fn is_ascii_digit(&self) -> bool {
582 matches!(*self, b'0'..=b'9')
585 /// Checks if the value is an ASCII hexadecimal digit:
587 /// - U+0030 '0' ..= U+0039 '9', or
588 /// - U+0041 'A' ..= U+0046 'F', or
589 /// - U+0061 'a' ..= U+0066 'f'.
594 /// let uppercase_a = b'A';
595 /// let uppercase_g = b'G';
599 /// let percent = b'%';
600 /// let space = b' ';
602 /// let esc = b'\x1b';
604 /// assert!(uppercase_a.is_ascii_hexdigit());
605 /// assert!(!uppercase_g.is_ascii_hexdigit());
606 /// assert!(a.is_ascii_hexdigit());
607 /// assert!(!g.is_ascii_hexdigit());
608 /// assert!(zero.is_ascii_hexdigit());
609 /// assert!(!percent.is_ascii_hexdigit());
610 /// assert!(!space.is_ascii_hexdigit());
611 /// assert!(!lf.is_ascii_hexdigit());
612 /// assert!(!esc.is_ascii_hexdigit());
615 #[stable(feature = "ascii_ctype_on_intrinsics", since = "1.24.0")]
616 #[rustc_const_stable(feature = "const_ascii_ctype_on_intrinsics", since = "1.47.0")]
618 pub const fn is_ascii_hexdigit(&self) -> bool {
619 matches!(*self, b'0'..=b'9' | b'A'..=b'F' | b'a'..=b'f')
622 /// Checks if the value is an ASCII punctuation character:
624 /// - U+0021 ..= U+002F `! " # $ % & ' ( ) * + , - . /`, or
625 /// - U+003A ..= U+0040 `: ; < = > ? @`, or
626 /// - U+005B ..= U+0060 ``[ \ ] ^ _ ` ``, or
627 /// - U+007B ..= U+007E `{ | } ~`
632 /// let uppercase_a = b'A';
633 /// let uppercase_g = b'G';
637 /// let percent = b'%';
638 /// let space = b' ';
640 /// let esc = b'\x1b';
642 /// assert!(!uppercase_a.is_ascii_punctuation());
643 /// assert!(!uppercase_g.is_ascii_punctuation());
644 /// assert!(!a.is_ascii_punctuation());
645 /// assert!(!g.is_ascii_punctuation());
646 /// assert!(!zero.is_ascii_punctuation());
647 /// assert!(percent.is_ascii_punctuation());
648 /// assert!(!space.is_ascii_punctuation());
649 /// assert!(!lf.is_ascii_punctuation());
650 /// assert!(!esc.is_ascii_punctuation());
653 #[stable(feature = "ascii_ctype_on_intrinsics", since = "1.24.0")]
654 #[rustc_const_stable(feature = "const_ascii_ctype_on_intrinsics", since = "1.47.0")]
656 pub const fn is_ascii_punctuation(&self) -> bool {
657 matches!(*self, b'!'..=b'/' | b':'..=b'@' | b'['..=b'`' | b'{'..=b'~')
660 /// Checks if the value is an ASCII graphic character:
661 /// U+0021 '!' ..= U+007E '~'.
666 /// let uppercase_a = b'A';
667 /// let uppercase_g = b'G';
671 /// let percent = b'%';
672 /// let space = b' ';
674 /// let esc = b'\x1b';
676 /// assert!(uppercase_a.is_ascii_graphic());
677 /// assert!(uppercase_g.is_ascii_graphic());
678 /// assert!(a.is_ascii_graphic());
679 /// assert!(g.is_ascii_graphic());
680 /// assert!(zero.is_ascii_graphic());
681 /// assert!(percent.is_ascii_graphic());
682 /// assert!(!space.is_ascii_graphic());
683 /// assert!(!lf.is_ascii_graphic());
684 /// assert!(!esc.is_ascii_graphic());
687 #[stable(feature = "ascii_ctype_on_intrinsics", since = "1.24.0")]
688 #[rustc_const_stable(feature = "const_ascii_ctype_on_intrinsics", since = "1.47.0")]
690 pub const fn is_ascii_graphic(&self) -> bool {
691 matches!(*self, b'!'..=b'~')
694 /// Checks if the value is an ASCII whitespace character:
695 /// U+0020 SPACE, U+0009 HORIZONTAL TAB, U+000A LINE FEED,
696 /// U+000C FORM FEED, or U+000D CARRIAGE RETURN.
698 /// Rust uses the WhatWG Infra Standard's [definition of ASCII
699 /// whitespace][infra-aw]. There are several other definitions in
700 /// wide use. For instance, [the POSIX locale][pct] includes
701 /// U+000B VERTICAL TAB as well as all the above characters,
702 /// but—from the very same specification—[the default rule for
703 /// "field splitting" in the Bourne shell][bfs] considers *only*
704 /// SPACE, HORIZONTAL TAB, and LINE FEED as whitespace.
706 /// If you are writing a program that will process an existing
707 /// file format, check what that format's definition of whitespace is
708 /// before using this function.
710 /// [infra-aw]: https://infra.spec.whatwg.org/#ascii-whitespace
711 /// [pct]: https://pubs.opengroup.org/onlinepubs/9699919799/basedefs/V1_chap07.html#tag_07_03_01
712 /// [bfs]: https://pubs.opengroup.org/onlinepubs/9699919799/utilities/V3_chap02.html#tag_18_06_05
717 /// let uppercase_a = b'A';
718 /// let uppercase_g = b'G';
722 /// let percent = b'%';
723 /// let space = b' ';
725 /// let esc = b'\x1b';
727 /// assert!(!uppercase_a.is_ascii_whitespace());
728 /// assert!(!uppercase_g.is_ascii_whitespace());
729 /// assert!(!a.is_ascii_whitespace());
730 /// assert!(!g.is_ascii_whitespace());
731 /// assert!(!zero.is_ascii_whitespace());
732 /// assert!(!percent.is_ascii_whitespace());
733 /// assert!(space.is_ascii_whitespace());
734 /// assert!(lf.is_ascii_whitespace());
735 /// assert!(!esc.is_ascii_whitespace());
738 #[stable(feature = "ascii_ctype_on_intrinsics", since = "1.24.0")]
739 #[rustc_const_stable(feature = "const_ascii_ctype_on_intrinsics", since = "1.47.0")]
741 pub const fn is_ascii_whitespace(&self) -> bool {
742 matches!(*self, b'\t' | b'\n' | b'\x0C' | b'\r' | b' ')
745 /// Checks if the value is an ASCII control character:
746 /// U+0000 NUL ..= U+001F UNIT SEPARATOR, or U+007F DELETE.
747 /// Note that most ASCII whitespace characters are control
748 /// characters, but SPACE is not.
753 /// let uppercase_a = b'A';
754 /// let uppercase_g = b'G';
758 /// let percent = b'%';
759 /// let space = b' ';
761 /// let esc = b'\x1b';
763 /// assert!(!uppercase_a.is_ascii_control());
764 /// assert!(!uppercase_g.is_ascii_control());
765 /// assert!(!a.is_ascii_control());
766 /// assert!(!g.is_ascii_control());
767 /// assert!(!zero.is_ascii_control());
768 /// assert!(!percent.is_ascii_control());
769 /// assert!(!space.is_ascii_control());
770 /// assert!(lf.is_ascii_control());
771 /// assert!(esc.is_ascii_control());
774 #[stable(feature = "ascii_ctype_on_intrinsics", since = "1.24.0")]
775 #[rustc_const_stable(feature = "const_ascii_ctype_on_intrinsics", since = "1.47.0")]
777 pub const fn is_ascii_control(&self) -> bool {
778 matches!(*self, b'\0'..=b'\x1F' | b'\x7F')
781 /// Returns an iterator that produces an escaped version of a `u8`,
782 /// treating it as an ASCII character.
784 /// The behavior is identical to [`ascii::escape_default`].
790 /// assert_eq!("0", b'0'.escape_ascii().to_string());
791 /// assert_eq!("\\t", b'\t'.escape_ascii().to_string());
792 /// assert_eq!("\\r", b'\r'.escape_ascii().to_string());
793 /// assert_eq!("\\n", b'\n'.escape_ascii().to_string());
794 /// assert_eq!("\\'", b'\''.escape_ascii().to_string());
795 /// assert_eq!("\\\"", b'"'.escape_ascii().to_string());
796 /// assert_eq!("\\\\", b'\\'.escape_ascii().to_string());
797 /// assert_eq!("\\x9d", b'\x9d'.escape_ascii().to_string());
799 #[must_use = "this returns the escaped byte as an iterator, \
800 without modifying the original"]
801 #[stable(feature = "inherent_ascii_escape", since = "1.60.0")]
803 pub fn escape_ascii(self) -> ascii::EscapeDefault {
804 ascii::escape_default(self)
808 pub(crate) const fn is_utf8_char_boundary(self) -> bool {
809 // This is bit magic equivalent to: b < 128 || b >= 192
810 (self as i8) >= -0x40
815 uint_impl! { u16, u16, i16, NonZeroU16, 16, 65535, 4, "0xa003", "0x3a", "0x1234", "0x3412", "0x2c48",
816 "[0x34, 0x12]", "[0x12, 0x34]", "", "", "" }
817 widening_impl! { u16, u32, 16, unsigned }
819 /// Checks if the value is a Unicode surrogate code point, which are disallowed values for [`char`].
824 /// #![feature(utf16_extra)]
826 /// let low_non_surrogate = 0xA000u16;
827 /// let low_surrogate = 0xD800u16;
828 /// let high_surrogate = 0xDC00u16;
829 /// let high_non_surrogate = 0xE000u16;
831 /// assert!(!low_non_surrogate.is_utf16_surrogate());
832 /// assert!(low_surrogate.is_utf16_surrogate());
833 /// assert!(high_surrogate.is_utf16_surrogate());
834 /// assert!(!high_non_surrogate.is_utf16_surrogate());
837 #[unstable(feature = "utf16_extra", issue = "94919")]
838 #[rustc_const_unstable(feature = "utf16_extra_const", issue = "94919")]
840 pub const fn is_utf16_surrogate(self) -> bool {
841 matches!(self, 0xD800..=0xDFFF)
846 uint_impl! { u32, u32, i32, NonZeroU32, 32, 4294967295, 8, "0x10000b3", "0xb301", "0x12345678",
847 "0x78563412", "0x1e6a2c48", "[0x78, 0x56, 0x34, 0x12]", "[0x12, 0x34, 0x56, 0x78]", "", "", "" }
848 widening_impl! { u32, u64, 32, unsigned }
852 uint_impl! { u64, u64, i64, NonZeroU64, 64, 18446744073709551615, 12, "0xaa00000000006e1", "0x6e10aa",
853 "0x1234567890123456", "0x5634129078563412", "0x6a2c48091e6a2c48",
854 "[0x56, 0x34, 0x12, 0x90, 0x78, 0x56, 0x34, 0x12]",
855 "[0x12, 0x34, 0x56, 0x78, 0x90, 0x12, 0x34, 0x56]",
857 widening_impl! { u64, u128, 64, unsigned }
861 uint_impl! { u128, u128, i128, NonZeroU128, 128, 340282366920938463463374607431768211455, 16,
862 "0x13f40000000000000000000000004f76", "0x4f7613f4", "0x12345678901234567890123456789012",
863 "0x12907856341290785634129078563412", "0x48091e6a2c48091e6a2c48091e6a2c48",
864 "[0x12, 0x90, 0x78, 0x56, 0x34, 0x12, 0x90, 0x78, \
865 0x56, 0x34, 0x12, 0x90, 0x78, 0x56, 0x34, 0x12]",
866 "[0x12, 0x34, 0x56, 0x78, 0x90, 0x12, 0x34, 0x56, \
867 0x78, 0x90, 0x12, 0x34, 0x56, 0x78, 0x90, 0x12]",
871 #[cfg(target_pointer_width = "16")]
873 uint_impl! { usize, u16, isize, NonZeroUsize, 16, 65535, 4, "0xa003", "0x3a", "0x1234", "0x3412", "0x2c48",
874 "[0x34, 0x12]", "[0x12, 0x34]",
875 usize_isize_to_xe_bytes_doc!(), usize_isize_from_xe_bytes_doc!(),
876 " on 16-bit targets" }
877 widening_impl! { usize, u32, 16, unsigned }
879 #[cfg(target_pointer_width = "32")]
881 uint_impl! { usize, u32, isize, NonZeroUsize, 32, 4294967295, 8, "0x10000b3", "0xb301", "0x12345678",
882 "0x78563412", "0x1e6a2c48", "[0x78, 0x56, 0x34, 0x12]", "[0x12, 0x34, 0x56, 0x78]",
883 usize_isize_to_xe_bytes_doc!(), usize_isize_from_xe_bytes_doc!(),
884 " on 32-bit targets" }
885 widening_impl! { usize, u64, 32, unsigned }
888 #[cfg(target_pointer_width = "64")]
890 uint_impl! { usize, u64, isize, NonZeroUsize, 64, 18446744073709551615, 12, "0xaa00000000006e1", "0x6e10aa",
891 "0x1234567890123456", "0x5634129078563412", "0x6a2c48091e6a2c48",
892 "[0x56, 0x34, 0x12, 0x90, 0x78, 0x56, 0x34, 0x12]",
893 "[0x12, 0x34, 0x56, 0x78, 0x90, 0x12, 0x34, 0x56]",
894 usize_isize_to_xe_bytes_doc!(), usize_isize_from_xe_bytes_doc!(),
895 " on 64-bit targets" }
896 widening_impl! { usize, u128, 64, unsigned }
900 /// Returns an `usize` where every byte is equal to `x`.
902 pub(crate) const fn repeat_u8(x: u8) -> usize {
903 usize::from_ne_bytes([x; mem::size_of::<usize>()])
906 /// Returns an `usize` where every byte pair is equal to `x`.
908 pub(crate) const fn repeat_u16(x: u16) -> usize {
911 while i < mem::size_of::<usize>() {
912 // Use `wrapping_shl` to make it work on targets with 16-bit `usize`
913 r = r.wrapping_shl(16) | (x as usize);
920 /// A classification of floating point numbers.
922 /// This `enum` is used as the return type for [`f32::classify`] and [`f64::classify`]. See
923 /// their documentation for more.
928 /// use std::num::FpCategory;
930 /// let num = 12.4_f32;
931 /// let inf = f32::INFINITY;
933 /// let sub: f32 = 1.1754942e-38;
934 /// let nan = f32::NAN;
936 /// assert_eq!(num.classify(), FpCategory::Normal);
937 /// assert_eq!(inf.classify(), FpCategory::Infinite);
938 /// assert_eq!(zero.classify(), FpCategory::Zero);
939 /// assert_eq!(sub.classify(), FpCategory::Subnormal);
940 /// assert_eq!(nan.classify(), FpCategory::Nan);
942 #[derive(Copy, Clone, PartialEq, Eq, Debug)]
943 #[stable(feature = "rust1", since = "1.0.0")]
944 pub enum FpCategory {
945 /// NaN (not a number): this value results from calculations like `(-1.0).sqrt()`.
947 /// See [the documentation for `f32`](f32) for more information on the unusual properties
949 #[stable(feature = "rust1", since = "1.0.0")]
952 /// Positive or negative infinity, which often results from dividing a nonzero number
954 #[stable(feature = "rust1", since = "1.0.0")]
957 /// Positive or negative zero.
959 /// See [the documentation for `f32`](f32) for more information on the signedness of zeroes.
960 #[stable(feature = "rust1", since = "1.0.0")]
963 /// “Subnormal” or “denormal” floating point representation (less precise, relative to
964 /// their magnitude, than [`Normal`]).
966 /// Subnormal numbers are larger in magnitude than [`Zero`] but smaller in magnitude than all
967 /// [`Normal`] numbers.
969 /// [`Normal`]: Self::Normal
970 /// [`Zero`]: Self::Zero
971 #[stable(feature = "rust1", since = "1.0.0")]
974 /// A regular floating point number, not any of the exceptional categories.
976 /// The smallest positive normal numbers are [`f32::MIN_POSITIVE`] and [`f64::MIN_POSITIVE`],
977 /// and the largest positive normal numbers are [`f32::MAX`] and [`f64::MAX`]. (Unlike signed
978 /// integers, floating point numbers are symmetric in their range, so negating any of these
979 /// constants will produce their negative counterpart.)
980 #[stable(feature = "rust1", since = "1.0.0")]
985 trait FromStrRadixHelper:
986 PartialOrd + Copy + Add<Output = Self> + Sub<Output = Self> + Mul<Output = Self>
989 fn from_u32(u: u32) -> Self;
990 fn checked_mul(&self, other: u32) -> Option<Self>;
991 fn checked_sub(&self, other: u32) -> Option<Self>;
992 fn checked_add(&self, other: u32) -> Option<Self>;
995 macro_rules! from_str_radix_int_impl {
997 #[stable(feature = "rust1", since = "1.0.0")]
998 impl FromStr for $t {
999 type Err = ParseIntError;
1000 fn from_str(src: &str) -> Result<Self, ParseIntError> {
1001 from_str_radix(src, 10)
1006 from_str_radix_int_impl! { isize i8 i16 i32 i64 i128 usize u8 u16 u32 u64 u128 }
1008 macro_rules! impl_helper_for {
1009 ($($t:ty)*) => ($(impl FromStrRadixHelper for $t {
1010 const MIN: Self = Self::MIN;
1012 fn from_u32(u: u32) -> Self { u as Self }
1014 fn checked_mul(&self, other: u32) -> Option<Self> {
1015 Self::checked_mul(*self, other as Self)
1018 fn checked_sub(&self, other: u32) -> Option<Self> {
1019 Self::checked_sub(*self, other as Self)
1022 fn checked_add(&self, other: u32) -> Option<Self> {
1023 Self::checked_add(*self, other as Self)
1027 impl_helper_for! { i8 i16 i32 i64 i128 isize u8 u16 u32 u64 u128 usize }
1029 /// Determines if a string of text of that length of that radix could be guaranteed to be
1030 /// stored in the given type T.
1031 /// Note that if the radix is known to the compiler, it is just the check of digits.len that
1032 /// is done at runtime.
1035 #[unstable(issue = "none", feature = "std_internals")]
1036 pub fn can_not_overflow<T>(radix: u32, is_signed_ty: bool, digits: &[u8]) -> bool {
1037 radix <= 16 && digits.len() <= mem::size_of::<T>() * 2 - is_signed_ty as usize
1040 fn from_str_radix<T: FromStrRadixHelper>(src: &str, radix: u32) -> Result<T, ParseIntError> {
1041 use self::IntErrorKind::*;
1042 use self::ParseIntError as PIE;
1045 (2..=36).contains(&radix),
1046 "from_str_radix_int: must lie in the range `[2, 36]` - found {}",
1051 return Err(PIE { kind: Empty });
1054 let is_signed_ty = T::from_u32(0) > T::MIN;
1056 // all valid digits are ascii, so we will just iterate over the utf8 bytes
1057 // and cast them to chars. .to_digit() will safely return None for anything
1058 // other than a valid ascii digit for the given radix, including the first-byte
1059 // of multi-byte sequences
1060 let src = src.as_bytes();
1062 let (is_positive, digits) = match src[0] {
1063 b'+' | b'-' if src[1..].is_empty() => {
1064 return Err(PIE { kind: InvalidDigit });
1066 b'+' => (true, &src[1..]),
1067 b'-' if is_signed_ty => (false, &src[1..]),
1071 let mut result = T::from_u32(0);
1073 if can_not_overflow::<T>(radix, is_signed_ty, digits) {
1074 // If the len of the str is short compared to the range of the type
1075 // we are parsing into, then we can be certain that an overflow will not occur.
1076 // This bound is when `radix.pow(digits.len()) - 1 <= T::MAX` but the condition
1077 // above is a faster (conservative) approximation of this.
1079 // Consider radix 16 as it has the highest information density per digit and will thus overflow the earliest:
1080 // `u8::MAX` is `ff` - any str of len 2 is guaranteed to not overflow.
1081 // `i8::MAX` is `7f` - only a str of len 1 is guaranteed to not overflow.
1082 macro_rules! run_unchecked_loop {
1083 ($unchecked_additive_op:expr) => {
1085 result = result * T::from_u32(radix);
1086 let x = (c as char).to_digit(radix).ok_or(PIE { kind: InvalidDigit })?;
1087 result = $unchecked_additive_op(result, T::from_u32(x));
1092 run_unchecked_loop!(<T as core::ops::Add>::add)
1094 run_unchecked_loop!(<T as core::ops::Sub>::sub)
1097 macro_rules! run_checked_loop {
1098 ($checked_additive_op:ident, $overflow_err:expr) => {
1100 // When `radix` is passed in as a literal, rather than doing a slow `imul`
1101 // the compiler can use shifts if `radix` can be expressed as a
1102 // sum of powers of 2 (x*10 can be written as x*8 + x*2).
1103 // When the compiler can't use these optimisations,
1104 // the latency of the multiplication can be hidden by issuing it
1105 // before the result is needed to improve performance on
1106 // modern out-of-order CPU as multiplication here is slower
1107 // than the other instructions, we can get the end result faster
1108 // doing multiplication first and let the CPU spends other cycles
1109 // doing other computation and get multiplication result later.
1110 let mul = result.checked_mul(radix);
1111 let x = (c as char).to_digit(radix).ok_or(PIE { kind: InvalidDigit })?;
1112 result = mul.ok_or_else($overflow_err)?;
1113 result = T::$checked_additive_op(&result, x).ok_or_else($overflow_err)?;
1118 run_checked_loop!(checked_add, || PIE { kind: PosOverflow })
1120 run_checked_loop!(checked_sub, || PIE { kind: NegOverflow })