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 // All these modules are technically private and only exposed for coretests:
33 mod int_macros; // import int_impl!
35 mod uint_macros; // import uint_impl!
41 #[stable(feature = "rust1", since = "1.0.0")]
42 pub use wrapping::Wrapping;
44 #[stable(feature = "rust1", since = "1.0.0")]
45 pub use dec2flt::ParseFloatError;
47 #[stable(feature = "rust1", since = "1.0.0")]
48 pub use error::ParseIntError;
50 #[stable(feature = "nonzero", since = "1.28.0")]
51 pub use nonzero::{NonZeroU128, NonZeroU16, NonZeroU32, NonZeroU64, NonZeroU8, NonZeroUsize};
53 #[stable(feature = "signed_nonzero", since = "1.34.0")]
54 pub use nonzero::{NonZeroI128, NonZeroI16, NonZeroI32, NonZeroI64, NonZeroI8, NonZeroIsize};
56 #[stable(feature = "try_from", since = "1.34.0")]
57 pub use error::TryFromIntError;
60 feature = "int_error_matching",
61 reason = "it can be useful to match errors when making error messages \
65 pub use error::IntErrorKind;
67 macro_rules! usize_isize_to_xe_bytes_doc {
71 **Note**: This function returns an array of length 2, 4 or 8 bytes
72 depending on the target pointer size.
78 macro_rules! usize_isize_from_xe_bytes_doc {
82 **Note**: This function takes an array of length 2, 4 or 8 bytes
83 depending on the target pointer size.
91 int_impl! { i8, i8, u8, 8, -128, 127, 2, "-0x7e", "0xa", "0x12", "0x12", "0x48",
92 "[0x12]", "[0x12]", "", "" }
97 int_impl! { i16, i16, u16, 16, -32768, 32767, 4, "-0x5ffd", "0x3a", "0x1234", "0x3412",
98 "0x2c48", "[0x34, 0x12]", "[0x12, 0x34]", "", "" }
103 int_impl! { i32, i32, u32, 32, -2147483648, 2147483647, 8, "0x10000b3", "0xb301",
104 "0x12345678", "0x78563412", "0x1e6a2c48", "[0x78, 0x56, 0x34, 0x12]",
105 "[0x12, 0x34, 0x56, 0x78]", "", "" }
110 int_impl! { i64, i64, u64, 64, -9223372036854775808, 9223372036854775807, 12,
111 "0xaa00000000006e1", "0x6e10aa", "0x1234567890123456", "0x5634129078563412",
112 "0x6a2c48091e6a2c48", "[0x56, 0x34, 0x12, 0x90, 0x78, 0x56, 0x34, 0x12]",
113 "[0x12, 0x34, 0x56, 0x78, 0x90, 0x12, 0x34, 0x56]", "", "" }
118 int_impl! { i128, i128, u128, 128, -170141183460469231731687303715884105728,
119 170141183460469231731687303715884105727, 16,
120 "0x13f40000000000000000000000004f76", "0x4f7613f4", "0x12345678901234567890123456789012",
121 "0x12907856341290785634129078563412", "0x48091e6a2c48091e6a2c48091e6a2c48",
122 "[0x12, 0x90, 0x78, 0x56, 0x34, 0x12, 0x90, 0x78, \
123 0x56, 0x34, 0x12, 0x90, 0x78, 0x56, 0x34, 0x12]",
124 "[0x12, 0x34, 0x56, 0x78, 0x90, 0x12, 0x34, 0x56, \
125 0x78, 0x90, 0x12, 0x34, 0x56, 0x78, 0x90, 0x12]", "", "" }
128 #[cfg(target_pointer_width = "16")]
131 int_impl! { isize, i16, usize, 16, -32768, 32767, 4, "-0x5ffd", "0x3a", "0x1234",
132 "0x3412", "0x2c48", "[0x34, 0x12]", "[0x12, 0x34]",
133 usize_isize_to_xe_bytes_doc!(), usize_isize_from_xe_bytes_doc!() }
136 #[cfg(target_pointer_width = "32")]
139 int_impl! { isize, i32, usize, 32, -2147483648, 2147483647, 8, "0x10000b3", "0xb301",
140 "0x12345678", "0x78563412", "0x1e6a2c48", "[0x78, 0x56, 0x34, 0x12]",
141 "[0x12, 0x34, 0x56, 0x78]",
142 usize_isize_to_xe_bytes_doc!(), usize_isize_from_xe_bytes_doc!() }
145 #[cfg(target_pointer_width = "64")]
148 int_impl! { isize, i64, usize, 64, -9223372036854775808, 9223372036854775807,
149 12, "0xaa00000000006e1", "0x6e10aa", "0x1234567890123456", "0x5634129078563412",
150 "0x6a2c48091e6a2c48", "[0x56, 0x34, 0x12, 0x90, 0x78, 0x56, 0x34, 0x12]",
151 "[0x12, 0x34, 0x56, 0x78, 0x90, 0x12, 0x34, 0x56]",
152 usize_isize_to_xe_bytes_doc!(), usize_isize_from_xe_bytes_doc!() }
155 /// If 6th bit set ascii is upper case.
156 const ASCII_CASE_MASK: u8 = 0b0010_0000;
160 uint_impl! { u8, u8, 8, 255, 2, "0x82", "0xa", "0x12", "0x12", "0x48", "[0x12]",
163 /// Checks if the value is within the ASCII range.
168 /// let ascii = 97u8;
169 /// let non_ascii = 150u8;
171 /// assert!(ascii.is_ascii());
172 /// assert!(!non_ascii.is_ascii());
174 #[stable(feature = "ascii_methods_on_intrinsics", since = "1.23.0")]
175 #[rustc_const_stable(feature = "const_ascii_methods_on_intrinsics", since = "1.43.0")]
177 pub const fn is_ascii(&self) -> bool {
181 /// Makes a copy of the value in its ASCII upper case equivalent.
183 /// ASCII letters 'a' to 'z' are mapped to 'A' to 'Z',
184 /// but non-ASCII letters are unchanged.
186 /// To uppercase the value in-place, use [`make_ascii_uppercase`].
191 /// let lowercase_a = 97u8;
193 /// assert_eq!(65, lowercase_a.to_ascii_uppercase());
196 /// [`make_ascii_uppercase`]: #method.make_ascii_uppercase
197 #[stable(feature = "ascii_methods_on_intrinsics", since = "1.23.0")]
198 #[rustc_const_stable(feature = "const_ascii_methods_on_intrinsics", since = "1.50.0")]
200 pub const fn to_ascii_uppercase(&self) -> u8 {
201 // Unset the fifth bit if this is a lowercase letter
202 *self & !((self.is_ascii_lowercase() as u8) * ASCII_CASE_MASK)
205 /// Makes a copy of the value in its ASCII lower case equivalent.
207 /// ASCII letters 'A' to 'Z' are mapped to 'a' to 'z',
208 /// but non-ASCII letters are unchanged.
210 /// To lowercase the value in-place, use [`make_ascii_lowercase`].
215 /// let uppercase_a = 65u8;
217 /// assert_eq!(97, uppercase_a.to_ascii_lowercase());
220 /// [`make_ascii_lowercase`]: #method.make_ascii_lowercase
221 #[stable(feature = "ascii_methods_on_intrinsics", since = "1.23.0")]
222 #[rustc_const_stable(feature = "const_ascii_methods_on_intrinsics", since = "1.50.0")]
224 pub const fn to_ascii_lowercase(&self) -> u8 {
225 // Set the fifth bit if this is an uppercase letter
226 *self | (self.is_ascii_uppercase() as u8 * ASCII_CASE_MASK)
229 /// Assumes self is ascii
231 pub(crate) fn ascii_change_case_unchecked(&self) -> u8 {
232 *self ^ ASCII_CASE_MASK
235 /// Checks that two values are an ASCII case-insensitive match.
237 /// This is equivalent to `to_ascii_lowercase(a) == to_ascii_lowercase(b)`.
242 /// let lowercase_a = 97u8;
243 /// let uppercase_a = 65u8;
245 /// assert!(lowercase_a.eq_ignore_ascii_case(&uppercase_a));
247 #[stable(feature = "ascii_methods_on_intrinsics", since = "1.23.0")]
248 #[rustc_const_stable(feature = "const_ascii_methods_on_intrinsics", since = "1.50.0")]
250 pub const fn eq_ignore_ascii_case(&self, other: &u8) -> bool {
251 self.to_ascii_lowercase() == other.to_ascii_lowercase()
254 /// Converts this value to its ASCII upper case equivalent in-place.
256 /// ASCII letters 'a' to 'z' are mapped to 'A' to 'Z',
257 /// but non-ASCII letters are unchanged.
259 /// To return a new uppercased value without modifying the existing one, use
260 /// [`to_ascii_uppercase`].
265 /// let mut byte = b'a';
267 /// byte.make_ascii_uppercase();
269 /// assert_eq!(b'A', byte);
272 /// [`to_ascii_uppercase`]: #method.to_ascii_uppercase
273 #[stable(feature = "ascii_methods_on_intrinsics", since = "1.23.0")]
275 pub fn make_ascii_uppercase(&mut self) {
276 *self = self.to_ascii_uppercase();
279 /// Converts this value to its ASCII lower case equivalent in-place.
281 /// ASCII letters 'A' to 'Z' are mapped to 'a' to 'z',
282 /// but non-ASCII letters are unchanged.
284 /// To return a new lowercased value without modifying the existing one, use
285 /// [`to_ascii_lowercase`].
290 /// let mut byte = b'A';
292 /// byte.make_ascii_lowercase();
294 /// assert_eq!(b'a', byte);
297 /// [`to_ascii_lowercase`]: #method.to_ascii_lowercase
298 #[stable(feature = "ascii_methods_on_intrinsics", since = "1.23.0")]
300 pub fn make_ascii_lowercase(&mut self) {
301 *self = self.to_ascii_lowercase();
304 /// Checks if the value is an ASCII alphabetic character:
306 /// - U+0041 'A' ..= U+005A 'Z', or
307 /// - U+0061 'a' ..= U+007A 'z'.
312 /// let uppercase_a = b'A';
313 /// let uppercase_g = b'G';
317 /// let percent = b'%';
318 /// let space = b' ';
320 /// let esc = 0x1b_u8;
322 /// assert!(uppercase_a.is_ascii_alphabetic());
323 /// assert!(uppercase_g.is_ascii_alphabetic());
324 /// assert!(a.is_ascii_alphabetic());
325 /// assert!(g.is_ascii_alphabetic());
326 /// assert!(!zero.is_ascii_alphabetic());
327 /// assert!(!percent.is_ascii_alphabetic());
328 /// assert!(!space.is_ascii_alphabetic());
329 /// assert!(!lf.is_ascii_alphabetic());
330 /// assert!(!esc.is_ascii_alphabetic());
332 #[stable(feature = "ascii_ctype_on_intrinsics", since = "1.24.0")]
333 #[rustc_const_stable(feature = "const_ascii_ctype_on_intrinsics", since = "1.47.0")]
335 pub const fn is_ascii_alphabetic(&self) -> bool {
336 matches!(*self, b'A'..=b'Z' | b'a'..=b'z')
339 /// Checks if the value is an ASCII uppercase character:
340 /// U+0041 'A' ..= U+005A 'Z'.
345 /// let uppercase_a = b'A';
346 /// let uppercase_g = b'G';
350 /// let percent = b'%';
351 /// let space = b' ';
353 /// let esc = 0x1b_u8;
355 /// assert!(uppercase_a.is_ascii_uppercase());
356 /// assert!(uppercase_g.is_ascii_uppercase());
357 /// assert!(!a.is_ascii_uppercase());
358 /// assert!(!g.is_ascii_uppercase());
359 /// assert!(!zero.is_ascii_uppercase());
360 /// assert!(!percent.is_ascii_uppercase());
361 /// assert!(!space.is_ascii_uppercase());
362 /// assert!(!lf.is_ascii_uppercase());
363 /// assert!(!esc.is_ascii_uppercase());
365 #[stable(feature = "ascii_ctype_on_intrinsics", since = "1.24.0")]
366 #[rustc_const_stable(feature = "const_ascii_ctype_on_intrinsics", since = "1.47.0")]
368 pub const fn is_ascii_uppercase(&self) -> bool {
369 matches!(*self, b'A'..=b'Z')
372 /// Checks if the value is an ASCII lowercase character:
373 /// U+0061 'a' ..= U+007A 'z'.
378 /// let uppercase_a = b'A';
379 /// let uppercase_g = b'G';
383 /// let percent = b'%';
384 /// let space = b' ';
386 /// let esc = 0x1b_u8;
388 /// assert!(!uppercase_a.is_ascii_lowercase());
389 /// assert!(!uppercase_g.is_ascii_lowercase());
390 /// assert!(a.is_ascii_lowercase());
391 /// assert!(g.is_ascii_lowercase());
392 /// assert!(!zero.is_ascii_lowercase());
393 /// assert!(!percent.is_ascii_lowercase());
394 /// assert!(!space.is_ascii_lowercase());
395 /// assert!(!lf.is_ascii_lowercase());
396 /// assert!(!esc.is_ascii_lowercase());
398 #[stable(feature = "ascii_ctype_on_intrinsics", since = "1.24.0")]
399 #[rustc_const_stable(feature = "const_ascii_ctype_on_intrinsics", since = "1.47.0")]
401 pub const fn is_ascii_lowercase(&self) -> bool {
402 matches!(*self, b'a'..=b'z')
405 /// Checks if the value is an ASCII alphanumeric character:
407 /// - U+0041 'A' ..= U+005A 'Z', or
408 /// - U+0061 'a' ..= U+007A 'z', or
409 /// - U+0030 '0' ..= U+0039 '9'.
414 /// let uppercase_a = b'A';
415 /// let uppercase_g = b'G';
419 /// let percent = b'%';
420 /// let space = b' ';
422 /// let esc = 0x1b_u8;
424 /// assert!(uppercase_a.is_ascii_alphanumeric());
425 /// assert!(uppercase_g.is_ascii_alphanumeric());
426 /// assert!(a.is_ascii_alphanumeric());
427 /// assert!(g.is_ascii_alphanumeric());
428 /// assert!(zero.is_ascii_alphanumeric());
429 /// assert!(!percent.is_ascii_alphanumeric());
430 /// assert!(!space.is_ascii_alphanumeric());
431 /// assert!(!lf.is_ascii_alphanumeric());
432 /// assert!(!esc.is_ascii_alphanumeric());
434 #[stable(feature = "ascii_ctype_on_intrinsics", since = "1.24.0")]
435 #[rustc_const_stable(feature = "const_ascii_ctype_on_intrinsics", since = "1.47.0")]
437 pub const fn is_ascii_alphanumeric(&self) -> bool {
438 matches!(*self, b'0'..=b'9' | b'A'..=b'Z' | b'a'..=b'z')
441 /// Checks if the value is an ASCII decimal digit:
442 /// U+0030 '0' ..= U+0039 '9'.
447 /// let uppercase_a = b'A';
448 /// let uppercase_g = b'G';
452 /// let percent = b'%';
453 /// let space = b' ';
455 /// let esc = 0x1b_u8;
457 /// assert!(!uppercase_a.is_ascii_digit());
458 /// assert!(!uppercase_g.is_ascii_digit());
459 /// assert!(!a.is_ascii_digit());
460 /// assert!(!g.is_ascii_digit());
461 /// assert!(zero.is_ascii_digit());
462 /// assert!(!percent.is_ascii_digit());
463 /// assert!(!space.is_ascii_digit());
464 /// assert!(!lf.is_ascii_digit());
465 /// assert!(!esc.is_ascii_digit());
467 #[stable(feature = "ascii_ctype_on_intrinsics", since = "1.24.0")]
468 #[rustc_const_stable(feature = "const_ascii_ctype_on_intrinsics", since = "1.47.0")]
470 pub const fn is_ascii_digit(&self) -> bool {
471 matches!(*self, b'0'..=b'9')
474 /// Checks if the value is an ASCII hexadecimal digit:
476 /// - U+0030 '0' ..= U+0039 '9', or
477 /// - U+0041 'A' ..= U+0046 'F', or
478 /// - U+0061 'a' ..= U+0066 'f'.
483 /// let uppercase_a = b'A';
484 /// let uppercase_g = b'G';
488 /// let percent = b'%';
489 /// let space = b' ';
491 /// let esc = 0x1b_u8;
493 /// assert!(uppercase_a.is_ascii_hexdigit());
494 /// assert!(!uppercase_g.is_ascii_hexdigit());
495 /// assert!(a.is_ascii_hexdigit());
496 /// assert!(!g.is_ascii_hexdigit());
497 /// assert!(zero.is_ascii_hexdigit());
498 /// assert!(!percent.is_ascii_hexdigit());
499 /// assert!(!space.is_ascii_hexdigit());
500 /// assert!(!lf.is_ascii_hexdigit());
501 /// assert!(!esc.is_ascii_hexdigit());
503 #[stable(feature = "ascii_ctype_on_intrinsics", since = "1.24.0")]
504 #[rustc_const_stable(feature = "const_ascii_ctype_on_intrinsics", since = "1.47.0")]
506 pub const fn is_ascii_hexdigit(&self) -> bool {
507 matches!(*self, b'0'..=b'9' | b'A'..=b'F' | b'a'..=b'f')
510 /// Checks if the value is an ASCII punctuation character:
512 /// - U+0021 ..= U+002F `! " # $ % & ' ( ) * + , - . /`, or
513 /// - U+003A ..= U+0040 `: ; < = > ? @`, or
514 /// - U+005B ..= U+0060 ``[ \ ] ^ _ ` ``, or
515 /// - U+007B ..= U+007E `{ | } ~`
520 /// let uppercase_a = b'A';
521 /// let uppercase_g = b'G';
525 /// let percent = b'%';
526 /// let space = b' ';
528 /// let esc = 0x1b_u8;
530 /// assert!(!uppercase_a.is_ascii_punctuation());
531 /// assert!(!uppercase_g.is_ascii_punctuation());
532 /// assert!(!a.is_ascii_punctuation());
533 /// assert!(!g.is_ascii_punctuation());
534 /// assert!(!zero.is_ascii_punctuation());
535 /// assert!(percent.is_ascii_punctuation());
536 /// assert!(!space.is_ascii_punctuation());
537 /// assert!(!lf.is_ascii_punctuation());
538 /// assert!(!esc.is_ascii_punctuation());
540 #[stable(feature = "ascii_ctype_on_intrinsics", since = "1.24.0")]
541 #[rustc_const_stable(feature = "const_ascii_ctype_on_intrinsics", since = "1.47.0")]
543 pub const fn is_ascii_punctuation(&self) -> bool {
544 matches!(*self, b'!'..=b'/' | b':'..=b'@' | b'['..=b'`' | b'{'..=b'~')
547 /// Checks if the value is an ASCII graphic character:
548 /// U+0021 '!' ..= U+007E '~'.
553 /// let uppercase_a = b'A';
554 /// let uppercase_g = b'G';
558 /// let percent = b'%';
559 /// let space = b' ';
561 /// let esc = 0x1b_u8;
563 /// assert!(uppercase_a.is_ascii_graphic());
564 /// assert!(uppercase_g.is_ascii_graphic());
565 /// assert!(a.is_ascii_graphic());
566 /// assert!(g.is_ascii_graphic());
567 /// assert!(zero.is_ascii_graphic());
568 /// assert!(percent.is_ascii_graphic());
569 /// assert!(!space.is_ascii_graphic());
570 /// assert!(!lf.is_ascii_graphic());
571 /// assert!(!esc.is_ascii_graphic());
573 #[stable(feature = "ascii_ctype_on_intrinsics", since = "1.24.0")]
574 #[rustc_const_stable(feature = "const_ascii_ctype_on_intrinsics", since = "1.47.0")]
576 pub const fn is_ascii_graphic(&self) -> bool {
577 matches!(*self, b'!'..=b'~')
580 /// Checks if the value is an ASCII whitespace character:
581 /// U+0020 SPACE, U+0009 HORIZONTAL TAB, U+000A LINE FEED,
582 /// U+000C FORM FEED, or U+000D CARRIAGE RETURN.
584 /// Rust uses the WhatWG Infra Standard's [definition of ASCII
585 /// whitespace][infra-aw]. There are several other definitions in
586 /// wide use. For instance, [the POSIX locale][pct] includes
587 /// U+000B VERTICAL TAB as well as all the above characters,
588 /// but—from the very same specification—[the default rule for
589 /// "field splitting" in the Bourne shell][bfs] considers *only*
590 /// SPACE, HORIZONTAL TAB, and LINE FEED as whitespace.
592 /// If you are writing a program that will process an existing
593 /// file format, check what that format's definition of whitespace is
594 /// before using this function.
596 /// [infra-aw]: https://infra.spec.whatwg.org/#ascii-whitespace
597 /// [pct]: http://pubs.opengroup.org/onlinepubs/9699919799/basedefs/V1_chap07.html#tag_07_03_01
598 /// [bfs]: http://pubs.opengroup.org/onlinepubs/9699919799/utilities/V3_chap02.html#tag_18_06_05
603 /// let uppercase_a = b'A';
604 /// let uppercase_g = b'G';
608 /// let percent = b'%';
609 /// let space = b' ';
611 /// let esc = 0x1b_u8;
613 /// assert!(!uppercase_a.is_ascii_whitespace());
614 /// assert!(!uppercase_g.is_ascii_whitespace());
615 /// assert!(!a.is_ascii_whitespace());
616 /// assert!(!g.is_ascii_whitespace());
617 /// assert!(!zero.is_ascii_whitespace());
618 /// assert!(!percent.is_ascii_whitespace());
619 /// assert!(space.is_ascii_whitespace());
620 /// assert!(lf.is_ascii_whitespace());
621 /// assert!(!esc.is_ascii_whitespace());
623 #[stable(feature = "ascii_ctype_on_intrinsics", since = "1.24.0")]
624 #[rustc_const_stable(feature = "const_ascii_ctype_on_intrinsics", since = "1.47.0")]
626 pub const fn is_ascii_whitespace(&self) -> bool {
627 matches!(*self, b'\t' | b'\n' | b'\x0C' | b'\r' | b' ')
630 /// Checks if the value is an ASCII control character:
631 /// U+0000 NUL ..= U+001F UNIT SEPARATOR, or U+007F DELETE.
632 /// Note that most ASCII whitespace characters are control
633 /// characters, but SPACE is not.
638 /// let uppercase_a = b'A';
639 /// let uppercase_g = b'G';
643 /// let percent = b'%';
644 /// let space = b' ';
646 /// let esc = 0x1b_u8;
648 /// assert!(!uppercase_a.is_ascii_control());
649 /// assert!(!uppercase_g.is_ascii_control());
650 /// assert!(!a.is_ascii_control());
651 /// assert!(!g.is_ascii_control());
652 /// assert!(!zero.is_ascii_control());
653 /// assert!(!percent.is_ascii_control());
654 /// assert!(!space.is_ascii_control());
655 /// assert!(lf.is_ascii_control());
656 /// assert!(esc.is_ascii_control());
658 #[stable(feature = "ascii_ctype_on_intrinsics", since = "1.24.0")]
659 #[rustc_const_stable(feature = "const_ascii_ctype_on_intrinsics", since = "1.47.0")]
661 pub const fn is_ascii_control(&self) -> bool {
662 matches!(*self, b'\0'..=b'\x1F' | b'\x7F')
668 uint_impl! { u16, u16, 16, 65535, 4, "0xa003", "0x3a", "0x1234", "0x3412", "0x2c48",
669 "[0x34, 0x12]", "[0x12, 0x34]", "", "" }
674 uint_impl! { u32, u32, 32, 4294967295, 8, "0x10000b3", "0xb301", "0x12345678",
675 "0x78563412", "0x1e6a2c48", "[0x78, 0x56, 0x34, 0x12]", "[0x12, 0x34, 0x56, 0x78]", "", "" }
680 uint_impl! { u64, u64, 64, 18446744073709551615, 12, "0xaa00000000006e1", "0x6e10aa",
681 "0x1234567890123456", "0x5634129078563412", "0x6a2c48091e6a2c48",
682 "[0x56, 0x34, 0x12, 0x90, 0x78, 0x56, 0x34, 0x12]",
683 "[0x12, 0x34, 0x56, 0x78, 0x90, 0x12, 0x34, 0x56]",
689 uint_impl! { u128, u128, 128, 340282366920938463463374607431768211455, 16,
690 "0x13f40000000000000000000000004f76", "0x4f7613f4", "0x12345678901234567890123456789012",
691 "0x12907856341290785634129078563412", "0x48091e6a2c48091e6a2c48091e6a2c48",
692 "[0x12, 0x90, 0x78, 0x56, 0x34, 0x12, 0x90, 0x78, \
693 0x56, 0x34, 0x12, 0x90, 0x78, 0x56, 0x34, 0x12]",
694 "[0x12, 0x34, 0x56, 0x78, 0x90, 0x12, 0x34, 0x56, \
695 0x78, 0x90, 0x12, 0x34, 0x56, 0x78, 0x90, 0x12]",
699 #[cfg(target_pointer_width = "16")]
702 uint_impl! { usize, u16, 16, 65535, 4, "0xa003", "0x3a", "0x1234", "0x3412", "0x2c48",
703 "[0x34, 0x12]", "[0x12, 0x34]",
704 usize_isize_to_xe_bytes_doc!(), usize_isize_from_xe_bytes_doc!() }
706 #[cfg(target_pointer_width = "32")]
709 uint_impl! { usize, u32, 32, 4294967295, 8, "0x10000b3", "0xb301", "0x12345678",
710 "0x78563412", "0x1e6a2c48", "[0x78, 0x56, 0x34, 0x12]", "[0x12, 0x34, 0x56, 0x78]",
711 usize_isize_to_xe_bytes_doc!(), usize_isize_from_xe_bytes_doc!() }
714 #[cfg(target_pointer_width = "64")]
717 uint_impl! { usize, u64, 64, 18446744073709551615, 12, "0xaa00000000006e1", "0x6e10aa",
718 "0x1234567890123456", "0x5634129078563412", "0x6a2c48091e6a2c48",
719 "[0x56, 0x34, 0x12, 0x90, 0x78, 0x56, 0x34, 0x12]",
720 "[0x12, 0x34, 0x56, 0x78, 0x90, 0x12, 0x34, 0x56]",
721 usize_isize_to_xe_bytes_doc!(), usize_isize_from_xe_bytes_doc!() }
724 /// A classification of floating point numbers.
726 /// This `enum` is used as the return type for [`f32::classify`] and [`f64::classify`]. See
727 /// their documentation for more.
729 /// [`f32::classify`]: ../../std/primitive.f32.html#method.classify
730 /// [`f64::classify`]: ../../std/primitive.f64.html#method.classify
735 /// use std::num::FpCategory;
737 /// let num = 12.4_f32;
738 /// let inf = f32::INFINITY;
740 /// let sub: f32 = 1.1754942e-38;
741 /// let nan = f32::NAN;
743 /// assert_eq!(num.classify(), FpCategory::Normal);
744 /// assert_eq!(inf.classify(), FpCategory::Infinite);
745 /// assert_eq!(zero.classify(), FpCategory::Zero);
746 /// assert_eq!(nan.classify(), FpCategory::Nan);
747 /// assert_eq!(sub.classify(), FpCategory::Subnormal);
749 #[derive(Copy, Clone, PartialEq, Eq, Debug)]
750 #[stable(feature = "rust1", since = "1.0.0")]
751 pub enum FpCategory {
752 /// "Not a Number", often obtained by dividing by zero.
753 #[stable(feature = "rust1", since = "1.0.0")]
756 /// Positive or negative infinity.
757 #[stable(feature = "rust1", since = "1.0.0")]
760 /// Positive or negative zero.
761 #[stable(feature = "rust1", since = "1.0.0")]
764 /// De-normalized floating point representation (less precise than `Normal`).
765 #[stable(feature = "rust1", since = "1.0.0")]
768 /// A regular floating point number.
769 #[stable(feature = "rust1", since = "1.0.0")]
774 trait FromStrRadixHelper: PartialOrd + Copy {
775 fn min_value() -> Self;
776 fn max_value() -> Self;
777 fn from_u32(u: u32) -> Self;
778 fn checked_mul(&self, other: u32) -> Option<Self>;
779 fn checked_sub(&self, other: u32) -> Option<Self>;
780 fn checked_add(&self, other: u32) -> Option<Self>;
783 macro_rules! from_str_radix_int_impl {
785 #[stable(feature = "rust1", since = "1.0.0")]
786 impl FromStr for $t {
787 type Err = ParseIntError;
788 fn from_str(src: &str) -> Result<Self, ParseIntError> {
789 from_str_radix(src, 10)
794 from_str_radix_int_impl! { isize i8 i16 i32 i64 i128 usize u8 u16 u32 u64 u128 }
797 ($($t:ty)*) => ($(impl FromStrRadixHelper for $t {
799 fn min_value() -> Self { Self::MIN }
801 fn max_value() -> Self { Self::MAX }
803 fn from_u32(u: u32) -> Self { u as Self }
805 fn checked_mul(&self, other: u32) -> Option<Self> {
806 Self::checked_mul(*self, other as Self)
809 fn checked_sub(&self, other: u32) -> Option<Self> {
810 Self::checked_sub(*self, other as Self)
813 fn checked_add(&self, other: u32) -> Option<Self> {
814 Self::checked_add(*self, other as Self)
818 doit! { i8 i16 i32 i64 i128 isize u8 u16 u32 u64 u128 usize }
820 fn from_str_radix<T: FromStrRadixHelper>(src: &str, radix: u32) -> Result<T, ParseIntError> {
821 use self::IntErrorKind::*;
822 use self::ParseIntError as PIE;
825 radix >= 2 && radix <= 36,
826 "from_str_radix_int: must lie in the range `[2, 36]` - found {}",
831 return Err(PIE { kind: Empty });
834 let is_signed_ty = T::from_u32(0) > T::min_value();
836 // all valid digits are ascii, so we will just iterate over the utf8 bytes
837 // and cast them to chars. .to_digit() will safely return None for anything
838 // other than a valid ascii digit for the given radix, including the first-byte
839 // of multi-byte sequences
840 let src = src.as_bytes();
842 let (is_positive, digits) = match src[0] {
843 b'+' | b'-' if src[1..].is_empty() => {
844 return Err(PIE { kind: InvalidDigit });
846 b'+' => (true, &src[1..]),
847 b'-' if is_signed_ty => (false, &src[1..]),
851 let mut result = T::from_u32(0);
853 // The number is positive
855 let x = match (c as char).to_digit(radix) {
857 None => return Err(PIE { kind: InvalidDigit }),
859 result = match result.checked_mul(radix) {
860 Some(result) => result,
861 None => return Err(PIE { kind: PosOverflow }),
863 result = match result.checked_add(x) {
864 Some(result) => result,
865 None => return Err(PIE { kind: PosOverflow }),
869 // The number is negative
871 let x = match (c as char).to_digit(radix) {
873 None => return Err(PIE { kind: InvalidDigit }),
875 result = match result.checked_mul(radix) {
876 Some(result) => result,
877 None => return Err(PIE { kind: NegOverflow }),
879 result = match result.checked_sub(x) {
880 Some(result) => result,
881 None => return Err(PIE { kind: NegOverflow }),