1 //! Numeric traits and functions for the built-in numeric types.
3 #![stable(feature = "rust1", since = "1.0.0")]
7 use crate::str::FromStr;
9 // Used because the `?` operator is not allowed in a const context.
10 macro_rules! try_opt {
19 #[allow_internal_unstable(const_likely)]
20 macro_rules! unlikely {
22 intrinsics::unlikely($e)
26 macro_rules! doc_comment {
27 ($x:expr, $($tt:tt)*) => {
33 // All these modules are technically private and only exposed for coretests:
40 mod int_macros; // import int_impl!
42 mod uint_macros; // import uint_impl!
48 #[stable(feature = "rust1", since = "1.0.0")]
49 pub use wrapping::Wrapping;
51 #[stable(feature = "rust1", since = "1.0.0")]
52 pub use dec2flt::ParseFloatError;
54 #[stable(feature = "rust1", since = "1.0.0")]
55 pub use error::ParseIntError;
57 #[stable(feature = "nonzero", since = "1.28.0")]
58 pub use nonzero::{NonZeroU128, NonZeroU16, NonZeroU32, NonZeroU64, NonZeroU8, NonZeroUsize};
60 #[stable(feature = "signed_nonzero", since = "1.34.0")]
61 pub use nonzero::{NonZeroI128, NonZeroI16, NonZeroI32, NonZeroI64, NonZeroI8, NonZeroIsize};
63 #[stable(feature = "try_from", since = "1.34.0")]
64 pub use error::TryFromIntError;
66 #[stable(feature = "int_error_matching", since = "1.47.0")]
67 pub use error::IntErrorKind;
69 macro_rules! usize_isize_to_xe_bytes_doc {
73 **Note**: This function returns an array of length 2, 4 or 8 bytes
74 depending on the target pointer size.
80 macro_rules! usize_isize_from_xe_bytes_doc {
84 **Note**: This function takes an array of length 2, 4 or 8 bytes
85 depending on the target pointer size.
93 int_impl! { i8, i8, u8, 8, -128, 127, "", "", 2, "-0x7e", "0xa", "0x12", "0x12", "0x48",
94 "[0x12]", "[0x12]", "", "" }
99 int_impl! { i16, i16, u16, 16, -32768, 32767, "", "", 4, "-0x5ffd", "0x3a", "0x1234", "0x3412",
100 "0x2c48", "[0x34, 0x12]", "[0x12, 0x34]", "", "" }
105 int_impl! { i32, i32, u32, 32, -2147483648, 2147483647, "", "", 8, "0x10000b3", "0xb301",
106 "0x12345678", "0x78563412", "0x1e6a2c48", "[0x78, 0x56, 0x34, 0x12]",
107 "[0x12, 0x34, 0x56, 0x78]", "", "" }
112 int_impl! { i64, i64, u64, 64, -9223372036854775808, 9223372036854775807, "", "", 12,
113 "0xaa00000000006e1", "0x6e10aa", "0x1234567890123456", "0x5634129078563412",
114 "0x6a2c48091e6a2c48", "[0x56, 0x34, 0x12, 0x90, 0x78, 0x56, 0x34, 0x12]",
115 "[0x12, 0x34, 0x56, 0x78, 0x90, 0x12, 0x34, 0x56]", "", "" }
120 int_impl! { i128, i128, u128, 128, -170141183460469231731687303715884105728,
121 170141183460469231731687303715884105727, "", "", 16,
122 "0x13f40000000000000000000000004f76", "0x4f7613f4", "0x12345678901234567890123456789012",
123 "0x12907856341290785634129078563412", "0x48091e6a2c48091e6a2c48091e6a2c48",
124 "[0x12, 0x90, 0x78, 0x56, 0x34, 0x12, 0x90, 0x78, \
125 0x56, 0x34, 0x12, 0x90, 0x78, 0x56, 0x34, 0x12]",
126 "[0x12, 0x34, 0x56, 0x78, 0x90, 0x12, 0x34, 0x56, \
127 0x78, 0x90, 0x12, 0x34, 0x56, 0x78, 0x90, 0x12]", "", "" }
130 #[cfg(target_pointer_width = "16")]
133 int_impl! { isize, i16, u16, 16, -32768, 32767, "", "", 4, "-0x5ffd", "0x3a", "0x1234",
134 "0x3412", "0x2c48", "[0x34, 0x12]", "[0x12, 0x34]",
135 usize_isize_to_xe_bytes_doc!(), usize_isize_from_xe_bytes_doc!() }
138 #[cfg(target_pointer_width = "32")]
141 int_impl! { isize, i32, u32, 32, -2147483648, 2147483647, "", "", 8, "0x10000b3", "0xb301",
142 "0x12345678", "0x78563412", "0x1e6a2c48", "[0x78, 0x56, 0x34, 0x12]",
143 "[0x12, 0x34, 0x56, 0x78]",
144 usize_isize_to_xe_bytes_doc!(), usize_isize_from_xe_bytes_doc!() }
147 #[cfg(target_pointer_width = "64")]
150 int_impl! { isize, i64, u64, 64, -9223372036854775808, 9223372036854775807, "", "",
151 12, "0xaa00000000006e1", "0x6e10aa", "0x1234567890123456", "0x5634129078563412",
152 "0x6a2c48091e6a2c48", "[0x56, 0x34, 0x12, 0x90, 0x78, 0x56, 0x34, 0x12]",
153 "[0x12, 0x34, 0x56, 0x78, 0x90, 0x12, 0x34, 0x56]",
154 usize_isize_to_xe_bytes_doc!(), usize_isize_from_xe_bytes_doc!() }
159 uint_impl! { u8, u8, 8, 255, "", "", 2, "0x82", "0xa", "0x12", "0x12", "0x48", "[0x12]",
162 /// Checks if the value is within the ASCII range.
167 /// let ascii = 97u8;
168 /// let non_ascii = 150u8;
170 /// assert!(ascii.is_ascii());
171 /// assert!(!non_ascii.is_ascii());
173 #[stable(feature = "ascii_methods_on_intrinsics", since = "1.23.0")]
174 #[rustc_const_stable(feature = "const_ascii_methods_on_intrinsics", since = "1.43.0")]
176 pub const fn is_ascii(&self) -> bool {
180 /// Makes a copy of the value in its ASCII upper case equivalent.
182 /// ASCII letters 'a' to 'z' are mapped to 'A' to 'Z',
183 /// but non-ASCII letters are unchanged.
185 /// To uppercase the value in-place, use [`make_ascii_uppercase`].
190 /// let lowercase_a = 97u8;
192 /// assert_eq!(65, lowercase_a.to_ascii_uppercase());
195 /// [`make_ascii_uppercase`]: #method.make_ascii_uppercase
196 #[stable(feature = "ascii_methods_on_intrinsics", since = "1.23.0")]
198 pub fn to_ascii_uppercase(&self) -> u8 {
199 // Unset the fifth bit if this is a lowercase letter
200 *self & !((self.is_ascii_lowercase() as u8) << 5)
203 /// Makes a copy of the value in its ASCII lower case equivalent.
205 /// ASCII letters 'A' to 'Z' are mapped to 'a' to 'z',
206 /// but non-ASCII letters are unchanged.
208 /// To lowercase the value in-place, use [`make_ascii_lowercase`].
213 /// let uppercase_a = 65u8;
215 /// assert_eq!(97, uppercase_a.to_ascii_lowercase());
218 /// [`make_ascii_lowercase`]: #method.make_ascii_lowercase
219 #[stable(feature = "ascii_methods_on_intrinsics", since = "1.23.0")]
221 pub fn to_ascii_lowercase(&self) -> u8 {
222 // Set the fifth bit if this is an uppercase letter
223 *self | ((self.is_ascii_uppercase() as u8) << 5)
226 /// Checks that two values are an ASCII case-insensitive match.
228 /// This is equivalent to `to_ascii_lowercase(a) == to_ascii_lowercase(b)`.
233 /// let lowercase_a = 97u8;
234 /// let uppercase_a = 65u8;
236 /// assert!(lowercase_a.eq_ignore_ascii_case(&uppercase_a));
238 #[stable(feature = "ascii_methods_on_intrinsics", since = "1.23.0")]
240 pub fn eq_ignore_ascii_case(&self, other: &u8) -> bool {
241 self.to_ascii_lowercase() == other.to_ascii_lowercase()
244 /// Converts this value to its ASCII upper case equivalent in-place.
246 /// ASCII letters 'a' to 'z' are mapped to 'A' to 'Z',
247 /// but non-ASCII letters are unchanged.
249 /// To return a new uppercased value without modifying the existing one, use
250 /// [`to_ascii_uppercase`].
255 /// let mut byte = b'a';
257 /// byte.make_ascii_uppercase();
259 /// assert_eq!(b'A', byte);
262 /// [`to_ascii_uppercase`]: #method.to_ascii_uppercase
263 #[stable(feature = "ascii_methods_on_intrinsics", since = "1.23.0")]
265 pub fn make_ascii_uppercase(&mut self) {
266 *self = self.to_ascii_uppercase();
269 /// Converts this value to its ASCII lower case equivalent in-place.
271 /// ASCII letters 'A' to 'Z' are mapped to 'a' to 'z',
272 /// but non-ASCII letters are unchanged.
274 /// To return a new lowercased value without modifying the existing one, use
275 /// [`to_ascii_lowercase`].
280 /// let mut byte = b'A';
282 /// byte.make_ascii_lowercase();
284 /// assert_eq!(b'a', byte);
287 /// [`to_ascii_lowercase`]: #method.to_ascii_lowercase
288 #[stable(feature = "ascii_methods_on_intrinsics", since = "1.23.0")]
290 pub fn make_ascii_lowercase(&mut self) {
291 *self = self.to_ascii_lowercase();
294 /// Checks if the value is an ASCII alphabetic character:
296 /// - U+0041 'A' ..= U+005A 'Z', or
297 /// - U+0061 'a' ..= U+007A 'z'.
302 /// let uppercase_a = b'A';
303 /// let uppercase_g = b'G';
307 /// let percent = b'%';
308 /// let space = b' ';
310 /// let esc = 0x1b_u8;
312 /// assert!(uppercase_a.is_ascii_alphabetic());
313 /// assert!(uppercase_g.is_ascii_alphabetic());
314 /// assert!(a.is_ascii_alphabetic());
315 /// assert!(g.is_ascii_alphabetic());
316 /// assert!(!zero.is_ascii_alphabetic());
317 /// assert!(!percent.is_ascii_alphabetic());
318 /// assert!(!space.is_ascii_alphabetic());
319 /// assert!(!lf.is_ascii_alphabetic());
320 /// assert!(!esc.is_ascii_alphabetic());
322 #[stable(feature = "ascii_ctype_on_intrinsics", since = "1.24.0")]
323 #[rustc_const_stable(feature = "const_ascii_ctype_on_intrinsics", since = "1.47.0")]
325 pub const fn is_ascii_alphabetic(&self) -> bool {
326 matches!(*self, b'A'..=b'Z' | b'a'..=b'z')
329 /// Checks if the value is an ASCII uppercase character:
330 /// U+0041 'A' ..= U+005A 'Z'.
335 /// let uppercase_a = b'A';
336 /// let uppercase_g = b'G';
340 /// let percent = b'%';
341 /// let space = b' ';
343 /// let esc = 0x1b_u8;
345 /// assert!(uppercase_a.is_ascii_uppercase());
346 /// assert!(uppercase_g.is_ascii_uppercase());
347 /// assert!(!a.is_ascii_uppercase());
348 /// assert!(!g.is_ascii_uppercase());
349 /// assert!(!zero.is_ascii_uppercase());
350 /// assert!(!percent.is_ascii_uppercase());
351 /// assert!(!space.is_ascii_uppercase());
352 /// assert!(!lf.is_ascii_uppercase());
353 /// assert!(!esc.is_ascii_uppercase());
355 #[stable(feature = "ascii_ctype_on_intrinsics", since = "1.24.0")]
356 #[rustc_const_stable(feature = "const_ascii_ctype_on_intrinsics", since = "1.47.0")]
358 pub const fn is_ascii_uppercase(&self) -> bool {
359 matches!(*self, b'A'..=b'Z')
362 /// Checks if the value is an ASCII lowercase character:
363 /// U+0061 'a' ..= U+007A 'z'.
368 /// let uppercase_a = b'A';
369 /// let uppercase_g = b'G';
373 /// let percent = b'%';
374 /// let space = b' ';
376 /// let esc = 0x1b_u8;
378 /// assert!(!uppercase_a.is_ascii_lowercase());
379 /// assert!(!uppercase_g.is_ascii_lowercase());
380 /// assert!(a.is_ascii_lowercase());
381 /// assert!(g.is_ascii_lowercase());
382 /// assert!(!zero.is_ascii_lowercase());
383 /// assert!(!percent.is_ascii_lowercase());
384 /// assert!(!space.is_ascii_lowercase());
385 /// assert!(!lf.is_ascii_lowercase());
386 /// assert!(!esc.is_ascii_lowercase());
388 #[stable(feature = "ascii_ctype_on_intrinsics", since = "1.24.0")]
389 #[rustc_const_stable(feature = "const_ascii_ctype_on_intrinsics", since = "1.47.0")]
391 pub const fn is_ascii_lowercase(&self) -> bool {
392 matches!(*self, b'a'..=b'z')
395 /// Checks if the value is an ASCII alphanumeric character:
397 /// - U+0041 'A' ..= U+005A 'Z', or
398 /// - U+0061 'a' ..= U+007A 'z', or
399 /// - U+0030 '0' ..= U+0039 '9'.
404 /// let uppercase_a = b'A';
405 /// let uppercase_g = b'G';
409 /// let percent = b'%';
410 /// let space = b' ';
412 /// let esc = 0x1b_u8;
414 /// assert!(uppercase_a.is_ascii_alphanumeric());
415 /// assert!(uppercase_g.is_ascii_alphanumeric());
416 /// assert!(a.is_ascii_alphanumeric());
417 /// assert!(g.is_ascii_alphanumeric());
418 /// assert!(zero.is_ascii_alphanumeric());
419 /// assert!(!percent.is_ascii_alphanumeric());
420 /// assert!(!space.is_ascii_alphanumeric());
421 /// assert!(!lf.is_ascii_alphanumeric());
422 /// assert!(!esc.is_ascii_alphanumeric());
424 #[stable(feature = "ascii_ctype_on_intrinsics", since = "1.24.0")]
425 #[rustc_const_stable(feature = "const_ascii_ctype_on_intrinsics", since = "1.47.0")]
427 pub const fn is_ascii_alphanumeric(&self) -> bool {
428 matches!(*self, b'0'..=b'9' | b'A'..=b'Z' | b'a'..=b'z')
431 /// Checks if the value is an ASCII decimal digit:
432 /// U+0030 '0' ..= U+0039 '9'.
437 /// let uppercase_a = b'A';
438 /// let uppercase_g = b'G';
442 /// let percent = b'%';
443 /// let space = b' ';
445 /// let esc = 0x1b_u8;
447 /// assert!(!uppercase_a.is_ascii_digit());
448 /// assert!(!uppercase_g.is_ascii_digit());
449 /// assert!(!a.is_ascii_digit());
450 /// assert!(!g.is_ascii_digit());
451 /// assert!(zero.is_ascii_digit());
452 /// assert!(!percent.is_ascii_digit());
453 /// assert!(!space.is_ascii_digit());
454 /// assert!(!lf.is_ascii_digit());
455 /// assert!(!esc.is_ascii_digit());
457 #[stable(feature = "ascii_ctype_on_intrinsics", since = "1.24.0")]
458 #[rustc_const_stable(feature = "const_ascii_ctype_on_intrinsics", since = "1.47.0")]
460 pub const fn is_ascii_digit(&self) -> bool {
461 matches!(*self, b'0'..=b'9')
464 /// Checks if the value is an ASCII hexadecimal digit:
466 /// - U+0030 '0' ..= U+0039 '9', or
467 /// - U+0041 'A' ..= U+0046 'F', or
468 /// - U+0061 'a' ..= U+0066 'f'.
473 /// let uppercase_a = b'A';
474 /// let uppercase_g = b'G';
478 /// let percent = b'%';
479 /// let space = b' ';
481 /// let esc = 0x1b_u8;
483 /// assert!(uppercase_a.is_ascii_hexdigit());
484 /// assert!(!uppercase_g.is_ascii_hexdigit());
485 /// assert!(a.is_ascii_hexdigit());
486 /// assert!(!g.is_ascii_hexdigit());
487 /// assert!(zero.is_ascii_hexdigit());
488 /// assert!(!percent.is_ascii_hexdigit());
489 /// assert!(!space.is_ascii_hexdigit());
490 /// assert!(!lf.is_ascii_hexdigit());
491 /// assert!(!esc.is_ascii_hexdigit());
493 #[stable(feature = "ascii_ctype_on_intrinsics", since = "1.24.0")]
494 #[rustc_const_stable(feature = "const_ascii_ctype_on_intrinsics", since = "1.47.0")]
496 pub const fn is_ascii_hexdigit(&self) -> bool {
497 matches!(*self, b'0'..=b'9' | b'A'..=b'F' | b'a'..=b'f')
500 /// Checks if the value is an ASCII punctuation character:
502 /// - U+0021 ..= U+002F `! " # $ % & ' ( ) * + , - . /`, or
503 /// - U+003A ..= U+0040 `: ; < = > ? @`, or
504 /// - U+005B ..= U+0060 ``[ \ ] ^ _ ` ``, or
505 /// - U+007B ..= U+007E `{ | } ~`
510 /// let uppercase_a = b'A';
511 /// let uppercase_g = b'G';
515 /// let percent = b'%';
516 /// let space = b' ';
518 /// let esc = 0x1b_u8;
520 /// assert!(!uppercase_a.is_ascii_punctuation());
521 /// assert!(!uppercase_g.is_ascii_punctuation());
522 /// assert!(!a.is_ascii_punctuation());
523 /// assert!(!g.is_ascii_punctuation());
524 /// assert!(!zero.is_ascii_punctuation());
525 /// assert!(percent.is_ascii_punctuation());
526 /// assert!(!space.is_ascii_punctuation());
527 /// assert!(!lf.is_ascii_punctuation());
528 /// assert!(!esc.is_ascii_punctuation());
530 #[stable(feature = "ascii_ctype_on_intrinsics", since = "1.24.0")]
531 #[rustc_const_stable(feature = "const_ascii_ctype_on_intrinsics", since = "1.47.0")]
533 pub const fn is_ascii_punctuation(&self) -> bool {
534 matches!(*self, b'!'..=b'/' | b':'..=b'@' | b'['..=b'`' | b'{'..=b'~')
537 /// Checks if the value is an ASCII graphic character:
538 /// U+0021 '!' ..= U+007E '~'.
543 /// let uppercase_a = b'A';
544 /// let uppercase_g = b'G';
548 /// let percent = b'%';
549 /// let space = b' ';
551 /// let esc = 0x1b_u8;
553 /// assert!(uppercase_a.is_ascii_graphic());
554 /// assert!(uppercase_g.is_ascii_graphic());
555 /// assert!(a.is_ascii_graphic());
556 /// assert!(g.is_ascii_graphic());
557 /// assert!(zero.is_ascii_graphic());
558 /// assert!(percent.is_ascii_graphic());
559 /// assert!(!space.is_ascii_graphic());
560 /// assert!(!lf.is_ascii_graphic());
561 /// assert!(!esc.is_ascii_graphic());
563 #[stable(feature = "ascii_ctype_on_intrinsics", since = "1.24.0")]
564 #[rustc_const_stable(feature = "const_ascii_ctype_on_intrinsics", since = "1.47.0")]
566 pub const fn is_ascii_graphic(&self) -> bool {
567 matches!(*self, b'!'..=b'~')
570 /// Checks if the value is an ASCII whitespace character:
571 /// U+0020 SPACE, U+0009 HORIZONTAL TAB, U+000A LINE FEED,
572 /// U+000C FORM FEED, or U+000D CARRIAGE RETURN.
574 /// Rust uses the WhatWG Infra Standard's [definition of ASCII
575 /// whitespace][infra-aw]. There are several other definitions in
576 /// wide use. For instance, [the POSIX locale][pct] includes
577 /// U+000B VERTICAL TAB as well as all the above characters,
578 /// but—from the very same specification—[the default rule for
579 /// "field splitting" in the Bourne shell][bfs] considers *only*
580 /// SPACE, HORIZONTAL TAB, and LINE FEED as whitespace.
582 /// If you are writing a program that will process an existing
583 /// file format, check what that format's definition of whitespace is
584 /// before using this function.
586 /// [infra-aw]: https://infra.spec.whatwg.org/#ascii-whitespace
587 /// [pct]: http://pubs.opengroup.org/onlinepubs/9699919799/basedefs/V1_chap07.html#tag_07_03_01
588 /// [bfs]: http://pubs.opengroup.org/onlinepubs/9699919799/utilities/V3_chap02.html#tag_18_06_05
593 /// let uppercase_a = b'A';
594 /// let uppercase_g = b'G';
598 /// let percent = b'%';
599 /// let space = b' ';
601 /// let esc = 0x1b_u8;
603 /// assert!(!uppercase_a.is_ascii_whitespace());
604 /// assert!(!uppercase_g.is_ascii_whitespace());
605 /// assert!(!a.is_ascii_whitespace());
606 /// assert!(!g.is_ascii_whitespace());
607 /// assert!(!zero.is_ascii_whitespace());
608 /// assert!(!percent.is_ascii_whitespace());
609 /// assert!(space.is_ascii_whitespace());
610 /// assert!(lf.is_ascii_whitespace());
611 /// assert!(!esc.is_ascii_whitespace());
613 #[stable(feature = "ascii_ctype_on_intrinsics", since = "1.24.0")]
614 #[rustc_const_stable(feature = "const_ascii_ctype_on_intrinsics", since = "1.47.0")]
616 pub const fn is_ascii_whitespace(&self) -> bool {
617 matches!(*self, b'\t' | b'\n' | b'\x0C' | b'\r' | b' ')
620 /// Checks if the value is an ASCII control character:
621 /// U+0000 NUL ..= U+001F UNIT SEPARATOR, or U+007F DELETE.
622 /// Note that most ASCII whitespace characters are control
623 /// characters, but SPACE is not.
628 /// let uppercase_a = b'A';
629 /// let uppercase_g = b'G';
633 /// let percent = b'%';
634 /// let space = b' ';
636 /// let esc = 0x1b_u8;
638 /// assert!(!uppercase_a.is_ascii_control());
639 /// assert!(!uppercase_g.is_ascii_control());
640 /// assert!(!a.is_ascii_control());
641 /// assert!(!g.is_ascii_control());
642 /// assert!(!zero.is_ascii_control());
643 /// assert!(!percent.is_ascii_control());
644 /// assert!(!space.is_ascii_control());
645 /// assert!(lf.is_ascii_control());
646 /// assert!(esc.is_ascii_control());
648 #[stable(feature = "ascii_ctype_on_intrinsics", since = "1.24.0")]
649 #[rustc_const_stable(feature = "const_ascii_ctype_on_intrinsics", since = "1.47.0")]
651 pub const fn is_ascii_control(&self) -> bool {
652 matches!(*self, b'\0'..=b'\x1F' | b'\x7F')
658 uint_impl! { u16, u16, 16, 65535, "", "", 4, "0xa003", "0x3a", "0x1234", "0x3412", "0x2c48",
659 "[0x34, 0x12]", "[0x12, 0x34]", "", "" }
664 uint_impl! { u32, u32, 32, 4294967295, "", "", 8, "0x10000b3", "0xb301", "0x12345678",
665 "0x78563412", "0x1e6a2c48", "[0x78, 0x56, 0x34, 0x12]", "[0x12, 0x34, 0x56, 0x78]", "", "" }
670 uint_impl! { u64, u64, 64, 18446744073709551615, "", "", 12, "0xaa00000000006e1", "0x6e10aa",
671 "0x1234567890123456", "0x5634129078563412", "0x6a2c48091e6a2c48",
672 "[0x56, 0x34, 0x12, 0x90, 0x78, 0x56, 0x34, 0x12]",
673 "[0x12, 0x34, 0x56, 0x78, 0x90, 0x12, 0x34, 0x56]",
679 uint_impl! { u128, u128, 128, 340282366920938463463374607431768211455, "", "", 16,
680 "0x13f40000000000000000000000004f76", "0x4f7613f4", "0x12345678901234567890123456789012",
681 "0x12907856341290785634129078563412", "0x48091e6a2c48091e6a2c48091e6a2c48",
682 "[0x12, 0x90, 0x78, 0x56, 0x34, 0x12, 0x90, 0x78, \
683 0x56, 0x34, 0x12, 0x90, 0x78, 0x56, 0x34, 0x12]",
684 "[0x12, 0x34, 0x56, 0x78, 0x90, 0x12, 0x34, 0x56, \
685 0x78, 0x90, 0x12, 0x34, 0x56, 0x78, 0x90, 0x12]",
689 #[cfg(target_pointer_width = "16")]
692 uint_impl! { usize, u16, 16, 65535, "", "", 4, "0xa003", "0x3a", "0x1234", "0x3412", "0x2c48",
693 "[0x34, 0x12]", "[0x12, 0x34]",
694 usize_isize_to_xe_bytes_doc!(), usize_isize_from_xe_bytes_doc!() }
696 #[cfg(target_pointer_width = "32")]
699 uint_impl! { usize, u32, 32, 4294967295, "", "", 8, "0x10000b3", "0xb301", "0x12345678",
700 "0x78563412", "0x1e6a2c48", "[0x78, 0x56, 0x34, 0x12]", "[0x12, 0x34, 0x56, 0x78]",
701 usize_isize_to_xe_bytes_doc!(), usize_isize_from_xe_bytes_doc!() }
704 #[cfg(target_pointer_width = "64")]
707 uint_impl! { usize, u64, 64, 18446744073709551615, "", "", 12, "0xaa00000000006e1", "0x6e10aa",
708 "0x1234567890123456", "0x5634129078563412", "0x6a2c48091e6a2c48",
709 "[0x56, 0x34, 0x12, 0x90, 0x78, 0x56, 0x34, 0x12]",
710 "[0x12, 0x34, 0x56, 0x78, 0x90, 0x12, 0x34, 0x56]",
711 usize_isize_to_xe_bytes_doc!(), usize_isize_from_xe_bytes_doc!() }
714 /// A classification of floating point numbers.
716 /// This `enum` is used as the return type for [`f32::classify`] and [`f64::classify`]. See
717 /// their documentation for more.
719 /// [`f32::classify`]: ../../std/primitive.f32.html#method.classify
720 /// [`f64::classify`]: ../../std/primitive.f64.html#method.classify
725 /// use std::num::FpCategory;
727 /// let num = 12.4_f32;
728 /// let inf = f32::INFINITY;
730 /// let sub: f32 = 1.1754942e-38;
731 /// let nan = f32::NAN;
733 /// assert_eq!(num.classify(), FpCategory::Normal);
734 /// assert_eq!(inf.classify(), FpCategory::Infinite);
735 /// assert_eq!(zero.classify(), FpCategory::Zero);
736 /// assert_eq!(nan.classify(), FpCategory::Nan);
737 /// assert_eq!(sub.classify(), FpCategory::Subnormal);
739 #[derive(Copy, Clone, PartialEq, Eq, Debug)]
740 #[stable(feature = "rust1", since = "1.0.0")]
741 pub enum FpCategory {
742 /// "Not a Number", often obtained by dividing by zero.
743 #[stable(feature = "rust1", since = "1.0.0")]
746 /// Positive or negative infinity.
747 #[stable(feature = "rust1", since = "1.0.0")]
750 /// Positive or negative zero.
751 #[stable(feature = "rust1", since = "1.0.0")]
754 /// De-normalized floating point representation (less precise than `Normal`).
755 #[stable(feature = "rust1", since = "1.0.0")]
758 /// A regular floating point number.
759 #[stable(feature = "rust1", since = "1.0.0")]
764 trait FromStrRadixHelper: PartialOrd + Copy {
765 fn min_value() -> Self;
766 fn max_value() -> Self;
767 fn from_u32(u: u32) -> Self;
768 fn checked_mul(&self, other: u32) -> Option<Self>;
769 fn checked_sub(&self, other: u32) -> Option<Self>;
770 fn checked_add(&self, other: u32) -> Option<Self>;
773 macro_rules! from_str_radix_int_impl {
775 #[stable(feature = "rust1", since = "1.0.0")]
776 impl FromStr for $t {
777 type Err = ParseIntError;
778 fn from_str(src: &str) -> Result<Self, ParseIntError> {
779 from_str_radix(src, 10)
784 from_str_radix_int_impl! { isize i8 i16 i32 i64 i128 usize u8 u16 u32 u64 u128 }
787 ($($t:ty)*) => ($(impl FromStrRadixHelper for $t {
789 fn min_value() -> Self { Self::MIN }
791 fn max_value() -> Self { Self::MAX }
793 fn from_u32(u: u32) -> Self { u as Self }
795 fn checked_mul(&self, other: u32) -> Option<Self> {
796 Self::checked_mul(*self, other as Self)
799 fn checked_sub(&self, other: u32) -> Option<Self> {
800 Self::checked_sub(*self, other as Self)
803 fn checked_add(&self, other: u32) -> Option<Self> {
804 Self::checked_add(*self, other as Self)
808 doit! { i8 i16 i32 i64 i128 isize u8 u16 u32 u64 u128 usize }
810 fn from_str_radix<T: FromStrRadixHelper>(src: &str, radix: u32) -> Result<T, ParseIntError> {
811 use self::IntErrorKind::*;
812 use self::ParseIntError as PIE;
815 radix >= 2 && radix <= 36,
816 "from_str_radix_int: must lie in the range `[2, 36]` - found {}",
821 return Err(PIE { kind: Empty });
824 let is_signed_ty = T::from_u32(0) > T::min_value();
826 // all valid digits are ascii, so we will just iterate over the utf8 bytes
827 // and cast them to chars. .to_digit() will safely return None for anything
828 // other than a valid ascii digit for the given radix, including the first-byte
829 // of multi-byte sequences
830 let src = src.as_bytes();
832 let (is_positive, digits) = match src[0] {
833 b'+' | b'-' if src[1..].is_empty() => {
834 return Err(PIE { kind: InvalidDigit(src[0] as char) });
836 b'+' => (true, &src[1..]),
837 b'-' if is_signed_ty => (false, &src[1..]),
841 let mut result = T::from_u32(0);
843 // The number is positive
845 let x = match (c as char).to_digit(radix) {
847 None => return Err(PIE { kind: InvalidDigit(c as char) }),
849 result = match result.checked_mul(radix) {
850 Some(result) => result,
851 None => return Err(PIE { kind: PosOverflow }),
853 result = match result.checked_add(x) {
854 Some(result) => result,
855 None => return Err(PIE { kind: PosOverflow }),
859 // The number is negative
861 let x = match (c as char).to_digit(radix) {
863 None => return Err(PIE { kind: InvalidDigit(c as char) }),
865 result = match result.checked_mul(radix) {
866 Some(result) => result,
867 None => return Err(PIE { kind: NegOverflow }),
869 result = match result.checked_sub(x) {
870 Some(result) => result,
871 None => return Err(PIE { kind: NegOverflow }),