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;
67 feature = "int_error_matching",
68 reason = "it can be useful to match errors when making error messages \
72 pub use error::IntErrorKind;
74 macro_rules! usize_isize_to_xe_bytes_doc {
78 **Note**: This function returns an array of length 2, 4 or 8 bytes
79 depending on the target pointer size.
85 macro_rules! usize_isize_from_xe_bytes_doc {
89 **Note**: This function takes an array of length 2, 4 or 8 bytes
90 depending on the target pointer size.
98 int_impl! { i8, i8, u8, 8, -128, 127, "", "", 2, "-0x7e", "0xa", "0x12", "0x12", "0x48",
99 "[0x12]", "[0x12]", "", "" }
104 int_impl! { i16, i16, u16, 16, -32768, 32767, "", "", 4, "-0x5ffd", "0x3a", "0x1234", "0x3412",
105 "0x2c48", "[0x34, 0x12]", "[0x12, 0x34]", "", "" }
110 int_impl! { i32, i32, u32, 32, -2147483648, 2147483647, "", "", 8, "0x10000b3", "0xb301",
111 "0x12345678", "0x78563412", "0x1e6a2c48", "[0x78, 0x56, 0x34, 0x12]",
112 "[0x12, 0x34, 0x56, 0x78]", "", "" }
117 int_impl! { i64, i64, u64, 64, -9223372036854775808, 9223372036854775807, "", "", 12,
118 "0xaa00000000006e1", "0x6e10aa", "0x1234567890123456", "0x5634129078563412",
119 "0x6a2c48091e6a2c48", "[0x56, 0x34, 0x12, 0x90, 0x78, 0x56, 0x34, 0x12]",
120 "[0x12, 0x34, 0x56, 0x78, 0x90, 0x12, 0x34, 0x56]", "", "" }
125 int_impl! { i128, i128, u128, 128, -170141183460469231731687303715884105728,
126 170141183460469231731687303715884105727, "", "", 16,
127 "0x13f40000000000000000000000004f76", "0x4f7613f4", "0x12345678901234567890123456789012",
128 "0x12907856341290785634129078563412", "0x48091e6a2c48091e6a2c48091e6a2c48",
129 "[0x12, 0x90, 0x78, 0x56, 0x34, 0x12, 0x90, 0x78, \
130 0x56, 0x34, 0x12, 0x90, 0x78, 0x56, 0x34, 0x12]",
131 "[0x12, 0x34, 0x56, 0x78, 0x90, 0x12, 0x34, 0x56, \
132 0x78, 0x90, 0x12, 0x34, 0x56, 0x78, 0x90, 0x12]", "", "" }
135 #[cfg(target_pointer_width = "16")]
138 int_impl! { isize, i16, usize, 16, -32768, 32767, "", "", 4, "-0x5ffd", "0x3a", "0x1234",
139 "0x3412", "0x2c48", "[0x34, 0x12]", "[0x12, 0x34]",
140 usize_isize_to_xe_bytes_doc!(), usize_isize_from_xe_bytes_doc!() }
143 #[cfg(target_pointer_width = "32")]
146 int_impl! { isize, i32, usize, 32, -2147483648, 2147483647, "", "", 8, "0x10000b3", "0xb301",
147 "0x12345678", "0x78563412", "0x1e6a2c48", "[0x78, 0x56, 0x34, 0x12]",
148 "[0x12, 0x34, 0x56, 0x78]",
149 usize_isize_to_xe_bytes_doc!(), usize_isize_from_xe_bytes_doc!() }
152 #[cfg(target_pointer_width = "64")]
155 int_impl! { isize, i64, usize, 64, -9223372036854775808, 9223372036854775807, "", "",
156 12, "0xaa00000000006e1", "0x6e10aa", "0x1234567890123456", "0x5634129078563412",
157 "0x6a2c48091e6a2c48", "[0x56, 0x34, 0x12, 0x90, 0x78, 0x56, 0x34, 0x12]",
158 "[0x12, 0x34, 0x56, 0x78, 0x90, 0x12, 0x34, 0x56]",
159 usize_isize_to_xe_bytes_doc!(), usize_isize_from_xe_bytes_doc!() }
164 uint_impl! { u8, u8, 8, 255, "", "", 2, "0x82", "0xa", "0x12", "0x12", "0x48", "[0x12]",
167 /// Checks if the value is within the ASCII range.
172 /// let ascii = 97u8;
173 /// let non_ascii = 150u8;
175 /// assert!(ascii.is_ascii());
176 /// assert!(!non_ascii.is_ascii());
178 #[stable(feature = "ascii_methods_on_intrinsics", since = "1.23.0")]
179 #[rustc_const_stable(feature = "const_ascii_methods_on_intrinsics", since = "1.43.0")]
181 pub const fn is_ascii(&self) -> bool {
185 /// Makes a copy of the value in its ASCII upper case equivalent.
187 /// ASCII letters 'a' to 'z' are mapped to 'A' to 'Z',
188 /// but non-ASCII letters are unchanged.
190 /// To uppercase the value in-place, use [`make_ascii_uppercase`].
195 /// let lowercase_a = 97u8;
197 /// assert_eq!(65, lowercase_a.to_ascii_uppercase());
200 /// [`make_ascii_uppercase`]: #method.make_ascii_uppercase
201 #[stable(feature = "ascii_methods_on_intrinsics", since = "1.23.0")]
203 pub fn to_ascii_uppercase(&self) -> u8 {
204 // Unset the fifth bit if this is a lowercase letter
205 *self & !((self.is_ascii_lowercase() as u8) << 5)
208 /// Makes a copy of the value in its ASCII lower case equivalent.
210 /// ASCII letters 'A' to 'Z' are mapped to 'a' to 'z',
211 /// but non-ASCII letters are unchanged.
213 /// To lowercase the value in-place, use [`make_ascii_lowercase`].
218 /// let uppercase_a = 65u8;
220 /// assert_eq!(97, uppercase_a.to_ascii_lowercase());
223 /// [`make_ascii_lowercase`]: #method.make_ascii_lowercase
224 #[stable(feature = "ascii_methods_on_intrinsics", since = "1.23.0")]
226 pub fn to_ascii_lowercase(&self) -> u8 {
227 // Set the fifth bit if this is an uppercase letter
228 *self | ((self.is_ascii_uppercase() as u8) << 5)
231 /// Checks that two values are an ASCII case-insensitive match.
233 /// This is equivalent to `to_ascii_lowercase(a) == to_ascii_lowercase(b)`.
238 /// let lowercase_a = 97u8;
239 /// let uppercase_a = 65u8;
241 /// assert!(lowercase_a.eq_ignore_ascii_case(&uppercase_a));
243 #[stable(feature = "ascii_methods_on_intrinsics", since = "1.23.0")]
245 pub fn eq_ignore_ascii_case(&self, other: &u8) -> bool {
246 self.to_ascii_lowercase() == other.to_ascii_lowercase()
249 /// Converts this value to its ASCII upper case equivalent in-place.
251 /// ASCII letters 'a' to 'z' are mapped to 'A' to 'Z',
252 /// but non-ASCII letters are unchanged.
254 /// To return a new uppercased value without modifying the existing one, use
255 /// [`to_ascii_uppercase`].
260 /// let mut byte = b'a';
262 /// byte.make_ascii_uppercase();
264 /// assert_eq!(b'A', byte);
267 /// [`to_ascii_uppercase`]: #method.to_ascii_uppercase
268 #[stable(feature = "ascii_methods_on_intrinsics", since = "1.23.0")]
270 pub fn make_ascii_uppercase(&mut self) {
271 *self = self.to_ascii_uppercase();
274 /// Converts this value to its ASCII lower case equivalent in-place.
276 /// ASCII letters 'A' to 'Z' are mapped to 'a' to 'z',
277 /// but non-ASCII letters are unchanged.
279 /// To return a new lowercased value without modifying the existing one, use
280 /// [`to_ascii_lowercase`].
285 /// let mut byte = b'A';
287 /// byte.make_ascii_lowercase();
289 /// assert_eq!(b'a', byte);
292 /// [`to_ascii_lowercase`]: #method.to_ascii_lowercase
293 #[stable(feature = "ascii_methods_on_intrinsics", since = "1.23.0")]
295 pub fn make_ascii_lowercase(&mut self) {
296 *self = self.to_ascii_lowercase();
299 /// Checks if the value is an ASCII alphabetic character:
301 /// - U+0041 'A' ..= U+005A 'Z', or
302 /// - U+0061 'a' ..= U+007A 'z'.
307 /// let uppercase_a = b'A';
308 /// let uppercase_g = b'G';
312 /// let percent = b'%';
313 /// let space = b' ';
315 /// let esc = 0x1b_u8;
317 /// assert!(uppercase_a.is_ascii_alphabetic());
318 /// assert!(uppercase_g.is_ascii_alphabetic());
319 /// assert!(a.is_ascii_alphabetic());
320 /// assert!(g.is_ascii_alphabetic());
321 /// assert!(!zero.is_ascii_alphabetic());
322 /// assert!(!percent.is_ascii_alphabetic());
323 /// assert!(!space.is_ascii_alphabetic());
324 /// assert!(!lf.is_ascii_alphabetic());
325 /// assert!(!esc.is_ascii_alphabetic());
327 #[stable(feature = "ascii_ctype_on_intrinsics", since = "1.24.0")]
328 #[rustc_const_stable(feature = "const_ascii_ctype_on_intrinsics", since = "1.47.0")]
330 pub const fn is_ascii_alphabetic(&self) -> bool {
331 matches!(*self, b'A'..=b'Z' | b'a'..=b'z')
334 /// Checks if the value is an ASCII uppercase character:
335 /// U+0041 'A' ..= U+005A 'Z'.
340 /// let uppercase_a = b'A';
341 /// let uppercase_g = b'G';
345 /// let percent = b'%';
346 /// let space = b' ';
348 /// let esc = 0x1b_u8;
350 /// assert!(uppercase_a.is_ascii_uppercase());
351 /// assert!(uppercase_g.is_ascii_uppercase());
352 /// assert!(!a.is_ascii_uppercase());
353 /// assert!(!g.is_ascii_uppercase());
354 /// assert!(!zero.is_ascii_uppercase());
355 /// assert!(!percent.is_ascii_uppercase());
356 /// assert!(!space.is_ascii_uppercase());
357 /// assert!(!lf.is_ascii_uppercase());
358 /// assert!(!esc.is_ascii_uppercase());
360 #[stable(feature = "ascii_ctype_on_intrinsics", since = "1.24.0")]
361 #[rustc_const_stable(feature = "const_ascii_ctype_on_intrinsics", since = "1.47.0")]
363 pub const fn is_ascii_uppercase(&self) -> bool {
364 matches!(*self, b'A'..=b'Z')
367 /// Checks if the value is an ASCII lowercase character:
368 /// U+0061 'a' ..= U+007A 'z'.
373 /// let uppercase_a = b'A';
374 /// let uppercase_g = b'G';
378 /// let percent = b'%';
379 /// let space = b' ';
381 /// let esc = 0x1b_u8;
383 /// assert!(!uppercase_a.is_ascii_lowercase());
384 /// assert!(!uppercase_g.is_ascii_lowercase());
385 /// assert!(a.is_ascii_lowercase());
386 /// assert!(g.is_ascii_lowercase());
387 /// assert!(!zero.is_ascii_lowercase());
388 /// assert!(!percent.is_ascii_lowercase());
389 /// assert!(!space.is_ascii_lowercase());
390 /// assert!(!lf.is_ascii_lowercase());
391 /// assert!(!esc.is_ascii_lowercase());
393 #[stable(feature = "ascii_ctype_on_intrinsics", since = "1.24.0")]
394 #[rustc_const_stable(feature = "const_ascii_ctype_on_intrinsics", since = "1.47.0")]
396 pub const fn is_ascii_lowercase(&self) -> bool {
397 matches!(*self, b'a'..=b'z')
400 /// Checks if the value is an ASCII alphanumeric character:
402 /// - U+0041 'A' ..= U+005A 'Z', or
403 /// - U+0061 'a' ..= U+007A 'z', or
404 /// - U+0030 '0' ..= U+0039 '9'.
409 /// let uppercase_a = b'A';
410 /// let uppercase_g = b'G';
414 /// let percent = b'%';
415 /// let space = b' ';
417 /// let esc = 0x1b_u8;
419 /// assert!(uppercase_a.is_ascii_alphanumeric());
420 /// assert!(uppercase_g.is_ascii_alphanumeric());
421 /// assert!(a.is_ascii_alphanumeric());
422 /// assert!(g.is_ascii_alphanumeric());
423 /// assert!(zero.is_ascii_alphanumeric());
424 /// assert!(!percent.is_ascii_alphanumeric());
425 /// assert!(!space.is_ascii_alphanumeric());
426 /// assert!(!lf.is_ascii_alphanumeric());
427 /// assert!(!esc.is_ascii_alphanumeric());
429 #[stable(feature = "ascii_ctype_on_intrinsics", since = "1.24.0")]
430 #[rustc_const_stable(feature = "const_ascii_ctype_on_intrinsics", since = "1.47.0")]
432 pub const fn is_ascii_alphanumeric(&self) -> bool {
433 matches!(*self, b'0'..=b'9' | b'A'..=b'Z' | b'a'..=b'z')
436 /// Checks if the value is an ASCII decimal digit:
437 /// U+0030 '0' ..= U+0039 '9'.
442 /// let uppercase_a = b'A';
443 /// let uppercase_g = b'G';
447 /// let percent = b'%';
448 /// let space = b' ';
450 /// let esc = 0x1b_u8;
452 /// assert!(!uppercase_a.is_ascii_digit());
453 /// assert!(!uppercase_g.is_ascii_digit());
454 /// assert!(!a.is_ascii_digit());
455 /// assert!(!g.is_ascii_digit());
456 /// assert!(zero.is_ascii_digit());
457 /// assert!(!percent.is_ascii_digit());
458 /// assert!(!space.is_ascii_digit());
459 /// assert!(!lf.is_ascii_digit());
460 /// assert!(!esc.is_ascii_digit());
462 #[stable(feature = "ascii_ctype_on_intrinsics", since = "1.24.0")]
463 #[rustc_const_stable(feature = "const_ascii_ctype_on_intrinsics", since = "1.47.0")]
465 pub const fn is_ascii_digit(&self) -> bool {
466 matches!(*self, b'0'..=b'9')
469 /// Checks if the value is an ASCII hexadecimal digit:
471 /// - U+0030 '0' ..= U+0039 '9', or
472 /// - U+0041 'A' ..= U+0046 'F', or
473 /// - U+0061 'a' ..= U+0066 'f'.
478 /// let uppercase_a = b'A';
479 /// let uppercase_g = b'G';
483 /// let percent = b'%';
484 /// let space = b' ';
486 /// let esc = 0x1b_u8;
488 /// assert!(uppercase_a.is_ascii_hexdigit());
489 /// assert!(!uppercase_g.is_ascii_hexdigit());
490 /// assert!(a.is_ascii_hexdigit());
491 /// assert!(!g.is_ascii_hexdigit());
492 /// assert!(zero.is_ascii_hexdigit());
493 /// assert!(!percent.is_ascii_hexdigit());
494 /// assert!(!space.is_ascii_hexdigit());
495 /// assert!(!lf.is_ascii_hexdigit());
496 /// assert!(!esc.is_ascii_hexdigit());
498 #[stable(feature = "ascii_ctype_on_intrinsics", since = "1.24.0")]
499 #[rustc_const_stable(feature = "const_ascii_ctype_on_intrinsics", since = "1.47.0")]
501 pub const fn is_ascii_hexdigit(&self) -> bool {
502 matches!(*self, b'0'..=b'9' | b'A'..=b'F' | b'a'..=b'f')
505 /// Checks if the value is an ASCII punctuation character:
507 /// - U+0021 ..= U+002F `! " # $ % & ' ( ) * + , - . /`, or
508 /// - U+003A ..= U+0040 `: ; < = > ? @`, or
509 /// - U+005B ..= U+0060 ``[ \ ] ^ _ ` ``, or
510 /// - U+007B ..= U+007E `{ | } ~`
515 /// let uppercase_a = b'A';
516 /// let uppercase_g = b'G';
520 /// let percent = b'%';
521 /// let space = b' ';
523 /// let esc = 0x1b_u8;
525 /// assert!(!uppercase_a.is_ascii_punctuation());
526 /// assert!(!uppercase_g.is_ascii_punctuation());
527 /// assert!(!a.is_ascii_punctuation());
528 /// assert!(!g.is_ascii_punctuation());
529 /// assert!(!zero.is_ascii_punctuation());
530 /// assert!(percent.is_ascii_punctuation());
531 /// assert!(!space.is_ascii_punctuation());
532 /// assert!(!lf.is_ascii_punctuation());
533 /// assert!(!esc.is_ascii_punctuation());
535 #[stable(feature = "ascii_ctype_on_intrinsics", since = "1.24.0")]
536 #[rustc_const_stable(feature = "const_ascii_ctype_on_intrinsics", since = "1.47.0")]
538 pub const fn is_ascii_punctuation(&self) -> bool {
539 matches!(*self, b'!'..=b'/' | b':'..=b'@' | b'['..=b'`' | b'{'..=b'~')
542 /// Checks if the value is an ASCII graphic character:
543 /// U+0021 '!' ..= U+007E '~'.
548 /// let uppercase_a = b'A';
549 /// let uppercase_g = b'G';
553 /// let percent = b'%';
554 /// let space = b' ';
556 /// let esc = 0x1b_u8;
558 /// assert!(uppercase_a.is_ascii_graphic());
559 /// assert!(uppercase_g.is_ascii_graphic());
560 /// assert!(a.is_ascii_graphic());
561 /// assert!(g.is_ascii_graphic());
562 /// assert!(zero.is_ascii_graphic());
563 /// assert!(percent.is_ascii_graphic());
564 /// assert!(!space.is_ascii_graphic());
565 /// assert!(!lf.is_ascii_graphic());
566 /// assert!(!esc.is_ascii_graphic());
568 #[stable(feature = "ascii_ctype_on_intrinsics", since = "1.24.0")]
569 #[rustc_const_stable(feature = "const_ascii_ctype_on_intrinsics", since = "1.47.0")]
571 pub const fn is_ascii_graphic(&self) -> bool {
572 matches!(*self, b'!'..=b'~')
575 /// Checks if the value is an ASCII whitespace character:
576 /// U+0020 SPACE, U+0009 HORIZONTAL TAB, U+000A LINE FEED,
577 /// U+000C FORM FEED, or U+000D CARRIAGE RETURN.
579 /// Rust uses the WhatWG Infra Standard's [definition of ASCII
580 /// whitespace][infra-aw]. There are several other definitions in
581 /// wide use. For instance, [the POSIX locale][pct] includes
582 /// U+000B VERTICAL TAB as well as all the above characters,
583 /// but—from the very same specification—[the default rule for
584 /// "field splitting" in the Bourne shell][bfs] considers *only*
585 /// SPACE, HORIZONTAL TAB, and LINE FEED as whitespace.
587 /// If you are writing a program that will process an existing
588 /// file format, check what that format's definition of whitespace is
589 /// before using this function.
591 /// [infra-aw]: https://infra.spec.whatwg.org/#ascii-whitespace
592 /// [pct]: http://pubs.opengroup.org/onlinepubs/9699919799/basedefs/V1_chap07.html#tag_07_03_01
593 /// [bfs]: http://pubs.opengroup.org/onlinepubs/9699919799/utilities/V3_chap02.html#tag_18_06_05
598 /// let uppercase_a = b'A';
599 /// let uppercase_g = b'G';
603 /// let percent = b'%';
604 /// let space = b' ';
606 /// let esc = 0x1b_u8;
608 /// assert!(!uppercase_a.is_ascii_whitespace());
609 /// assert!(!uppercase_g.is_ascii_whitespace());
610 /// assert!(!a.is_ascii_whitespace());
611 /// assert!(!g.is_ascii_whitespace());
612 /// assert!(!zero.is_ascii_whitespace());
613 /// assert!(!percent.is_ascii_whitespace());
614 /// assert!(space.is_ascii_whitespace());
615 /// assert!(lf.is_ascii_whitespace());
616 /// assert!(!esc.is_ascii_whitespace());
618 #[stable(feature = "ascii_ctype_on_intrinsics", since = "1.24.0")]
619 #[rustc_const_stable(feature = "const_ascii_ctype_on_intrinsics", since = "1.47.0")]
621 pub const fn is_ascii_whitespace(&self) -> bool {
622 matches!(*self, b'\t' | b'\n' | b'\x0C' | b'\r' | b' ')
625 /// Checks if the value is an ASCII control character:
626 /// U+0000 NUL ..= U+001F UNIT SEPARATOR, or U+007F DELETE.
627 /// Note that most ASCII whitespace characters are control
628 /// characters, but SPACE is not.
633 /// let uppercase_a = b'A';
634 /// let uppercase_g = b'G';
638 /// let percent = b'%';
639 /// let space = b' ';
641 /// let esc = 0x1b_u8;
643 /// assert!(!uppercase_a.is_ascii_control());
644 /// assert!(!uppercase_g.is_ascii_control());
645 /// assert!(!a.is_ascii_control());
646 /// assert!(!g.is_ascii_control());
647 /// assert!(!zero.is_ascii_control());
648 /// assert!(!percent.is_ascii_control());
649 /// assert!(!space.is_ascii_control());
650 /// assert!(lf.is_ascii_control());
651 /// assert!(esc.is_ascii_control());
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_control(&self) -> bool {
657 matches!(*self, b'\0'..=b'\x1F' | b'\x7F')
663 uint_impl! { u16, u16, 16, 65535, "", "", 4, "0xa003", "0x3a", "0x1234", "0x3412", "0x2c48",
664 "[0x34, 0x12]", "[0x12, 0x34]", "", "" }
669 uint_impl! { u32, u32, 32, 4294967295, "", "", 8, "0x10000b3", "0xb301", "0x12345678",
670 "0x78563412", "0x1e6a2c48", "[0x78, 0x56, 0x34, 0x12]", "[0x12, 0x34, 0x56, 0x78]", "", "" }
675 uint_impl! { u64, u64, 64, 18446744073709551615, "", "", 12, "0xaa00000000006e1", "0x6e10aa",
676 "0x1234567890123456", "0x5634129078563412", "0x6a2c48091e6a2c48",
677 "[0x56, 0x34, 0x12, 0x90, 0x78, 0x56, 0x34, 0x12]",
678 "[0x12, 0x34, 0x56, 0x78, 0x90, 0x12, 0x34, 0x56]",
684 uint_impl! { u128, u128, 128, 340282366920938463463374607431768211455, "", "", 16,
685 "0x13f40000000000000000000000004f76", "0x4f7613f4", "0x12345678901234567890123456789012",
686 "0x12907856341290785634129078563412", "0x48091e6a2c48091e6a2c48091e6a2c48",
687 "[0x12, 0x90, 0x78, 0x56, 0x34, 0x12, 0x90, 0x78, \
688 0x56, 0x34, 0x12, 0x90, 0x78, 0x56, 0x34, 0x12]",
689 "[0x12, 0x34, 0x56, 0x78, 0x90, 0x12, 0x34, 0x56, \
690 0x78, 0x90, 0x12, 0x34, 0x56, 0x78, 0x90, 0x12]",
694 #[cfg(target_pointer_width = "16")]
697 uint_impl! { usize, u16, 16, 65535, "", "", 4, "0xa003", "0x3a", "0x1234", "0x3412", "0x2c48",
698 "[0x34, 0x12]", "[0x12, 0x34]",
699 usize_isize_to_xe_bytes_doc!(), usize_isize_from_xe_bytes_doc!() }
701 #[cfg(target_pointer_width = "32")]
704 uint_impl! { usize, u32, 32, 4294967295, "", "", 8, "0x10000b3", "0xb301", "0x12345678",
705 "0x78563412", "0x1e6a2c48", "[0x78, 0x56, 0x34, 0x12]", "[0x12, 0x34, 0x56, 0x78]",
706 usize_isize_to_xe_bytes_doc!(), usize_isize_from_xe_bytes_doc!() }
709 #[cfg(target_pointer_width = "64")]
712 uint_impl! { usize, u64, 64, 18446744073709551615, "", "", 12, "0xaa00000000006e1", "0x6e10aa",
713 "0x1234567890123456", "0x5634129078563412", "0x6a2c48091e6a2c48",
714 "[0x56, 0x34, 0x12, 0x90, 0x78, 0x56, 0x34, 0x12]",
715 "[0x12, 0x34, 0x56, 0x78, 0x90, 0x12, 0x34, 0x56]",
716 usize_isize_to_xe_bytes_doc!(), usize_isize_from_xe_bytes_doc!() }
719 /// A classification of floating point numbers.
721 /// This `enum` is used as the return type for [`f32::classify`] and [`f64::classify`]. See
722 /// their documentation for more.
724 /// [`f32::classify`]: ../../std/primitive.f32.html#method.classify
725 /// [`f64::classify`]: ../../std/primitive.f64.html#method.classify
730 /// use std::num::FpCategory;
732 /// let num = 12.4_f32;
733 /// let inf = f32::INFINITY;
735 /// let sub: f32 = 1.1754942e-38;
736 /// let nan = f32::NAN;
738 /// assert_eq!(num.classify(), FpCategory::Normal);
739 /// assert_eq!(inf.classify(), FpCategory::Infinite);
740 /// assert_eq!(zero.classify(), FpCategory::Zero);
741 /// assert_eq!(nan.classify(), FpCategory::Nan);
742 /// assert_eq!(sub.classify(), FpCategory::Subnormal);
744 #[derive(Copy, Clone, PartialEq, Eq, Debug)]
745 #[stable(feature = "rust1", since = "1.0.0")]
746 pub enum FpCategory {
747 /// "Not a Number", often obtained by dividing by zero.
748 #[stable(feature = "rust1", since = "1.0.0")]
751 /// Positive or negative infinity.
752 #[stable(feature = "rust1", since = "1.0.0")]
755 /// Positive or negative zero.
756 #[stable(feature = "rust1", since = "1.0.0")]
759 /// De-normalized floating point representation (less precise than `Normal`).
760 #[stable(feature = "rust1", since = "1.0.0")]
763 /// A regular floating point number.
764 #[stable(feature = "rust1", since = "1.0.0")]
769 trait FromStrRadixHelper: PartialOrd + Copy {
770 fn min_value() -> Self;
771 fn max_value() -> Self;
772 fn from_u32(u: u32) -> Self;
773 fn checked_mul(&self, other: u32) -> Option<Self>;
774 fn checked_sub(&self, other: u32) -> Option<Self>;
775 fn checked_add(&self, other: u32) -> Option<Self>;
778 macro_rules! from_str_radix_int_impl {
780 #[stable(feature = "rust1", since = "1.0.0")]
781 impl FromStr for $t {
782 type Err = ParseIntError;
783 fn from_str(src: &str) -> Result<Self, ParseIntError> {
784 from_str_radix(src, 10)
789 from_str_radix_int_impl! { isize i8 i16 i32 i64 i128 usize u8 u16 u32 u64 u128 }
792 ($($t:ty)*) => ($(impl FromStrRadixHelper for $t {
794 fn min_value() -> Self { Self::MIN }
796 fn max_value() -> Self { Self::MAX }
798 fn from_u32(u: u32) -> Self { u as Self }
800 fn checked_mul(&self, other: u32) -> Option<Self> {
801 Self::checked_mul(*self, other as Self)
804 fn checked_sub(&self, other: u32) -> Option<Self> {
805 Self::checked_sub(*self, other as Self)
808 fn checked_add(&self, other: u32) -> Option<Self> {
809 Self::checked_add(*self, other as Self)
813 doit! { i8 i16 i32 i64 i128 isize u8 u16 u32 u64 u128 usize }
815 fn from_str_radix<T: FromStrRadixHelper>(src: &str, radix: u32) -> Result<T, ParseIntError> {
816 use self::IntErrorKind::*;
817 use self::ParseIntError as PIE;
820 radix >= 2 && radix <= 36,
821 "from_str_radix_int: must lie in the range `[2, 36]` - found {}",
826 return Err(PIE { kind: Empty });
829 let is_signed_ty = T::from_u32(0) > T::min_value();
831 // all valid digits are ascii, so we will just iterate over the utf8 bytes
832 // and cast them to chars. .to_digit() will safely return None for anything
833 // other than a valid ascii digit for the given radix, including the first-byte
834 // of multi-byte sequences
835 let src = src.as_bytes();
837 let (is_positive, digits) = match src[0] {
838 b'+' | b'-' if src[1..].is_empty() => {
839 return Err(PIE { kind: InvalidDigit });
841 b'+' => (true, &src[1..]),
842 b'-' if is_signed_ty => (false, &src[1..]),
846 let mut result = T::from_u32(0);
848 // The number is positive
850 let x = match (c as char).to_digit(radix) {
852 None => return Err(PIE { kind: InvalidDigit }),
854 result = match result.checked_mul(radix) {
855 Some(result) => result,
856 None => return Err(PIE { kind: PosOverflow }),
858 result = match result.checked_add(x) {
859 Some(result) => result,
860 None => return Err(PIE { kind: PosOverflow }),
864 // The number is negative
866 let x = match (c as char).to_digit(radix) {
868 None => return Err(PIE { kind: InvalidDigit }),
870 result = match result.checked_mul(radix) {
871 Some(result) => result,
872 None => return Err(PIE { kind: NegOverflow }),
874 result = match result.checked_sub(x) {
875 Some(result) => result,
876 None => return Err(PIE { kind: NegOverflow }),