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:
34 mod int_macros
; // import int_impl!
36 mod uint_macros
; // import uint_impl!
42 #[stable(feature = "rust1", since = "1.0.0")]
43 pub use wrapping
::Wrapping
;
45 #[stable(feature = "rust1", since = "1.0.0")]
46 pub use dec2flt
::ParseFloatError
;
48 #[stable(feature = "rust1", since = "1.0.0")]
49 pub use error
::ParseIntError
;
51 #[stable(feature = "nonzero", since = "1.28.0")]
52 pub use nonzero
::{NonZeroU128, NonZeroU16, NonZeroU32, NonZeroU64, NonZeroU8, NonZeroUsize}
;
54 #[stable(feature = "signed_nonzero", since = "1.34.0")]
55 pub use nonzero
::{NonZeroI128, NonZeroI16, NonZeroI32, NonZeroI64, NonZeroI8, NonZeroIsize}
;
57 #[stable(feature = "try_from", since = "1.34.0")]
58 pub use error
::TryFromIntError
;
61 feature
= "int_error_matching",
62 reason
= "it can be useful to match errors when making error messages \
66 pub use error
::IntErrorKind
;
68 macro_rules
! usize_isize_to_xe_bytes_doc
{
72 **Note**: This function returns an array of length 2, 4 or 8 bytes
73 depending on the target pointer size.
79 macro_rules
! usize_isize_from_xe_bytes_doc
{
83 **Note**: This function takes an array of length 2, 4 or 8 bytes
84 depending on the target pointer size.
92 int_impl
! { i8, i8, u8, 8, -128, 127, 2, "-0x7e", "0xa", "0x12", "0x12", "0x48",
93 "[0x12]", "[0x12]", "", "" }
98 int_impl
! { i16, i16, u16, 16, -32768, 32767, 4, "-0x5ffd", "0x3a", "0x1234", "0x3412",
99 "0x2c48", "[0x34, 0x12]", "[0x12, 0x34]", "", "" }
104 int_impl
! { i32, i32, u32, 32, -2147483648, 2147483647, 8, "0x10000b3", "0xb301",
105 "0x12345678", "0x78563412", "0x1e6a2c48", "[0x78, 0x56, 0x34, 0x12]",
106 "[0x12, 0x34, 0x56, 0x78]", "", "" }
111 int_impl
! { i64, i64, u64, 64, -9223372036854775808, 9223372036854775807, 12,
112 "0xaa00000000006e1", "0x6e10aa", "0x1234567890123456", "0x5634129078563412",
113 "0x6a2c48091e6a2c48", "[0x56, 0x34, 0x12, 0x90, 0x78, 0x56, 0x34, 0x12]",
114 "[0x12, 0x34, 0x56, 0x78, 0x90, 0x12, 0x34, 0x56]", "", "" }
119 int_impl
! { i128
, i128
, u128
, 128, -170141183460469231731687303715884105728,
120 170141183460469231731687303715884105727, 16,
121 "0x13f40000000000000000000000004f76", "0x4f7613f4", "0x12345678901234567890123456789012",
122 "0x12907856341290785634129078563412", "0x48091e6a2c48091e6a2c48091e6a2c48",
123 "[0x12, 0x90, 0x78, 0x56, 0x34, 0x12, 0x90, 0x78, \
124 0x56, 0x34, 0x12, 0x90, 0x78, 0x56, 0x34, 0x12]",
125 "[0x12, 0x34, 0x56, 0x78, 0x90, 0x12, 0x34, 0x56, \
126 0x78, 0x90, 0x12, 0x34, 0x56, 0x78, 0x90, 0x12]", "", "" }
129 #[cfg(target_pointer_width = "16")]
132 int_impl
! { isize, i16, usize, 16, -32768, 32767, 4, "-0x5ffd", "0x3a", "0x1234",
133 "0x3412", "0x2c48", "[0x34, 0x12]", "[0x12, 0x34]",
134 usize_isize_to_xe_bytes_doc
!(), usize_isize_from_xe_bytes_doc
!() }
137 #[cfg(target_pointer_width = "32")]
140 int_impl
! { isize, i32, usize, 32, -2147483648, 2147483647, 8, "0x10000b3", "0xb301",
141 "0x12345678", "0x78563412", "0x1e6a2c48", "[0x78, 0x56, 0x34, 0x12]",
142 "[0x12, 0x34, 0x56, 0x78]",
143 usize_isize_to_xe_bytes_doc
!(), usize_isize_from_xe_bytes_doc
!() }
146 #[cfg(target_pointer_width = "64")]
149 int_impl
! { isize, i64, usize, 64, -9223372036854775808, 9223372036854775807,
150 12, "0xaa00000000006e1", "0x6e10aa", "0x1234567890123456", "0x5634129078563412",
151 "0x6a2c48091e6a2c48", "[0x56, 0x34, 0x12, 0x90, 0x78, 0x56, 0x34, 0x12]",
152 "[0x12, 0x34, 0x56, 0x78, 0x90, 0x12, 0x34, 0x56]",
153 usize_isize_to_xe_bytes_doc
!(), usize_isize_from_xe_bytes_doc
!() }
156 /// If 6th bit set ascii is upper case.
157 const ASCII_CASE_MASK
: u8 = 0b0010_0000;
161 uint_impl
! { u8, u8, 8, 255, 2, "0x82", "0xa", "0x12", "0x12", "0x48", "[0x12]",
164 /// Checks if the value is within the ASCII range.
169 /// let ascii = 97u8;
170 /// let non_ascii = 150u8;
172 /// assert!(ascii.is_ascii());
173 /// assert!(!non_ascii.is_ascii());
175 #[stable(feature = "ascii_methods_on_intrinsics", since = "1.23.0")]
176 #[rustc_const_stable(feature = "const_ascii_methods_on_intrinsics", since = "1.43.0")]
178 pub const fn is_ascii(&self) -> bool
{
182 /// Makes a copy of the value in its ASCII upper case equivalent.
184 /// ASCII letters 'a' to 'z' are mapped to 'A' to 'Z',
185 /// but non-ASCII letters are unchanged.
187 /// To uppercase the value in-place, use [`make_ascii_uppercase`].
192 /// let lowercase_a = 97u8;
194 /// assert_eq!(65, lowercase_a.to_ascii_uppercase());
197 /// [`make_ascii_uppercase`]: Self::make_ascii_uppercase
198 #[stable(feature = "ascii_methods_on_intrinsics", since = "1.23.0")]
199 #[rustc_const_stable(feature = "const_ascii_methods_on_intrinsics", since = "1.52.0")]
201 pub const fn to_ascii_uppercase(&self) -> u8 {
202 // Unset the fifth bit if this is a lowercase letter
203 *self & !((self.is_ascii_lowercase() as u8) * ASCII_CASE_MASK
)
206 /// Makes a copy of the value in its ASCII lower case equivalent.
208 /// ASCII letters 'A' to 'Z' are mapped to 'a' to 'z',
209 /// but non-ASCII letters are unchanged.
211 /// To lowercase the value in-place, use [`make_ascii_lowercase`].
216 /// let uppercase_a = 65u8;
218 /// assert_eq!(97, uppercase_a.to_ascii_lowercase());
221 /// [`make_ascii_lowercase`]: Self::make_ascii_lowercase
222 #[stable(feature = "ascii_methods_on_intrinsics", since = "1.23.0")]
223 #[rustc_const_stable(feature = "const_ascii_methods_on_intrinsics", since = "1.52.0")]
225 pub const fn to_ascii_lowercase(&self) -> u8 {
226 // Set the fifth bit if this is an uppercase letter
227 *self | (self.is_ascii_uppercase() as u8 * ASCII_CASE_MASK
)
230 /// Assumes self is ascii
232 pub(crate) const fn ascii_change_case_unchecked(&self) -> u8 {
233 *self ^ ASCII_CASE_MASK
236 /// Checks that two values are an ASCII case-insensitive match.
238 /// This is equivalent to `to_ascii_lowercase(a) == to_ascii_lowercase(b)`.
243 /// let lowercase_a = 97u8;
244 /// let uppercase_a = 65u8;
246 /// assert!(lowercase_a.eq_ignore_ascii_case(&uppercase_a));
248 #[stable(feature = "ascii_methods_on_intrinsics", since = "1.23.0")]
249 #[rustc_const_stable(feature = "const_ascii_methods_on_intrinsics", since = "1.52.0")]
251 pub const fn eq_ignore_ascii_case(&self, other
: &u8) -> bool
{
252 self.to_ascii_lowercase() == other
.to_ascii_lowercase()
255 /// Converts this value to its ASCII upper case equivalent in-place.
257 /// ASCII letters 'a' to 'z' are mapped to 'A' to 'Z',
258 /// but non-ASCII letters are unchanged.
260 /// To return a new uppercased value without modifying the existing one, use
261 /// [`to_ascii_uppercase`].
266 /// let mut byte = b'a';
268 /// byte.make_ascii_uppercase();
270 /// assert_eq!(b'A', byte);
273 /// [`to_ascii_uppercase`]: Self::to_ascii_uppercase
274 #[stable(feature = "ascii_methods_on_intrinsics", since = "1.23.0")]
276 pub fn make_ascii_uppercase(&mut self) {
277 *self = self.to_ascii_uppercase();
280 /// Converts this value to its ASCII lower case equivalent in-place.
282 /// ASCII letters 'A' to 'Z' are mapped to 'a' to 'z',
283 /// but non-ASCII letters are unchanged.
285 /// To return a new lowercased value without modifying the existing one, use
286 /// [`to_ascii_lowercase`].
291 /// let mut byte = b'A';
293 /// byte.make_ascii_lowercase();
295 /// assert_eq!(b'a', byte);
298 /// [`to_ascii_lowercase`]: Self::to_ascii_lowercase
299 #[stable(feature = "ascii_methods_on_intrinsics", since = "1.23.0")]
301 pub fn make_ascii_lowercase(&mut self) {
302 *self = self.to_ascii_lowercase();
305 /// Checks if the value is an ASCII alphabetic character:
307 /// - U+0041 'A' ..= U+005A 'Z', or
308 /// - U+0061 'a' ..= U+007A 'z'.
313 /// let uppercase_a = b'A';
314 /// let uppercase_g = b'G';
318 /// let percent = b'%';
319 /// let space = b' ';
321 /// let esc = 0x1b_u8;
323 /// assert!(uppercase_a.is_ascii_alphabetic());
324 /// assert!(uppercase_g.is_ascii_alphabetic());
325 /// assert!(a.is_ascii_alphabetic());
326 /// assert!(g.is_ascii_alphabetic());
327 /// assert!(!zero.is_ascii_alphabetic());
328 /// assert!(!percent.is_ascii_alphabetic());
329 /// assert!(!space.is_ascii_alphabetic());
330 /// assert!(!lf.is_ascii_alphabetic());
331 /// assert!(!esc.is_ascii_alphabetic());
333 #[stable(feature = "ascii_ctype_on_intrinsics", since = "1.24.0")]
334 #[rustc_const_stable(feature = "const_ascii_ctype_on_intrinsics", since = "1.47.0")]
336 pub const fn is_ascii_alphabetic(&self) -> bool
{
337 matches
!(*self, b'A'
..=b'Z'
| b'a'
..=b'z'
)
340 /// Checks if the value is an ASCII uppercase character:
341 /// U+0041 'A' ..= U+005A 'Z'.
346 /// let uppercase_a = b'A';
347 /// let uppercase_g = b'G';
351 /// let percent = b'%';
352 /// let space = b' ';
354 /// let esc = 0x1b_u8;
356 /// assert!(uppercase_a.is_ascii_uppercase());
357 /// assert!(uppercase_g.is_ascii_uppercase());
358 /// assert!(!a.is_ascii_uppercase());
359 /// assert!(!g.is_ascii_uppercase());
360 /// assert!(!zero.is_ascii_uppercase());
361 /// assert!(!percent.is_ascii_uppercase());
362 /// assert!(!space.is_ascii_uppercase());
363 /// assert!(!lf.is_ascii_uppercase());
364 /// assert!(!esc.is_ascii_uppercase());
366 #[stable(feature = "ascii_ctype_on_intrinsics", since = "1.24.0")]
367 #[rustc_const_stable(feature = "const_ascii_ctype_on_intrinsics", since = "1.47.0")]
369 pub const fn is_ascii_uppercase(&self) -> bool
{
370 matches
!(*self, b'A'
..=b'Z'
)
373 /// Checks if the value is an ASCII lowercase character:
374 /// U+0061 'a' ..= U+007A 'z'.
379 /// let uppercase_a = b'A';
380 /// let uppercase_g = b'G';
384 /// let percent = b'%';
385 /// let space = b' ';
387 /// let esc = 0x1b_u8;
389 /// assert!(!uppercase_a.is_ascii_lowercase());
390 /// assert!(!uppercase_g.is_ascii_lowercase());
391 /// assert!(a.is_ascii_lowercase());
392 /// assert!(g.is_ascii_lowercase());
393 /// assert!(!zero.is_ascii_lowercase());
394 /// assert!(!percent.is_ascii_lowercase());
395 /// assert!(!space.is_ascii_lowercase());
396 /// assert!(!lf.is_ascii_lowercase());
397 /// assert!(!esc.is_ascii_lowercase());
399 #[stable(feature = "ascii_ctype_on_intrinsics", since = "1.24.0")]
400 #[rustc_const_stable(feature = "const_ascii_ctype_on_intrinsics", since = "1.47.0")]
402 pub const fn is_ascii_lowercase(&self) -> bool
{
403 matches
!(*self, b'a'
..=b'z'
)
406 /// Checks if the value is an ASCII alphanumeric character:
408 /// - U+0041 'A' ..= U+005A 'Z', or
409 /// - U+0061 'a' ..= U+007A 'z', or
410 /// - U+0030 '0' ..= U+0039 '9'.
415 /// let uppercase_a = b'A';
416 /// let uppercase_g = b'G';
420 /// let percent = b'%';
421 /// let space = b' ';
423 /// let esc = 0x1b_u8;
425 /// assert!(uppercase_a.is_ascii_alphanumeric());
426 /// assert!(uppercase_g.is_ascii_alphanumeric());
427 /// assert!(a.is_ascii_alphanumeric());
428 /// assert!(g.is_ascii_alphanumeric());
429 /// assert!(zero.is_ascii_alphanumeric());
430 /// assert!(!percent.is_ascii_alphanumeric());
431 /// assert!(!space.is_ascii_alphanumeric());
432 /// assert!(!lf.is_ascii_alphanumeric());
433 /// assert!(!esc.is_ascii_alphanumeric());
435 #[stable(feature = "ascii_ctype_on_intrinsics", since = "1.24.0")]
436 #[rustc_const_stable(feature = "const_ascii_ctype_on_intrinsics", since = "1.47.0")]
438 pub const fn is_ascii_alphanumeric(&self) -> bool
{
439 matches
!(*self, b'
0'
..=b'
9'
| b'A'
..=b'Z'
| b'a'
..=b'z'
)
442 /// Checks if the value is an ASCII decimal digit:
443 /// U+0030 '0' ..= U+0039 '9'.
448 /// let uppercase_a = b'A';
449 /// let uppercase_g = b'G';
453 /// let percent = b'%';
454 /// let space = b' ';
456 /// let esc = 0x1b_u8;
458 /// assert!(!uppercase_a.is_ascii_digit());
459 /// assert!(!uppercase_g.is_ascii_digit());
460 /// assert!(!a.is_ascii_digit());
461 /// assert!(!g.is_ascii_digit());
462 /// assert!(zero.is_ascii_digit());
463 /// assert!(!percent.is_ascii_digit());
464 /// assert!(!space.is_ascii_digit());
465 /// assert!(!lf.is_ascii_digit());
466 /// assert!(!esc.is_ascii_digit());
468 #[stable(feature = "ascii_ctype_on_intrinsics", since = "1.24.0")]
469 #[rustc_const_stable(feature = "const_ascii_ctype_on_intrinsics", since = "1.47.0")]
471 pub const fn is_ascii_digit(&self) -> bool
{
472 matches
!(*self, b'
0'
..=b'
9'
)
475 /// Checks if the value is an ASCII hexadecimal digit:
477 /// - U+0030 '0' ..= U+0039 '9', or
478 /// - U+0041 'A' ..= U+0046 'F', or
479 /// - U+0061 'a' ..= U+0066 'f'.
484 /// let uppercase_a = b'A';
485 /// let uppercase_g = b'G';
489 /// let percent = b'%';
490 /// let space = b' ';
492 /// let esc = 0x1b_u8;
494 /// assert!(uppercase_a.is_ascii_hexdigit());
495 /// assert!(!uppercase_g.is_ascii_hexdigit());
496 /// assert!(a.is_ascii_hexdigit());
497 /// assert!(!g.is_ascii_hexdigit());
498 /// assert!(zero.is_ascii_hexdigit());
499 /// assert!(!percent.is_ascii_hexdigit());
500 /// assert!(!space.is_ascii_hexdigit());
501 /// assert!(!lf.is_ascii_hexdigit());
502 /// assert!(!esc.is_ascii_hexdigit());
504 #[stable(feature = "ascii_ctype_on_intrinsics", since = "1.24.0")]
505 #[rustc_const_stable(feature = "const_ascii_ctype_on_intrinsics", since = "1.47.0")]
507 pub const fn is_ascii_hexdigit(&self) -> bool
{
508 matches
!(*self, b'
0'
..=b'
9'
| b'A'
..=b'F'
| b'a'
..=b'f'
)
511 /// Checks if the value is an ASCII punctuation character:
513 /// - U+0021 ..= U+002F `! " # $ % & ' ( ) * + , - . /`, or
514 /// - U+003A ..= U+0040 `: ; < = > ? @`, or
515 /// - U+005B ..= U+0060 ``[ \ ] ^ _ ` ``, or
516 /// - U+007B ..= U+007E `{ | } ~`
521 /// let uppercase_a = b'A';
522 /// let uppercase_g = b'G';
526 /// let percent = b'%';
527 /// let space = b' ';
529 /// let esc = 0x1b_u8;
531 /// assert!(!uppercase_a.is_ascii_punctuation());
532 /// assert!(!uppercase_g.is_ascii_punctuation());
533 /// assert!(!a.is_ascii_punctuation());
534 /// assert!(!g.is_ascii_punctuation());
535 /// assert!(!zero.is_ascii_punctuation());
536 /// assert!(percent.is_ascii_punctuation());
537 /// assert!(!space.is_ascii_punctuation());
538 /// assert!(!lf.is_ascii_punctuation());
539 /// assert!(!esc.is_ascii_punctuation());
541 #[stable(feature = "ascii_ctype_on_intrinsics", since = "1.24.0")]
542 #[rustc_const_stable(feature = "const_ascii_ctype_on_intrinsics", since = "1.47.0")]
544 pub const fn is_ascii_punctuation(&self) -> bool
{
545 matches
!(*self, b'
!'
..=b'
/'
| b'
:'
..=b'@'
| b'
['
..=b'`'
| b'
{'
..=b'
~'
)
548 /// Checks if the value is an ASCII graphic character:
549 /// U+0021 '!' ..= U+007E '~'.
554 /// let uppercase_a = b'A';
555 /// let uppercase_g = b'G';
559 /// let percent = b'%';
560 /// let space = b' ';
562 /// let esc = 0x1b_u8;
564 /// assert!(uppercase_a.is_ascii_graphic());
565 /// assert!(uppercase_g.is_ascii_graphic());
566 /// assert!(a.is_ascii_graphic());
567 /// assert!(g.is_ascii_graphic());
568 /// assert!(zero.is_ascii_graphic());
569 /// assert!(percent.is_ascii_graphic());
570 /// assert!(!space.is_ascii_graphic());
571 /// assert!(!lf.is_ascii_graphic());
572 /// assert!(!esc.is_ascii_graphic());
574 #[stable(feature = "ascii_ctype_on_intrinsics", since = "1.24.0")]
575 #[rustc_const_stable(feature = "const_ascii_ctype_on_intrinsics", since = "1.47.0")]
577 pub const fn is_ascii_graphic(&self) -> bool
{
578 matches
!(*self, b'
!'
..=b'
~'
)
581 /// Checks if the value is an ASCII whitespace character:
582 /// U+0020 SPACE, U+0009 HORIZONTAL TAB, U+000A LINE FEED,
583 /// U+000C FORM FEED, or U+000D CARRIAGE RETURN.
585 /// Rust uses the WhatWG Infra Standard's [definition of ASCII
586 /// whitespace][infra-aw]. There are several other definitions in
587 /// wide use. For instance, [the POSIX locale][pct] includes
588 /// U+000B VERTICAL TAB as well as all the above characters,
589 /// but—from the very same specification—[the default rule for
590 /// "field splitting" in the Bourne shell][bfs] considers *only*
591 /// SPACE, HORIZONTAL TAB, and LINE FEED as whitespace.
593 /// If you are writing a program that will process an existing
594 /// file format, check what that format's definition of whitespace is
595 /// before using this function.
597 /// [infra-aw]: https://infra.spec.whatwg.org/#ascii-whitespace
598 /// [pct]: http://pubs.opengroup.org/onlinepubs/9699919799/basedefs/V1_chap07.html#tag_07_03_01
599 /// [bfs]: http://pubs.opengroup.org/onlinepubs/9699919799/utilities/V3_chap02.html#tag_18_06_05
604 /// let uppercase_a = b'A';
605 /// let uppercase_g = b'G';
609 /// let percent = b'%';
610 /// let space = b' ';
612 /// let esc = 0x1b_u8;
614 /// assert!(!uppercase_a.is_ascii_whitespace());
615 /// assert!(!uppercase_g.is_ascii_whitespace());
616 /// assert!(!a.is_ascii_whitespace());
617 /// assert!(!g.is_ascii_whitespace());
618 /// assert!(!zero.is_ascii_whitespace());
619 /// assert!(!percent.is_ascii_whitespace());
620 /// assert!(space.is_ascii_whitespace());
621 /// assert!(lf.is_ascii_whitespace());
622 /// assert!(!esc.is_ascii_whitespace());
624 #[stable(feature = "ascii_ctype_on_intrinsics", since = "1.24.0")]
625 #[rustc_const_stable(feature = "const_ascii_ctype_on_intrinsics", since = "1.47.0")]
627 pub const fn is_ascii_whitespace(&self) -> bool
{
628 matches
!(*self, b'
\t'
| b'
\n'
| b'
\x0C'
| b'
\r'
| b' '
)
631 /// Checks if the value is an ASCII control character:
632 /// U+0000 NUL ..= U+001F UNIT SEPARATOR, or U+007F DELETE.
633 /// Note that most ASCII whitespace characters are control
634 /// characters, but SPACE is not.
639 /// let uppercase_a = b'A';
640 /// let uppercase_g = b'G';
644 /// let percent = b'%';
645 /// let space = b' ';
647 /// let esc = 0x1b_u8;
649 /// assert!(!uppercase_a.is_ascii_control());
650 /// assert!(!uppercase_g.is_ascii_control());
651 /// assert!(!a.is_ascii_control());
652 /// assert!(!g.is_ascii_control());
653 /// assert!(!zero.is_ascii_control());
654 /// assert!(!percent.is_ascii_control());
655 /// assert!(!space.is_ascii_control());
656 /// assert!(lf.is_ascii_control());
657 /// assert!(esc.is_ascii_control());
659 #[stable(feature = "ascii_ctype_on_intrinsics", since = "1.24.0")]
660 #[rustc_const_stable(feature = "const_ascii_ctype_on_intrinsics", since = "1.47.0")]
662 pub const fn is_ascii_control(&self) -> bool
{
663 matches
!(*self, b'
\0'
..=b'
\x1F'
| b'
\x7F'
)
666 /// Returns an iterator that produces an escaped version of a `u8`,
667 /// treating it as an ASCII character.
669 /// The behavior is identical to [`ascii::escape_default`].
674 /// #![feature(inherent_ascii_escape)]
676 /// assert_eq!("0", b'0'.escape_ascii().to_string());
677 /// assert_eq!("\\t", b'\t'.escape_ascii().to_string());
678 /// assert_eq!("\\r", b'\r'.escape_ascii().to_string());
679 /// assert_eq!("\\n", b'\n'.escape_ascii().to_string());
680 /// assert_eq!("\\'", b'\''.escape_ascii().to_string());
681 /// assert_eq!("\\\"", b'"'.escape_ascii().to_string());
682 /// assert_eq!("\\\\", b'\\'.escape_ascii().to_string());
683 /// assert_eq!("\\x9d", b'\x9d'.escape_ascii().to_string());
685 #[unstable(feature = "inherent_ascii_escape", issue = "77174")]
687 pub fn escape_ascii(&self) -> ascii
::EscapeDefault
{
688 ascii
::escape_default(*self)
694 uint_impl
! { u16, u16, 16, 65535, 4, "0xa003", "0x3a", "0x1234", "0x3412", "0x2c48",
695 "[0x34, 0x12]", "[0x12, 0x34]", "", "" }
700 uint_impl
! { u32, u32, 32, 4294967295, 8, "0x10000b3", "0xb301", "0x12345678",
701 "0x78563412", "0x1e6a2c48", "[0x78, 0x56, 0x34, 0x12]", "[0x12, 0x34, 0x56, 0x78]", "", "" }
706 uint_impl
! { u64, u64, 64, 18446744073709551615, 12, "0xaa00000000006e1", "0x6e10aa",
707 "0x1234567890123456", "0x5634129078563412", "0x6a2c48091e6a2c48",
708 "[0x56, 0x34, 0x12, 0x90, 0x78, 0x56, 0x34, 0x12]",
709 "[0x12, 0x34, 0x56, 0x78, 0x90, 0x12, 0x34, 0x56]",
715 uint_impl
! { u128
, u128
, 128, 340282366920938463463374607431768211455, 16,
716 "0x13f40000000000000000000000004f76", "0x4f7613f4", "0x12345678901234567890123456789012",
717 "0x12907856341290785634129078563412", "0x48091e6a2c48091e6a2c48091e6a2c48",
718 "[0x12, 0x90, 0x78, 0x56, 0x34, 0x12, 0x90, 0x78, \
719 0x56, 0x34, 0x12, 0x90, 0x78, 0x56, 0x34, 0x12]",
720 "[0x12, 0x34, 0x56, 0x78, 0x90, 0x12, 0x34, 0x56, \
721 0x78, 0x90, 0x12, 0x34, 0x56, 0x78, 0x90, 0x12]",
725 #[cfg(target_pointer_width = "16")]
728 uint_impl
! { usize, u16, 16, 65535, 4, "0xa003", "0x3a", "0x1234", "0x3412", "0x2c48",
729 "[0x34, 0x12]", "[0x12, 0x34]",
730 usize_isize_to_xe_bytes_doc
!(), usize_isize_from_xe_bytes_doc
!() }
732 #[cfg(target_pointer_width = "32")]
735 uint_impl
! { usize, u32, 32, 4294967295, 8, "0x10000b3", "0xb301", "0x12345678",
736 "0x78563412", "0x1e6a2c48", "[0x78, 0x56, 0x34, 0x12]", "[0x12, 0x34, 0x56, 0x78]",
737 usize_isize_to_xe_bytes_doc
!(), usize_isize_from_xe_bytes_doc
!() }
740 #[cfg(target_pointer_width = "64")]
743 uint_impl
! { usize, u64, 64, 18446744073709551615, 12, "0xaa00000000006e1", "0x6e10aa",
744 "0x1234567890123456", "0x5634129078563412", "0x6a2c48091e6a2c48",
745 "[0x56, 0x34, 0x12, 0x90, 0x78, 0x56, 0x34, 0x12]",
746 "[0x12, 0x34, 0x56, 0x78, 0x90, 0x12, 0x34, 0x56]",
747 usize_isize_to_xe_bytes_doc
!(), usize_isize_from_xe_bytes_doc
!() }
750 /// A classification of floating point numbers.
752 /// This `enum` is used as the return type for [`f32::classify`] and [`f64::classify`]. See
753 /// their documentation for more.
758 /// use std::num::FpCategory;
760 /// let num = 12.4_f32;
761 /// let inf = f32::INFINITY;
763 /// let sub: f32 = 1.1754942e-38;
764 /// let nan = f32::NAN;
766 /// assert_eq!(num.classify(), FpCategory::Normal);
767 /// assert_eq!(inf.classify(), FpCategory::Infinite);
768 /// assert_eq!(zero.classify(), FpCategory::Zero);
769 /// assert_eq!(nan.classify(), FpCategory::Nan);
770 /// assert_eq!(sub.classify(), FpCategory::Subnormal);
772 #[derive(Copy, Clone, PartialEq, Eq, Debug)]
773 #[stable(feature = "rust1", since = "1.0.0")]
774 pub enum FpCategory
{
775 /// "Not a Number", often obtained by dividing by zero.
776 #[stable(feature = "rust1", since = "1.0.0")]
779 /// Positive or negative infinity.
780 #[stable(feature = "rust1", since = "1.0.0")]
783 /// Positive or negative zero.
784 #[stable(feature = "rust1", since = "1.0.0")]
787 /// De-normalized floating point representation (less precise than `Normal`).
788 #[stable(feature = "rust1", since = "1.0.0")]
791 /// A regular floating point number.
792 #[stable(feature = "rust1", since = "1.0.0")]
797 trait FromStrRadixHelper
: PartialOrd
+ Copy
{
798 fn min_value() -> Self;
799 fn max_value() -> Self;
800 fn from_u32(u
: u32) -> Self;
801 fn checked_mul(&self, other
: u32) -> Option
<Self>;
802 fn checked_sub(&self, other
: u32) -> Option
<Self>;
803 fn checked_add(&self, other
: u32) -> Option
<Self>;
806 macro_rules
! from_str_radix_int_impl
{
808 #[stable(feature = "rust1", since = "1.0.0")]
809 impl FromStr
for $t
{
810 type Err
= ParseIntError
;
811 fn from_str(src
: &str) -> Result
<Self, ParseIntError
> {
812 from_str_radix(src
, 10)
817 from_str_radix_int_impl
! { isize i8 i16 i32 i64 i128 usize u8 u16 u32 u64 u128 }
820 ($
($t
:ty
)*) => ($
(impl FromStrRadixHelper
for $t
{
822 fn min_value() -> Self { Self::MIN }
824 fn max_value() -> Self { Self::MAX }
826 fn from_u32(u
: u32) -> Self { u as Self }
828 fn checked_mul(&self, other
: u32) -> Option
<Self> {
829 Self::checked_mul(*self, other
as Self)
832 fn checked_sub(&self, other
: u32) -> Option
<Self> {
833 Self::checked_sub(*self, other
as Self)
836 fn checked_add(&self, other
: u32) -> Option
<Self> {
837 Self::checked_add(*self, other
as Self)
841 doit
! { i8 i16 i32 i64 i128 isize u8 u16 u32 u64 u128 usize }
843 fn from_str_radix
<T
: FromStrRadixHelper
>(src
: &str, radix
: u32) -> Result
<T
, ParseIntError
> {
844 use self::IntErrorKind
::*;
845 use self::ParseIntError
as PIE
;
848 radix
>= 2 && radix
<= 36,
849 "from_str_radix_int: must lie in the range `[2, 36]` - found {}",
854 return Err(PIE { kind: Empty }
);
857 let is_signed_ty
= T
::from_u32(0) > T
::min_value();
859 // all valid digits are ascii, so we will just iterate over the utf8 bytes
860 // and cast them to chars. .to_digit() will safely return None for anything
861 // other than a valid ascii digit for the given radix, including the first-byte
862 // of multi-byte sequences
863 let src
= src
.as_bytes();
865 let (is_positive
, digits
) = match src
[0] {
866 b'
+'
| b'
-'
if src
[1..].is_empty() => {
867 return Err(PIE { kind: InvalidDigit }
);
869 b'
+'
=> (true, &src
[1..]),
870 b'
-'
if is_signed_ty
=> (false, &src
[1..]),
874 let mut result
= T
::from_u32(0);
876 // The number is positive
878 let x
= match (c
as char).to_digit(radix
) {
880 None
=> return Err(PIE { kind: InvalidDigit }
),
882 result
= match result
.checked_mul(radix
) {
883 Some(result
) => result
,
884 None
=> return Err(PIE { kind: PosOverflow }
),
886 result
= match result
.checked_add(x
) {
887 Some(result
) => result
,
888 None
=> return Err(PIE { kind: PosOverflow }
),
892 // The number is negative
894 let x
= match (c
as char).to_digit(radix
) {
896 None
=> return Err(PIE { kind: InvalidDigit }
),
898 result
= match result
.checked_mul(radix
) {
899 Some(result
) => result
,
900 None
=> return Err(PIE { kind: NegOverflow }
),
902 result
= match result
.checked_sub(x
) {
903 Some(result
) => result
,
904 None
=> return Err(PIE { kind: NegOverflow }
),