1 //! Numeric traits and functions for the built-in numeric types.
3 #![stable(feature = "rust1", since = "1.0.0")]
8 use crate::str::FromStr;
10 // Used because the `?` operator is not allowed in a const context.
11 macro_rules! try_opt {
20 #[allow_internal_unstable(const_likely)]
21 macro_rules! unlikely {
23 intrinsics::unlikely($e)
27 // All these modules are technically private and only exposed for coretests:
28 #[cfg(not(no_fp_fmt_parse))]
30 #[cfg(not(no_fp_fmt_parse))]
32 #[cfg(not(no_fp_fmt_parse))]
34 #[cfg(not(no_fp_fmt_parse))]
39 mod int_macros; // import int_impl!
41 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 #[cfg(not(no_fp_fmt_parse))]
53 pub use dec2flt::ParseFloatError;
55 #[stable(feature = "rust1", since = "1.0.0")]
56 pub use error::ParseIntError;
58 #[stable(feature = "nonzero", since = "1.28.0")]
59 pub use nonzero::{NonZeroU128, NonZeroU16, NonZeroU32, NonZeroU64, NonZeroU8, NonZeroUsize};
61 #[stable(feature = "signed_nonzero", since = "1.34.0")]
62 pub use nonzero::{NonZeroI128, NonZeroI16, NonZeroI32, NonZeroI64, NonZeroI8, NonZeroIsize};
64 #[stable(feature = "try_from", since = "1.34.0")]
65 pub use error::TryFromIntError;
67 #[stable(feature = "int_error_matching", since = "1.55.0")]
68 pub use error::IntErrorKind;
70 macro_rules! usize_isize_to_xe_bytes_doc {
74 **Note**: This function returns an array of length 2, 4 or 8 bytes
75 depending on the target pointer size.
81 macro_rules! usize_isize_from_xe_bytes_doc {
85 **Note**: This function takes an array of length 2, 4 or 8 bytes
86 depending on the target pointer size.
94 int_impl! { i8, i8, u8, 8, 7, -128, 127, 2, "-0x7e", "0xa", "0x12", "0x12", "0x48",
95 "[0x12]", "[0x12]", "", "" }
100 int_impl! { i16, i16, u16, 16, 15, -32768, 32767, 4, "-0x5ffd", "0x3a", "0x1234", "0x3412",
101 "0x2c48", "[0x34, 0x12]", "[0x12, 0x34]", "", "" }
106 int_impl! { i32, i32, u32, 32, 31, -2147483648, 2147483647, 8, "0x10000b3", "0xb301",
107 "0x12345678", "0x78563412", "0x1e6a2c48", "[0x78, 0x56, 0x34, 0x12]",
108 "[0x12, 0x34, 0x56, 0x78]", "", "" }
113 int_impl! { i64, i64, u64, 64, 63, -9223372036854775808, 9223372036854775807, 12,
114 "0xaa00000000006e1", "0x6e10aa", "0x1234567890123456", "0x5634129078563412",
115 "0x6a2c48091e6a2c48", "[0x56, 0x34, 0x12, 0x90, 0x78, 0x56, 0x34, 0x12]",
116 "[0x12, 0x34, 0x56, 0x78, 0x90, 0x12, 0x34, 0x56]", "", "" }
121 int_impl! { i128, i128, u128, 128, 127, -170141183460469231731687303715884105728,
122 170141183460469231731687303715884105727, 16,
123 "0x13f40000000000000000000000004f76", "0x4f7613f4", "0x12345678901234567890123456789012",
124 "0x12907856341290785634129078563412", "0x48091e6a2c48091e6a2c48091e6a2c48",
125 "[0x12, 0x90, 0x78, 0x56, 0x34, 0x12, 0x90, 0x78, \
126 0x56, 0x34, 0x12, 0x90, 0x78, 0x56, 0x34, 0x12]",
127 "[0x12, 0x34, 0x56, 0x78, 0x90, 0x12, 0x34, 0x56, \
128 0x78, 0x90, 0x12, 0x34, 0x56, 0x78, 0x90, 0x12]", "", "" }
131 #[cfg(target_pointer_width = "16")]
134 int_impl! { isize, i16, usize, 16, 15, -32768, 32767, 4, "-0x5ffd", "0x3a", "0x1234",
135 "0x3412", "0x2c48", "[0x34, 0x12]", "[0x12, 0x34]",
136 usize_isize_to_xe_bytes_doc!(), usize_isize_from_xe_bytes_doc!() }
139 #[cfg(target_pointer_width = "32")]
142 int_impl! { isize, i32, usize, 32, 31, -2147483648, 2147483647, 8, "0x10000b3", "0xb301",
143 "0x12345678", "0x78563412", "0x1e6a2c48", "[0x78, 0x56, 0x34, 0x12]",
144 "[0x12, 0x34, 0x56, 0x78]",
145 usize_isize_to_xe_bytes_doc!(), usize_isize_from_xe_bytes_doc!() }
148 #[cfg(target_pointer_width = "64")]
151 int_impl! { isize, i64, usize, 64, 63, -9223372036854775808, 9223372036854775807,
152 12, "0xaa00000000006e1", "0x6e10aa", "0x1234567890123456", "0x5634129078563412",
153 "0x6a2c48091e6a2c48", "[0x56, 0x34, 0x12, 0x90, 0x78, 0x56, 0x34, 0x12]",
154 "[0x12, 0x34, 0x56, 0x78, 0x90, 0x12, 0x34, 0x56]",
155 usize_isize_to_xe_bytes_doc!(), usize_isize_from_xe_bytes_doc!() }
158 /// If 6th bit set ascii is upper case.
159 const ASCII_CASE_MASK: u8 = 0b0010_0000;
163 uint_impl! { u8, u8, 8, 255, 2, "0x82", "0xa", "0x12", "0x12", "0x48", "[0x12]",
166 /// Checks if the value is within the ASCII range.
171 /// let ascii = 97u8;
172 /// let non_ascii = 150u8;
174 /// assert!(ascii.is_ascii());
175 /// assert!(!non_ascii.is_ascii());
177 #[stable(feature = "ascii_methods_on_intrinsics", since = "1.23.0")]
178 #[rustc_const_stable(feature = "const_ascii_methods_on_intrinsics", since = "1.43.0")]
180 pub const fn is_ascii(&self) -> bool {
184 /// Makes a copy of the value in its ASCII upper case equivalent.
186 /// ASCII letters 'a' to 'z' are mapped to 'A' to 'Z',
187 /// but non-ASCII letters are unchanged.
189 /// To uppercase the value in-place, use [`make_ascii_uppercase`].
194 /// let lowercase_a = 97u8;
196 /// assert_eq!(65, lowercase_a.to_ascii_uppercase());
199 /// [`make_ascii_uppercase`]: Self::make_ascii_uppercase
200 #[stable(feature = "ascii_methods_on_intrinsics", since = "1.23.0")]
201 #[rustc_const_stable(feature = "const_ascii_methods_on_intrinsics", since = "1.52.0")]
203 pub const 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) * ASCII_CASE_MASK)
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`]: Self::make_ascii_lowercase
224 #[stable(feature = "ascii_methods_on_intrinsics", since = "1.23.0")]
225 #[rustc_const_stable(feature = "const_ascii_methods_on_intrinsics", since = "1.52.0")]
227 pub const fn to_ascii_lowercase(&self) -> u8 {
228 // Set the fifth bit if this is an uppercase letter
229 *self | (self.is_ascii_uppercase() as u8 * ASCII_CASE_MASK)
232 /// Assumes self is ascii
234 pub(crate) const fn ascii_change_case_unchecked(&self) -> u8 {
235 *self ^ ASCII_CASE_MASK
238 /// Checks that two values are an ASCII case-insensitive match.
240 /// This is equivalent to `to_ascii_lowercase(a) == to_ascii_lowercase(b)`.
245 /// let lowercase_a = 97u8;
246 /// let uppercase_a = 65u8;
248 /// assert!(lowercase_a.eq_ignore_ascii_case(&uppercase_a));
250 #[stable(feature = "ascii_methods_on_intrinsics", since = "1.23.0")]
251 #[rustc_const_stable(feature = "const_ascii_methods_on_intrinsics", since = "1.52.0")]
253 pub const fn eq_ignore_ascii_case(&self, other: &u8) -> bool {
254 self.to_ascii_lowercase() == other.to_ascii_lowercase()
257 /// Converts this value to its ASCII upper case equivalent in-place.
259 /// ASCII letters 'a' to 'z' are mapped to 'A' to 'Z',
260 /// but non-ASCII letters are unchanged.
262 /// To return a new uppercased value without modifying the existing one, use
263 /// [`to_ascii_uppercase`].
268 /// let mut byte = b'a';
270 /// byte.make_ascii_uppercase();
272 /// assert_eq!(b'A', byte);
275 /// [`to_ascii_uppercase`]: Self::to_ascii_uppercase
276 #[stable(feature = "ascii_methods_on_intrinsics", since = "1.23.0")]
278 pub fn make_ascii_uppercase(&mut self) {
279 *self = self.to_ascii_uppercase();
282 /// Converts this value to its ASCII lower case equivalent in-place.
284 /// ASCII letters 'A' to 'Z' are mapped to 'a' to 'z',
285 /// but non-ASCII letters are unchanged.
287 /// To return a new lowercased value without modifying the existing one, use
288 /// [`to_ascii_lowercase`].
293 /// let mut byte = b'A';
295 /// byte.make_ascii_lowercase();
297 /// assert_eq!(b'a', byte);
300 /// [`to_ascii_lowercase`]: Self::to_ascii_lowercase
301 #[stable(feature = "ascii_methods_on_intrinsics", since = "1.23.0")]
303 pub fn make_ascii_lowercase(&mut self) {
304 *self = self.to_ascii_lowercase();
307 /// Checks if the value is an ASCII alphabetic character:
309 /// - U+0041 'A' ..= U+005A 'Z', or
310 /// - U+0061 'a' ..= U+007A 'z'.
315 /// let uppercase_a = b'A';
316 /// let uppercase_g = b'G';
320 /// let percent = b'%';
321 /// let space = b' ';
323 /// let esc = 0x1b_u8;
325 /// assert!(uppercase_a.is_ascii_alphabetic());
326 /// assert!(uppercase_g.is_ascii_alphabetic());
327 /// assert!(a.is_ascii_alphabetic());
328 /// assert!(g.is_ascii_alphabetic());
329 /// assert!(!zero.is_ascii_alphabetic());
330 /// assert!(!percent.is_ascii_alphabetic());
331 /// assert!(!space.is_ascii_alphabetic());
332 /// assert!(!lf.is_ascii_alphabetic());
333 /// assert!(!esc.is_ascii_alphabetic());
335 #[stable(feature = "ascii_ctype_on_intrinsics", since = "1.24.0")]
336 #[rustc_const_stable(feature = "const_ascii_ctype_on_intrinsics", since = "1.47.0")]
338 pub const fn is_ascii_alphabetic(&self) -> bool {
339 matches!(*self, b'A'..=b'Z' | b'a'..=b'z')
342 /// Checks if the value is an ASCII uppercase character:
343 /// U+0041 'A' ..= U+005A 'Z'.
348 /// let uppercase_a = b'A';
349 /// let uppercase_g = b'G';
353 /// let percent = b'%';
354 /// let space = b' ';
356 /// let esc = 0x1b_u8;
358 /// assert!(uppercase_a.is_ascii_uppercase());
359 /// assert!(uppercase_g.is_ascii_uppercase());
360 /// assert!(!a.is_ascii_uppercase());
361 /// assert!(!g.is_ascii_uppercase());
362 /// assert!(!zero.is_ascii_uppercase());
363 /// assert!(!percent.is_ascii_uppercase());
364 /// assert!(!space.is_ascii_uppercase());
365 /// assert!(!lf.is_ascii_uppercase());
366 /// assert!(!esc.is_ascii_uppercase());
368 #[stable(feature = "ascii_ctype_on_intrinsics", since = "1.24.0")]
369 #[rustc_const_stable(feature = "const_ascii_ctype_on_intrinsics", since = "1.47.0")]
371 pub const fn is_ascii_uppercase(&self) -> bool {
372 matches!(*self, b'A'..=b'Z')
375 /// Checks if the value is an ASCII lowercase character:
376 /// U+0061 'a' ..= U+007A 'z'.
381 /// let uppercase_a = b'A';
382 /// let uppercase_g = b'G';
386 /// let percent = b'%';
387 /// let space = b' ';
389 /// let esc = 0x1b_u8;
391 /// assert!(!uppercase_a.is_ascii_lowercase());
392 /// assert!(!uppercase_g.is_ascii_lowercase());
393 /// assert!(a.is_ascii_lowercase());
394 /// assert!(g.is_ascii_lowercase());
395 /// assert!(!zero.is_ascii_lowercase());
396 /// assert!(!percent.is_ascii_lowercase());
397 /// assert!(!space.is_ascii_lowercase());
398 /// assert!(!lf.is_ascii_lowercase());
399 /// assert!(!esc.is_ascii_lowercase());
401 #[stable(feature = "ascii_ctype_on_intrinsics", since = "1.24.0")]
402 #[rustc_const_stable(feature = "const_ascii_ctype_on_intrinsics", since = "1.47.0")]
404 pub const fn is_ascii_lowercase(&self) -> bool {
405 matches!(*self, b'a'..=b'z')
408 /// Checks if the value is an ASCII alphanumeric character:
410 /// - U+0041 'A' ..= U+005A 'Z', or
411 /// - U+0061 'a' ..= U+007A 'z', or
412 /// - U+0030 '0' ..= U+0039 '9'.
417 /// let uppercase_a = b'A';
418 /// let uppercase_g = b'G';
422 /// let percent = b'%';
423 /// let space = b' ';
425 /// let esc = 0x1b_u8;
427 /// assert!(uppercase_a.is_ascii_alphanumeric());
428 /// assert!(uppercase_g.is_ascii_alphanumeric());
429 /// assert!(a.is_ascii_alphanumeric());
430 /// assert!(g.is_ascii_alphanumeric());
431 /// assert!(zero.is_ascii_alphanumeric());
432 /// assert!(!percent.is_ascii_alphanumeric());
433 /// assert!(!space.is_ascii_alphanumeric());
434 /// assert!(!lf.is_ascii_alphanumeric());
435 /// assert!(!esc.is_ascii_alphanumeric());
437 #[stable(feature = "ascii_ctype_on_intrinsics", since = "1.24.0")]
438 #[rustc_const_stable(feature = "const_ascii_ctype_on_intrinsics", since = "1.47.0")]
440 pub const fn is_ascii_alphanumeric(&self) -> bool {
441 matches!(*self, b'0'..=b'9' | b'A'..=b'Z' | b'a'..=b'z')
444 /// Checks if the value is an ASCII decimal digit:
445 /// U+0030 '0' ..= U+0039 '9'.
450 /// let uppercase_a = b'A';
451 /// let uppercase_g = b'G';
455 /// let percent = b'%';
456 /// let space = b' ';
458 /// let esc = 0x1b_u8;
460 /// assert!(!uppercase_a.is_ascii_digit());
461 /// assert!(!uppercase_g.is_ascii_digit());
462 /// assert!(!a.is_ascii_digit());
463 /// assert!(!g.is_ascii_digit());
464 /// assert!(zero.is_ascii_digit());
465 /// assert!(!percent.is_ascii_digit());
466 /// assert!(!space.is_ascii_digit());
467 /// assert!(!lf.is_ascii_digit());
468 /// assert!(!esc.is_ascii_digit());
470 #[stable(feature = "ascii_ctype_on_intrinsics", since = "1.24.0")]
471 #[rustc_const_stable(feature = "const_ascii_ctype_on_intrinsics", since = "1.47.0")]
473 pub const fn is_ascii_digit(&self) -> bool {
474 matches!(*self, b'0'..=b'9')
477 /// Checks if the value is an ASCII hexadecimal digit:
479 /// - U+0030 '0' ..= U+0039 '9', or
480 /// - U+0041 'A' ..= U+0046 'F', or
481 /// - U+0061 'a' ..= U+0066 'f'.
486 /// let uppercase_a = b'A';
487 /// let uppercase_g = b'G';
491 /// let percent = b'%';
492 /// let space = b' ';
494 /// let esc = 0x1b_u8;
496 /// assert!(uppercase_a.is_ascii_hexdigit());
497 /// assert!(!uppercase_g.is_ascii_hexdigit());
498 /// assert!(a.is_ascii_hexdigit());
499 /// assert!(!g.is_ascii_hexdigit());
500 /// assert!(zero.is_ascii_hexdigit());
501 /// assert!(!percent.is_ascii_hexdigit());
502 /// assert!(!space.is_ascii_hexdigit());
503 /// assert!(!lf.is_ascii_hexdigit());
504 /// assert!(!esc.is_ascii_hexdigit());
506 #[stable(feature = "ascii_ctype_on_intrinsics", since = "1.24.0")]
507 #[rustc_const_stable(feature = "const_ascii_ctype_on_intrinsics", since = "1.47.0")]
509 pub const fn is_ascii_hexdigit(&self) -> bool {
510 matches!(*self, b'0'..=b'9' | b'A'..=b'F' | b'a'..=b'f')
513 /// Checks if the value is an ASCII punctuation character:
515 /// - U+0021 ..= U+002F `! " # $ % & ' ( ) * + , - . /`, or
516 /// - U+003A ..= U+0040 `: ; < = > ? @`, or
517 /// - U+005B ..= U+0060 ``[ \ ] ^ _ ` ``, or
518 /// - U+007B ..= U+007E `{ | } ~`
523 /// let uppercase_a = b'A';
524 /// let uppercase_g = b'G';
528 /// let percent = b'%';
529 /// let space = b' ';
531 /// let esc = 0x1b_u8;
533 /// assert!(!uppercase_a.is_ascii_punctuation());
534 /// assert!(!uppercase_g.is_ascii_punctuation());
535 /// assert!(!a.is_ascii_punctuation());
536 /// assert!(!g.is_ascii_punctuation());
537 /// assert!(!zero.is_ascii_punctuation());
538 /// assert!(percent.is_ascii_punctuation());
539 /// assert!(!space.is_ascii_punctuation());
540 /// assert!(!lf.is_ascii_punctuation());
541 /// assert!(!esc.is_ascii_punctuation());
543 #[stable(feature = "ascii_ctype_on_intrinsics", since = "1.24.0")]
544 #[rustc_const_stable(feature = "const_ascii_ctype_on_intrinsics", since = "1.47.0")]
546 pub const fn is_ascii_punctuation(&self) -> bool {
547 matches!(*self, b'!'..=b'/' | b':'..=b'@' | b'['..=b'`' | b'{'..=b'~')
550 /// Checks if the value is an ASCII graphic character:
551 /// U+0021 '!' ..= U+007E '~'.
556 /// let uppercase_a = b'A';
557 /// let uppercase_g = b'G';
561 /// let percent = b'%';
562 /// let space = b' ';
564 /// let esc = 0x1b_u8;
566 /// assert!(uppercase_a.is_ascii_graphic());
567 /// assert!(uppercase_g.is_ascii_graphic());
568 /// assert!(a.is_ascii_graphic());
569 /// assert!(g.is_ascii_graphic());
570 /// assert!(zero.is_ascii_graphic());
571 /// assert!(percent.is_ascii_graphic());
572 /// assert!(!space.is_ascii_graphic());
573 /// assert!(!lf.is_ascii_graphic());
574 /// assert!(!esc.is_ascii_graphic());
576 #[stable(feature = "ascii_ctype_on_intrinsics", since = "1.24.0")]
577 #[rustc_const_stable(feature = "const_ascii_ctype_on_intrinsics", since = "1.47.0")]
579 pub const fn is_ascii_graphic(&self) -> bool {
580 matches!(*self, b'!'..=b'~')
583 /// Checks if the value is an ASCII whitespace character:
584 /// U+0020 SPACE, U+0009 HORIZONTAL TAB, U+000A LINE FEED,
585 /// U+000C FORM FEED, or U+000D CARRIAGE RETURN.
587 /// Rust uses the WhatWG Infra Standard's [definition of ASCII
588 /// whitespace][infra-aw]. There are several other definitions in
589 /// wide use. For instance, [the POSIX locale][pct] includes
590 /// U+000B VERTICAL TAB as well as all the above characters,
591 /// but—from the very same specification—[the default rule for
592 /// "field splitting" in the Bourne shell][bfs] considers *only*
593 /// SPACE, HORIZONTAL TAB, and LINE FEED as whitespace.
595 /// If you are writing a program that will process an existing
596 /// file format, check what that format's definition of whitespace is
597 /// before using this function.
599 /// [infra-aw]: https://infra.spec.whatwg.org/#ascii-whitespace
600 /// [pct]: https://pubs.opengroup.org/onlinepubs/9699919799/basedefs/V1_chap07.html#tag_07_03_01
601 /// [bfs]: https://pubs.opengroup.org/onlinepubs/9699919799/utilities/V3_chap02.html#tag_18_06_05
606 /// let uppercase_a = b'A';
607 /// let uppercase_g = b'G';
611 /// let percent = b'%';
612 /// let space = b' ';
614 /// let esc = 0x1b_u8;
616 /// assert!(!uppercase_a.is_ascii_whitespace());
617 /// assert!(!uppercase_g.is_ascii_whitespace());
618 /// assert!(!a.is_ascii_whitespace());
619 /// assert!(!g.is_ascii_whitespace());
620 /// assert!(!zero.is_ascii_whitespace());
621 /// assert!(!percent.is_ascii_whitespace());
622 /// assert!(space.is_ascii_whitespace());
623 /// assert!(lf.is_ascii_whitespace());
624 /// assert!(!esc.is_ascii_whitespace());
626 #[stable(feature = "ascii_ctype_on_intrinsics", since = "1.24.0")]
627 #[rustc_const_stable(feature = "const_ascii_ctype_on_intrinsics", since = "1.47.0")]
629 pub const fn is_ascii_whitespace(&self) -> bool {
630 matches!(*self, b'\t' | b'\n' | b'\x0C' | b'\r' | b' ')
633 /// Checks if the value is an ASCII control character:
634 /// U+0000 NUL ..= U+001F UNIT SEPARATOR, or U+007F DELETE.
635 /// Note that most ASCII whitespace characters are control
636 /// characters, but SPACE is not.
641 /// let uppercase_a = b'A';
642 /// let uppercase_g = b'G';
646 /// let percent = b'%';
647 /// let space = b' ';
649 /// let esc = 0x1b_u8;
651 /// assert!(!uppercase_a.is_ascii_control());
652 /// assert!(!uppercase_g.is_ascii_control());
653 /// assert!(!a.is_ascii_control());
654 /// assert!(!g.is_ascii_control());
655 /// assert!(!zero.is_ascii_control());
656 /// assert!(!percent.is_ascii_control());
657 /// assert!(!space.is_ascii_control());
658 /// assert!(lf.is_ascii_control());
659 /// assert!(esc.is_ascii_control());
661 #[stable(feature = "ascii_ctype_on_intrinsics", since = "1.24.0")]
662 #[rustc_const_stable(feature = "const_ascii_ctype_on_intrinsics", since = "1.47.0")]
664 pub const fn is_ascii_control(&self) -> bool {
665 matches!(*self, b'\0'..=b'\x1F' | b'\x7F')
668 /// Returns an iterator that produces an escaped version of a `u8`,
669 /// treating it as an ASCII character.
671 /// The behavior is identical to [`ascii::escape_default`].
676 /// #![feature(inherent_ascii_escape)]
678 /// assert_eq!("0", b'0'.escape_ascii().to_string());
679 /// assert_eq!("\\t", b'\t'.escape_ascii().to_string());
680 /// assert_eq!("\\r", b'\r'.escape_ascii().to_string());
681 /// assert_eq!("\\n", b'\n'.escape_ascii().to_string());
682 /// assert_eq!("\\'", b'\''.escape_ascii().to_string());
683 /// assert_eq!("\\\"", b'"'.escape_ascii().to_string());
684 /// assert_eq!("\\\\", b'\\'.escape_ascii().to_string());
685 /// assert_eq!("\\x9d", b'\x9d'.escape_ascii().to_string());
687 #[unstable(feature = "inherent_ascii_escape", issue = "77174")]
689 pub fn escape_ascii(&self) -> ascii::EscapeDefault {
690 ascii::escape_default(*self)
696 uint_impl! { u16, u16, 16, 65535, 4, "0xa003", "0x3a", "0x1234", "0x3412", "0x2c48",
697 "[0x34, 0x12]", "[0x12, 0x34]", "", "" }
702 uint_impl! { u32, u32, 32, 4294967295, 8, "0x10000b3", "0xb301", "0x12345678",
703 "0x78563412", "0x1e6a2c48", "[0x78, 0x56, 0x34, 0x12]", "[0x12, 0x34, 0x56, 0x78]", "", "" }
708 uint_impl! { u64, u64, 64, 18446744073709551615, 12, "0xaa00000000006e1", "0x6e10aa",
709 "0x1234567890123456", "0x5634129078563412", "0x6a2c48091e6a2c48",
710 "[0x56, 0x34, 0x12, 0x90, 0x78, 0x56, 0x34, 0x12]",
711 "[0x12, 0x34, 0x56, 0x78, 0x90, 0x12, 0x34, 0x56]",
717 uint_impl! { u128, u128, 128, 340282366920938463463374607431768211455, 16,
718 "0x13f40000000000000000000000004f76", "0x4f7613f4", "0x12345678901234567890123456789012",
719 "0x12907856341290785634129078563412", "0x48091e6a2c48091e6a2c48091e6a2c48",
720 "[0x12, 0x90, 0x78, 0x56, 0x34, 0x12, 0x90, 0x78, \
721 0x56, 0x34, 0x12, 0x90, 0x78, 0x56, 0x34, 0x12]",
722 "[0x12, 0x34, 0x56, 0x78, 0x90, 0x12, 0x34, 0x56, \
723 0x78, 0x90, 0x12, 0x34, 0x56, 0x78, 0x90, 0x12]",
727 #[cfg(target_pointer_width = "16")]
730 uint_impl! { usize, u16, 16, 65535, 4, "0xa003", "0x3a", "0x1234", "0x3412", "0x2c48",
731 "[0x34, 0x12]", "[0x12, 0x34]",
732 usize_isize_to_xe_bytes_doc!(), usize_isize_from_xe_bytes_doc!() }
734 #[cfg(target_pointer_width = "32")]
737 uint_impl! { usize, u32, 32, 4294967295, 8, "0x10000b3", "0xb301", "0x12345678",
738 "0x78563412", "0x1e6a2c48", "[0x78, 0x56, 0x34, 0x12]", "[0x12, 0x34, 0x56, 0x78]",
739 usize_isize_to_xe_bytes_doc!(), usize_isize_from_xe_bytes_doc!() }
742 #[cfg(target_pointer_width = "64")]
745 uint_impl! { usize, u64, 64, 18446744073709551615, 12, "0xaa00000000006e1", "0x6e10aa",
746 "0x1234567890123456", "0x5634129078563412", "0x6a2c48091e6a2c48",
747 "[0x56, 0x34, 0x12, 0x90, 0x78, 0x56, 0x34, 0x12]",
748 "[0x12, 0x34, 0x56, 0x78, 0x90, 0x12, 0x34, 0x56]",
749 usize_isize_to_xe_bytes_doc!(), usize_isize_from_xe_bytes_doc!() }
752 /// A classification of floating point numbers.
754 /// This `enum` is used as the return type for [`f32::classify`] and [`f64::classify`]. See
755 /// their documentation for more.
760 /// use std::num::FpCategory;
762 /// let num = 12.4_f32;
763 /// let inf = f32::INFINITY;
765 /// let sub: f32 = 1.1754942e-38;
766 /// let nan = f32::NAN;
768 /// assert_eq!(num.classify(), FpCategory::Normal);
769 /// assert_eq!(inf.classify(), FpCategory::Infinite);
770 /// assert_eq!(zero.classify(), FpCategory::Zero);
771 /// assert_eq!(nan.classify(), FpCategory::Nan);
772 /// assert_eq!(sub.classify(), FpCategory::Subnormal);
774 #[derive(Copy, Clone, PartialEq, Eq, Debug)]
775 #[stable(feature = "rust1", since = "1.0.0")]
776 pub enum FpCategory {
777 /// "Not a Number", often obtained by dividing by zero.
778 #[stable(feature = "rust1", since = "1.0.0")]
781 /// Positive or negative infinity.
782 #[stable(feature = "rust1", since = "1.0.0")]
785 /// Positive or negative zero.
786 #[stable(feature = "rust1", since = "1.0.0")]
789 /// De-normalized floating point representation (less precise than `Normal`).
790 #[stable(feature = "rust1", since = "1.0.0")]
793 /// A regular floating point number.
794 #[stable(feature = "rust1", since = "1.0.0")]
799 trait FromStrRadixHelper: PartialOrd + Copy {
800 fn min_value() -> Self;
801 fn max_value() -> Self;
802 fn from_u32(u: u32) -> Self;
803 fn checked_mul(&self, other: u32) -> Option<Self>;
804 fn checked_sub(&self, other: u32) -> Option<Self>;
805 fn checked_add(&self, other: u32) -> Option<Self>;
808 macro_rules! from_str_radix_int_impl {
810 #[stable(feature = "rust1", since = "1.0.0")]
811 impl FromStr for $t {
812 type Err = ParseIntError;
813 fn from_str(src: &str) -> Result<Self, ParseIntError> {
814 from_str_radix(src, 10)
819 from_str_radix_int_impl! { isize i8 i16 i32 i64 i128 usize u8 u16 u32 u64 u128 }
822 ($($t:ty)*) => ($(impl FromStrRadixHelper for $t {
824 fn min_value() -> Self { Self::MIN }
826 fn max_value() -> Self { Self::MAX }
828 fn from_u32(u: u32) -> Self { u as Self }
830 fn checked_mul(&self, other: u32) -> Option<Self> {
831 Self::checked_mul(*self, other as Self)
834 fn checked_sub(&self, other: u32) -> Option<Self> {
835 Self::checked_sub(*self, other as Self)
838 fn checked_add(&self, other: u32) -> Option<Self> {
839 Self::checked_add(*self, other as Self)
843 doit! { i8 i16 i32 i64 i128 isize u8 u16 u32 u64 u128 usize }
845 fn from_str_radix<T: FromStrRadixHelper>(src: &str, radix: u32) -> Result<T, ParseIntError> {
846 use self::IntErrorKind::*;
847 use self::ParseIntError as PIE;
850 (2..=36).contains(&radix),
851 "from_str_radix_int: must lie in the range `[2, 36]` - found {}",
856 return Err(PIE { kind: Empty });
859 let is_signed_ty = T::from_u32(0) > T::min_value();
861 // all valid digits are ascii, so we will just iterate over the utf8 bytes
862 // and cast them to chars. .to_digit() will safely return None for anything
863 // other than a valid ascii digit for the given radix, including the first-byte
864 // of multi-byte sequences
865 let src = src.as_bytes();
867 let (is_positive, digits) = match src[0] {
868 b'+' | b'-' if src[1..].is_empty() => {
869 return Err(PIE { kind: InvalidDigit });
871 b'+' => (true, &src[1..]),
872 b'-' if is_signed_ty => (false, &src[1..]),
876 let mut result = T::from_u32(0);
878 // The number is positive
880 let x = match (c as char).to_digit(radix) {
882 None => return Err(PIE { kind: InvalidDigit }),
884 result = match result.checked_mul(radix) {
885 Some(result) => result,
886 None => return Err(PIE { kind: PosOverflow }),
888 result = match result.checked_add(x) {
889 Some(result) => result,
890 None => return Err(PIE { kind: PosOverflow }),
894 // The number is negative
896 let x = match (c as char).to_digit(radix) {
898 None => return Err(PIE { kind: InvalidDigit }),
900 result = match result.checked_mul(radix) {
901 Some(result) => result,
902 None => return Err(PIE { kind: NegOverflow }),
904 result = match result.checked_sub(x) {
905 Some(result) => result,
906 None => return Err(PIE { kind: NegOverflow }),