Skip to content

Commit

Permalink
Rename to to_ascii_digit_radix, add issue number
Browse files Browse the repository at this point in the history
  • Loading branch information
coolreader18 committed Apr 21, 2021
1 parent da5251f commit c416b71
Show file tree
Hide file tree
Showing 3 changed files with 24 additions and 21 deletions.
19 changes: 11 additions & 8 deletions library/core/src/num/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -714,27 +714,30 @@ impl u8 {
/// Basic usage:
///
/// ```
/// assert_eq!(b'1'.to_digit(10), Some(1));
/// assert_eq!(b'f'.to_digit(16), Some(15));
/// #![feature(to_ascii_digit)]
/// assert_eq!(b'1'.to_ascii_digit_radix(10), Some(1));
/// assert_eq!(b'f'.to_ascii_digit_radix(16), Some(15));
/// ```
///
/// Passing a non-digit results in failure:
///
/// ```
/// assert_eq!(b'f'.to_digit(10), None);
/// assert_eq!(b'z'.to_digit(16), None);
/// #![feature(to_ascii_digit)]
/// assert_eq!(b'f'.to_ascii_digit_radix(10), None);
/// assert_eq!(b'z'.to_ascii_digit_radix(16), None);
/// ```
///
/// Passing a large radix, causing a panic:
///
/// ```should_panic
/// #![feature(to_ascii_digit)]
/// // this panics
/// b'1'.to_digit(37);
/// b'1'.to_ascii_digit_radix(37);
/// ```
#[unstable(feature = "ascii_to_digit", issue = "none")]
#[unstable(feature = "to_ascii_digit", issue = "83447")]
#[inline]
pub fn to_digit(&self, radix: u32) -> Option<u32> {
assert!(radix <= 36, "to_digit: radix is too high (maximum 36)");
pub fn to_ascii_digit_radix(&self, radix: u32) -> Option<u32> {
assert!(radix <= 36, "to_ascii_digit_radix: radix is too high (maximum 36)");
// the code is split up here to improve execution speed for cases where
// the `radix` is constant and 10 or smaller
let val = if intrinsics::likely(radix <= 10) {
Expand Down
24 changes: 12 additions & 12 deletions library/core/tests/ascii.rs
Original file line number Diff line number Diff line change
Expand Up @@ -347,18 +347,18 @@ fn test_is_ascii_control() {
#[cfg(not(bootstrap))]
#[test]
fn test_to_digit() {
assert_eq!(b'0'.to_digit(10), Some(0));
assert_eq!(b'1'.to_digit(2), Some(1));
assert_eq!(b'2'.to_digit(3), Some(2));
assert_eq!(b'9'.to_digit(10), Some(9));
assert_eq!(b'a'.to_digit(16), Some(10));
assert_eq!(b'A'.to_digit(16), Some(10));
assert_eq!(b'b'.to_digit(16), Some(11));
assert_eq!(b'B'.to_digit(16), Some(11));
assert_eq!(b'z'.to_digit(36), Some(35));
assert_eq!(b'Z'.to_digit(36), Some(35));
assert_eq!(b' '.to_digit(10), None);
assert_eq!(b'$'.to_digit(36), None);
assert_eq!(b'0'.to_ascii_digit_radix(10), Some(0));
assert_eq!(b'1'.to_ascii_digit_radix(2), Some(1));
assert_eq!(b'2'.to_ascii_digit_radix(3), Some(2));
assert_eq!(b'9'.to_ascii_digit_radix(10), Some(9));
assert_eq!(b'a'.to_ascii_digit_radix(16), Some(10));
assert_eq!(b'A'.to_ascii_digit_radix(16), Some(10));
assert_eq!(b'b'.to_ascii_digit_radix(16), Some(11));
assert_eq!(b'B'.to_ascii_digit_radix(16), Some(11));
assert_eq!(b'z'.to_ascii_digit_radix(36), Some(35));
assert_eq!(b'Z'.to_ascii_digit_radix(36), Some(35));
assert_eq!(b' '.to_ascii_digit_radix(10), None);
assert_eq!(b'$'.to_ascii_digit_radix(36), None);
}

// `is_ascii` does a good amount of pointer manipulation and has
Expand Down
2 changes: 1 addition & 1 deletion library/core/tests/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,6 @@
#![feature(array_methods)]
#![feature(array_map)]
#![feature(array_windows)]
#![cfg_attr(not(bootstrap), feature(ascii_to_digit))]
#![feature(bool_to_option)]
#![feature(bound_cloned)]
#![feature(box_syntax)]
Expand Down Expand Up @@ -45,6 +44,7 @@
#![feature(step_trait_ext)]
#![feature(str_internals)]
#![feature(test)]
#![cfg_attr(not(bootstrap), feature(to_ascii_digit))]
#![feature(trusted_len)]
#![feature(try_trait)]
#![feature(slice_internals)]
Expand Down

0 comments on commit c416b71

Please sign in to comment.