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!() }
157 uint_impl! { u8, u8, 8, 255, 2, "0x82", "0xa", "0x12", "0x12", "0x48", "[0x12]",
160 /// Checks if the value is within the ASCII range.
165 /// let ascii = 97u8;
166 /// let non_ascii = 150u8;
168 /// assert!(ascii.is_ascii());
169 /// assert!(!non_ascii.is_ascii());
171 #[stable(feature = "ascii_methods_on_intrinsics", since = "1.23.0")]
172 #[rustc_const_stable(feature = "const_ascii_methods_on_intrinsics", since = "1.43.0")]
174 pub const fn is_ascii(&self) -> bool {
178 /// Makes a copy of the value in its ASCII upper case equivalent.
180 /// ASCII letters 'a' to 'z' are mapped to 'A' to 'Z',
181 /// but non-ASCII letters are unchanged.
183 /// To uppercase the value in-place, use [`make_ascii_uppercase`].
188 /// let lowercase_a = 97u8;
190 /// assert_eq!(65, lowercase_a.to_ascii_uppercase());
193 /// [`make_ascii_uppercase`]: #method.make_ascii_uppercase
194 #[stable(feature = "ascii_methods_on_intrinsics", since = "1.23.0")]
196 pub fn to_ascii_uppercase(&self) -> u8 {
197 // Unset the fifth bit if this is a lowercase letter
198 *self & !((self.is_ascii_lowercase() as u8) << 5)
201 /// Makes a copy of the value in its ASCII lower case equivalent.
203 /// ASCII letters 'A' to 'Z' are mapped to 'a' to 'z',
204 /// but non-ASCII letters are unchanged.
206 /// To lowercase the value in-place, use [`make_ascii_lowercase`].
211 /// let uppercase_a = 65u8;
213 /// assert_eq!(97, uppercase_a.to_ascii_lowercase());
216 /// [`make_ascii_lowercase`]: #method.make_ascii_lowercase
217 #[stable(feature = "ascii_methods_on_intrinsics", since = "1.23.0")]
219 pub fn to_ascii_lowercase(&self) -> u8 {
220 // Set the fifth bit if this is an uppercase letter
221 *self | ((self.is_ascii_uppercase() as u8) << 5)
224 /// Checks that two values are an ASCII case-insensitive match.
226 /// This is equivalent to `to_ascii_lowercase(a) == to_ascii_lowercase(b)`.
231 /// let lowercase_a = 97u8;
232 /// let uppercase_a = 65u8;
234 /// assert!(lowercase_a.eq_ignore_ascii_case(&uppercase_a));
236 #[stable(feature = "ascii_methods_on_intrinsics", since = "1.23.0")]
238 pub fn eq_ignore_ascii_case(&self, other: &u8) -> bool {
239 self.to_ascii_lowercase() == other.to_ascii_lowercase()
242 /// Converts this value to its ASCII upper case equivalent in-place.
244 /// ASCII letters 'a' to 'z' are mapped to 'A' to 'Z',
245 /// but non-ASCII letters are unchanged.
247 /// To return a new uppercased value without modifying the existing one, use
248 /// [`to_ascii_uppercase`].
253 /// let mut byte = b'a';
255 /// byte.make_ascii_uppercase();
257 /// assert_eq!(b'A', byte);
260 /// [`to_ascii_uppercase`]: #method.to_ascii_uppercase
261 #[stable(feature = "ascii_methods_on_intrinsics", since = "1.23.0")]
263 pub fn make_ascii_uppercase(&mut self) {
264 *self = self.to_ascii_uppercase();
267 /// Converts this value to its ASCII lower case equivalent in-place.
269 /// ASCII letters 'A' to 'Z' are mapped to 'a' to 'z',
270 /// but non-ASCII letters are unchanged.
272 /// To return a new lowercased value without modifying the existing one, use
273 /// [`to_ascii_lowercase`].
278 /// let mut byte = b'A';
280 /// byte.make_ascii_lowercase();
282 /// assert_eq!(b'a', byte);
285 /// [`to_ascii_lowercase`]: #method.to_ascii_lowercase
286 #[stable(feature = "ascii_methods_on_intrinsics", since = "1.23.0")]
288 pub fn make_ascii_lowercase(&mut self) {
289 *self = self.to_ascii_lowercase();
292 /// Checks if the value is an ASCII alphabetic character:
294 /// - U+0041 'A' ..= U+005A 'Z', or
295 /// - U+0061 'a' ..= U+007A 'z'.
300 /// let uppercase_a = b'A';
301 /// let uppercase_g = b'G';
305 /// let percent = b'%';
306 /// let space = b' ';
308 /// let esc = 0x1b_u8;
310 /// assert!(uppercase_a.is_ascii_alphabetic());
311 /// assert!(uppercase_g.is_ascii_alphabetic());
312 /// assert!(a.is_ascii_alphabetic());
313 /// assert!(g.is_ascii_alphabetic());
314 /// assert!(!zero.is_ascii_alphabetic());
315 /// assert!(!percent.is_ascii_alphabetic());
316 /// assert!(!space.is_ascii_alphabetic());
317 /// assert!(!lf.is_ascii_alphabetic());
318 /// assert!(!esc.is_ascii_alphabetic());
320 #[stable(feature = "ascii_ctype_on_intrinsics", since = "1.24.0")]
321 #[rustc_const_stable(feature = "const_ascii_ctype_on_intrinsics", since = "1.47.0")]
323 pub const fn is_ascii_alphabetic(&self) -> bool {
324 matches!(*self, b'A'..=b'Z' | b'a'..=b'z')
327 /// Checks if the value is an ASCII uppercase character:
328 /// U+0041 'A' ..= U+005A 'Z'.
333 /// let uppercase_a = b'A';
334 /// let uppercase_g = b'G';
338 /// let percent = b'%';
339 /// let space = b' ';
341 /// let esc = 0x1b_u8;
343 /// assert!(uppercase_a.is_ascii_uppercase());
344 /// assert!(uppercase_g.is_ascii_uppercase());
345 /// assert!(!a.is_ascii_uppercase());
346 /// assert!(!g.is_ascii_uppercase());
347 /// assert!(!zero.is_ascii_uppercase());
348 /// assert!(!percent.is_ascii_uppercase());
349 /// assert!(!space.is_ascii_uppercase());
350 /// assert!(!lf.is_ascii_uppercase());
351 /// assert!(!esc.is_ascii_uppercase());
353 #[stable(feature = "ascii_ctype_on_intrinsics", since = "1.24.0")]
354 #[rustc_const_stable(feature = "const_ascii_ctype_on_intrinsics", since = "1.47.0")]
356 pub const fn is_ascii_uppercase(&self) -> bool {
357 matches!(*self, b'A'..=b'Z')
360 /// Checks if the value is an ASCII lowercase character:
361 /// U+0061 'a' ..= U+007A 'z'.
366 /// let uppercase_a = b'A';
367 /// let uppercase_g = b'G';
371 /// let percent = b'%';
372 /// let space = b' ';
374 /// let esc = 0x1b_u8;
376 /// assert!(!uppercase_a.is_ascii_lowercase());
377 /// assert!(!uppercase_g.is_ascii_lowercase());
378 /// assert!(a.is_ascii_lowercase());
379 /// assert!(g.is_ascii_lowercase());
380 /// assert!(!zero.is_ascii_lowercase());
381 /// assert!(!percent.is_ascii_lowercase());
382 /// assert!(!space.is_ascii_lowercase());
383 /// assert!(!lf.is_ascii_lowercase());
384 /// assert!(!esc.is_ascii_lowercase());
386 #[stable(feature = "ascii_ctype_on_intrinsics", since = "1.24.0")]
387 #[rustc_const_stable(feature = "const_ascii_ctype_on_intrinsics", since = "1.47.0")]
389 pub const fn is_ascii_lowercase(&self) -> bool {
390 matches!(*self, b'a'..=b'z')
393 /// Checks if the value is an ASCII alphanumeric character:
395 /// - U+0041 'A' ..= U+005A 'Z', or
396 /// - U+0061 'a' ..= U+007A 'z', or
397 /// - U+0030 '0' ..= U+0039 '9'.
402 /// let uppercase_a = b'A';
403 /// let uppercase_g = b'G';
407 /// let percent = b'%';
408 /// let space = b' ';
410 /// let esc = 0x1b_u8;
412 /// assert!(uppercase_a.is_ascii_alphanumeric());
413 /// assert!(uppercase_g.is_ascii_alphanumeric());
414 /// assert!(a.is_ascii_alphanumeric());
415 /// assert!(g.is_ascii_alphanumeric());
416 /// assert!(zero.is_ascii_alphanumeric());
417 /// assert!(!percent.is_ascii_alphanumeric());
418 /// assert!(!space.is_ascii_alphanumeric());
419 /// assert!(!lf.is_ascii_alphanumeric());
420 /// assert!(!esc.is_ascii_alphanumeric());
422 #[stable(feature = "ascii_ctype_on_intrinsics", since = "1.24.0")]
423 #[rustc_const_stable(feature = "const_ascii_ctype_on_intrinsics", since = "1.47.0")]
425 pub const fn is_ascii_alphanumeric(&self) -> bool {
426 matches!(*self, b'0'..=b'9' | b'A'..=b'Z' | b'a'..=b'z')
429 /// Checks if the value is an ASCII decimal digit:
430 /// U+0030 '0' ..= U+0039 '9'.
435 /// let uppercase_a = b'A';
436 /// let uppercase_g = b'G';
440 /// let percent = b'%';
441 /// let space = b' ';
443 /// let esc = 0x1b_u8;
445 /// assert!(!uppercase_a.is_ascii_digit());
446 /// assert!(!uppercase_g.is_ascii_digit());
447 /// assert!(!a.is_ascii_digit());
448 /// assert!(!g.is_ascii_digit());
449 /// assert!(zero.is_ascii_digit());
450 /// assert!(!percent.is_ascii_digit());
451 /// assert!(!space.is_ascii_digit());
452 /// assert!(!lf.is_ascii_digit());
453 /// assert!(!esc.is_ascii_digit());
455 #[stable(feature = "ascii_ctype_on_intrinsics", since = "1.24.0")]
456 #[rustc_const_stable(feature = "const_ascii_ctype_on_intrinsics", since = "1.47.0")]
458 pub const fn is_ascii_digit(&self) -> bool {
459 matches!(*self, b'0'..=b'9')
462 /// Checks if the value is an ASCII hexadecimal digit:
464 /// - U+0030 '0' ..= U+0039 '9', or
465 /// - U+0041 'A' ..= U+0046 'F', or
466 /// - U+0061 'a' ..= U+0066 'f'.
471 /// let uppercase_a = b'A';
472 /// let uppercase_g = b'G';
476 /// let percent = b'%';
477 /// let space = b' ';
479 /// let esc = 0x1b_u8;
481 /// assert!(uppercase_a.is_ascii_hexdigit());
482 /// assert!(!uppercase_g.is_ascii_hexdigit());
483 /// assert!(a.is_ascii_hexdigit());
484 /// assert!(!g.is_ascii_hexdigit());
485 /// assert!(zero.is_ascii_hexdigit());
486 /// assert!(!percent.is_ascii_hexdigit());
487 /// assert!(!space.is_ascii_hexdigit());
488 /// assert!(!lf.is_ascii_hexdigit());
489 /// assert!(!esc.is_ascii_hexdigit());
491 #[stable(feature = "ascii_ctype_on_intrinsics", since = "1.24.0")]
492 #[rustc_const_stable(feature = "const_ascii_ctype_on_intrinsics", since = "1.47.0")]
494 pub const fn is_ascii_hexdigit(&self) -> bool {
495 matches!(*self, b'0'..=b'9' | b'A'..=b'F' | b'a'..=b'f')
498 /// Checks if the value is an ASCII punctuation character:
500 /// - U+0021 ..= U+002F `! " # $ % & ' ( ) * + , - . /`, or
501 /// - U+003A ..= U+0040 `: ; < = > ? @`, or
502 /// - U+005B ..= U+0060 ``[ \ ] ^ _ ` ``, or
503 /// - U+007B ..= U+007E `{ | } ~`
508 /// let uppercase_a = b'A';
509 /// let uppercase_g = b'G';
513 /// let percent = b'%';
514 /// let space = b' ';
516 /// let esc = 0x1b_u8;
518 /// assert!(!uppercase_a.is_ascii_punctuation());
519 /// assert!(!uppercase_g.is_ascii_punctuation());
520 /// assert!(!a.is_ascii_punctuation());
521 /// assert!(!g.is_ascii_punctuation());
522 /// assert!(!zero.is_ascii_punctuation());
523 /// assert!(percent.is_ascii_punctuation());
524 /// assert!(!space.is_ascii_punctuation());
525 /// assert!(!lf.is_ascii_punctuation());
526 /// assert!(!esc.is_ascii_punctuation());
528 #[stable(feature = "ascii_ctype_on_intrinsics", since = "1.24.0")]
529 #[rustc_const_stable(feature = "const_ascii_ctype_on_intrinsics", since = "1.47.0")]
531 pub const fn is_ascii_punctuation(&self) -> bool {
532 matches!(*self, b'!'..=b'/' | b':'..=b'@' | b'['..=b'`' | b'{'..=b'~')
535 /// Checks if the value is an ASCII graphic character:
536 /// U+0021 '!' ..= U+007E '~'.
541 /// let uppercase_a = b'A';
542 /// let uppercase_g = b'G';
546 /// let percent = b'%';
547 /// let space = b' ';
549 /// let esc = 0x1b_u8;
551 /// assert!(uppercase_a.is_ascii_graphic());
552 /// assert!(uppercase_g.is_ascii_graphic());
553 /// assert!(a.is_ascii_graphic());
554 /// assert!(g.is_ascii_graphic());
555 /// assert!(zero.is_ascii_graphic());
556 /// assert!(percent.is_ascii_graphic());
557 /// assert!(!space.is_ascii_graphic());
558 /// assert!(!lf.is_ascii_graphic());
559 /// assert!(!esc.is_ascii_graphic());
561 #[stable(feature = "ascii_ctype_on_intrinsics", since = "1.24.0")]
562 #[rustc_const_stable(feature = "const_ascii_ctype_on_intrinsics", since = "1.47.0")]
564 pub const fn is_ascii_graphic(&self) -> bool {
565 matches!(*self, b'!'..=b'~')
568 /// Checks if the value is an ASCII whitespace character:
569 /// U+0020 SPACE, U+0009 HORIZONTAL TAB, U+000A LINE FEED,
570 /// U+000C FORM FEED, or U+000D CARRIAGE RETURN.
572 /// Rust uses the WhatWG Infra Standard's [definition of ASCII
573 /// whitespace][infra-aw]. There are several other definitions in
574 /// wide use. For instance, [the POSIX locale][pct] includes
575 /// U+000B VERTICAL TAB as well as all the above characters,
576 /// but—from the very same specification—[the default rule for
577 /// "field splitting" in the Bourne shell][bfs] considers *only*
578 /// SPACE, HORIZONTAL TAB, and LINE FEED as whitespace.
580 /// If you are writing a program that will process an existing
581 /// file format, check what that format's definition of whitespace is
582 /// before using this function.
584 /// [infra-aw]: https://infra.spec.whatwg.org/#ascii-whitespace
585 /// [pct]: http://pubs.opengroup.org/onlinepubs/9699919799/basedefs/V1_chap07.html#tag_07_03_01
586 /// [bfs]: http://pubs.opengroup.org/onlinepubs/9699919799/utilities/V3_chap02.html#tag_18_06_05
591 /// let uppercase_a = b'A';
592 /// let uppercase_g = b'G';
596 /// let percent = b'%';
597 /// let space = b' ';
599 /// let esc = 0x1b_u8;
601 /// assert!(!uppercase_a.is_ascii_whitespace());
602 /// assert!(!uppercase_g.is_ascii_whitespace());
603 /// assert!(!a.is_ascii_whitespace());
604 /// assert!(!g.is_ascii_whitespace());
605 /// assert!(!zero.is_ascii_whitespace());
606 /// assert!(!percent.is_ascii_whitespace());
607 /// assert!(space.is_ascii_whitespace());
608 /// assert!(lf.is_ascii_whitespace());
609 /// assert!(!esc.is_ascii_whitespace());
611 #[stable(feature = "ascii_ctype_on_intrinsics", since = "1.24.0")]
612 #[rustc_const_stable(feature = "const_ascii_ctype_on_intrinsics", since = "1.47.0")]
614 pub const fn is_ascii_whitespace(&self) -> bool {
615 matches!(*self, b'\t' | b'\n' | b'\x0C' | b'\r' | b' ')
618 /// Checks if the value is an ASCII control character:
619 /// U+0000 NUL ..= U+001F UNIT SEPARATOR, or U+007F DELETE.
620 /// Note that most ASCII whitespace characters are control
621 /// characters, but SPACE is not.
626 /// let uppercase_a = b'A';
627 /// let uppercase_g = b'G';
631 /// let percent = b'%';
632 /// let space = b' ';
634 /// let esc = 0x1b_u8;
636 /// assert!(!uppercase_a.is_ascii_control());
637 /// assert!(!uppercase_g.is_ascii_control());
638 /// assert!(!a.is_ascii_control());
639 /// assert!(!g.is_ascii_control());
640 /// assert!(!zero.is_ascii_control());
641 /// assert!(!percent.is_ascii_control());
642 /// assert!(!space.is_ascii_control());
643 /// assert!(lf.is_ascii_control());
644 /// assert!(esc.is_ascii_control());
646 #[stable(feature = "ascii_ctype_on_intrinsics", since = "1.24.0")]
647 #[rustc_const_stable(feature = "const_ascii_ctype_on_intrinsics", since = "1.47.0")]
649 pub const fn is_ascii_control(&self) -> bool {
650 matches!(*self, b'\0'..=b'\x1F' | b'\x7F')
656 uint_impl! { u16, u16, 16, 65535, 4, "0xa003", "0x3a", "0x1234", "0x3412", "0x2c48",
657 "[0x34, 0x12]", "[0x12, 0x34]", "", "" }
662 uint_impl! { u32, u32, 32, 4294967295, 8, "0x10000b3", "0xb301", "0x12345678",
663 "0x78563412", "0x1e6a2c48", "[0x78, 0x56, 0x34, 0x12]", "[0x12, 0x34, 0x56, 0x78]", "", "" }
668 uint_impl! { u64, u64, 64, 18446744073709551615, 12, "0xaa00000000006e1", "0x6e10aa",
669 "0x1234567890123456", "0x5634129078563412", "0x6a2c48091e6a2c48",
670 "[0x56, 0x34, 0x12, 0x90, 0x78, 0x56, 0x34, 0x12]",
671 "[0x12, 0x34, 0x56, 0x78, 0x90, 0x12, 0x34, 0x56]",
677 uint_impl! { u128, u128, 128, 340282366920938463463374607431768211455, 16,
678 "0x13f40000000000000000000000004f76", "0x4f7613f4", "0x12345678901234567890123456789012",
679 "0x12907856341290785634129078563412", "0x48091e6a2c48091e6a2c48091e6a2c48",
680 "[0x12, 0x90, 0x78, 0x56, 0x34, 0x12, 0x90, 0x78, \
681 0x56, 0x34, 0x12, 0x90, 0x78, 0x56, 0x34, 0x12]",
682 "[0x12, 0x34, 0x56, 0x78, 0x90, 0x12, 0x34, 0x56, \
683 0x78, 0x90, 0x12, 0x34, 0x56, 0x78, 0x90, 0x12]",
687 #[cfg(target_pointer_width = "16")]
690 uint_impl! { usize, u16, 16, 65535, 4, "0xa003", "0x3a", "0x1234", "0x3412", "0x2c48",
691 "[0x34, 0x12]", "[0x12, 0x34]",
692 usize_isize_to_xe_bytes_doc!(), usize_isize_from_xe_bytes_doc!() }
694 #[cfg(target_pointer_width = "32")]
697 uint_impl! { usize, u32, 32, 4294967295, 8, "0x10000b3", "0xb301", "0x12345678",
698 "0x78563412", "0x1e6a2c48", "[0x78, 0x56, 0x34, 0x12]", "[0x12, 0x34, 0x56, 0x78]",
699 usize_isize_to_xe_bytes_doc!(), usize_isize_from_xe_bytes_doc!() }
702 #[cfg(target_pointer_width = "64")]
705 uint_impl! { usize, u64, 64, 18446744073709551615, 12, "0xaa00000000006e1", "0x6e10aa",
706 "0x1234567890123456", "0x5634129078563412", "0x6a2c48091e6a2c48",
707 "[0x56, 0x34, 0x12, 0x90, 0x78, 0x56, 0x34, 0x12]",
708 "[0x12, 0x34, 0x56, 0x78, 0x90, 0x12, 0x34, 0x56]",
709 usize_isize_to_xe_bytes_doc!(), usize_isize_from_xe_bytes_doc!() }
712 /// A classification of floating point numbers.
714 /// This `enum` is used as the return type for [`f32::classify`] and [`f64::classify`]. See
715 /// their documentation for more.
717 /// [`f32::classify`]: ../../std/primitive.f32.html#method.classify
718 /// [`f64::classify`]: ../../std/primitive.f64.html#method.classify
723 /// use std::num::FpCategory;
725 /// let num = 12.4_f32;
726 /// let inf = f32::INFINITY;
728 /// let sub: f32 = 1.1754942e-38;
729 /// let nan = f32::NAN;
731 /// assert_eq!(num.classify(), FpCategory::Normal);
732 /// assert_eq!(inf.classify(), FpCategory::Infinite);
733 /// assert_eq!(zero.classify(), FpCategory::Zero);
734 /// assert_eq!(nan.classify(), FpCategory::Nan);
735 /// assert_eq!(sub.classify(), FpCategory::Subnormal);
737 #[derive(Copy, Clone, PartialEq, Eq, Debug)]
738 #[stable(feature = "rust1", since = "1.0.0")]
739 pub enum FpCategory {
740 /// "Not a Number", often obtained by dividing by zero.
741 #[stable(feature = "rust1", since = "1.0.0")]
744 /// Positive or negative infinity.
745 #[stable(feature = "rust1", since = "1.0.0")]
748 /// Positive or negative zero.
749 #[stable(feature = "rust1", since = "1.0.0")]
752 /// De-normalized floating point representation (less precise than `Normal`).
753 #[stable(feature = "rust1", since = "1.0.0")]
756 /// A regular floating point number.
757 #[stable(feature = "rust1", since = "1.0.0")]
762 trait FromStrRadixHelper: PartialOrd + Copy {
763 fn min_value() -> Self;
764 fn max_value() -> Self;
765 fn from_u32(u: u32) -> Self;
766 fn checked_mul(&self, other: u32) -> Option<Self>;
767 fn checked_sub(&self, other: u32) -> Option<Self>;
768 fn checked_add(&self, other: u32) -> Option<Self>;
771 macro_rules! from_str_radix_int_impl {
773 #[stable(feature = "rust1", since = "1.0.0")]
774 impl FromStr for $t {
775 type Err = ParseIntError;
776 fn from_str(src: &str) -> Result<Self, ParseIntError> {
777 from_str_radix(src, 10)
782 from_str_radix_int_impl! { isize i8 i16 i32 i64 i128 usize u8 u16 u32 u64 u128 }
785 ($($t:ty)*) => ($(impl FromStrRadixHelper for $t {
787 fn min_value() -> Self { Self::MIN }
789 fn max_value() -> Self { Self::MAX }
791 fn from_u32(u: u32) -> Self { u as Self }
793 fn checked_mul(&self, other: u32) -> Option<Self> {
794 Self::checked_mul(*self, other as Self)
797 fn checked_sub(&self, other: u32) -> Option<Self> {
798 Self::checked_sub(*self, other as Self)
801 fn checked_add(&self, other: u32) -> Option<Self> {
802 Self::checked_add(*self, other as Self)
806 doit! { i8 i16 i32 i64 i128 isize u8 u16 u32 u64 u128 usize }
808 fn from_str_radix<T: FromStrRadixHelper>(src: &str, radix: u32) -> Result<T, ParseIntError> {
809 use self::IntErrorKind::*;
810 use self::ParseIntError as PIE;
813 radix >= 2 && radix <= 36,
814 "from_str_radix_int: must lie in the range `[2, 36]` - found {}",
819 return Err(PIE { kind: Empty });
822 let is_signed_ty = T::from_u32(0) > T::min_value();
824 // all valid digits are ascii, so we will just iterate over the utf8 bytes
825 // and cast them to chars. .to_digit() will safely return None for anything
826 // other than a valid ascii digit for the given radix, including the first-byte
827 // of multi-byte sequences
828 let src = src.as_bytes();
830 let (is_positive, digits) = match src[0] {
831 b'+' | b'-' if src[1..].is_empty() => {
832 return Err(PIE { kind: InvalidDigit });
834 b'+' => (true, &src[1..]),
835 b'-' if is_signed_ty => (false, &src[1..]),
839 let mut result = T::from_u32(0);
841 // The number is positive
843 let x = match (c as char).to_digit(radix) {
845 None => return Err(PIE { kind: InvalidDigit }),
847 result = match result.checked_mul(radix) {
848 Some(result) => result,
849 None => return Err(PIE { kind: PosOverflow }),
851 result = match result.checked_add(x) {
852 Some(result) => result,
853 None => return Err(PIE { kind: PosOverflow }),
857 // The number is negative
859 let x = match (c as char).to_digit(radix) {
861 None => return Err(PIE { kind: InvalidDigit }),
863 result = match result.checked_mul(radix) {
864 Some(result) => result,
865 None => return Err(PIE { kind: NegOverflow }),
867 result = match result.checked_sub(x) {
868 Some(result) => result,
869 None => return Err(PIE { kind: NegOverflow }),