Skip to content

Commit

Permalink
Rollup merge of rust-lang#101308 - nerdypepper:feature/is-ascii-octdi…
Browse files Browse the repository at this point in the history
…git, r=joshtriplett

introduce `{char, u8}::is_ascii_octdigit`

This feature adds two new APIs: `char::is_ascii_octdigit` and `u8::is_ascii_octdigit`, under the feature gate `is_ascii_octdigit`. These methods are shorthands for `char::is_digit(self, 8)` and `u8::is_digit(self, 8)`:

```rust
// core::char

impl char {
    pub fn is_ascii_octdigit(self) -> bool;
}

// core::num

impl u8 {
    pub fn is_ascii_octdigit(self) -> bool;
}
```

---

Couple of things I need help understanding:

- `const`ness: have I used the right attribute in this case?
- is there a way to run the tests for `core::char` alone, instead of `./x.py test library/core`?
  • Loading branch information
matthiaskrgr authored Oct 3, 2022
2 parents ef21288 + 591c1f2 commit 7c7a12f
Show file tree
Hide file tree
Showing 5 changed files with 84 additions and 0 deletions.
32 changes: 32 additions & 0 deletions library/core/src/char/methods.rs
Original file line number Diff line number Diff line change
Expand Up @@ -1444,6 +1444,38 @@ impl char {
matches!(*self, '0'..='9')
}

/// Checks if the value is an ASCII octal digit:
/// U+0030 '0' ..= U+0037 '7'.
///
/// # Examples
///
/// ```
/// #![feature(is_ascii_octdigit)]
///
/// let uppercase_a = 'A';
/// let a = 'a';
/// let zero = '0';
/// let seven = '7';
/// let nine = '9';
/// let percent = '%';
/// let lf = '\n';
///
/// assert!(!uppercase_a.is_ascii_octdigit());
/// assert!(!a.is_ascii_octdigit());
/// assert!(zero.is_ascii_octdigit());
/// assert!(seven.is_ascii_octdigit());
/// assert!(!nine.is_ascii_octdigit());
/// assert!(!percent.is_ascii_octdigit());
/// assert!(!lf.is_ascii_octdigit());
/// ```
#[must_use]
#[unstable(feature = "is_ascii_octdigit", issue = "101288")]
#[rustc_const_unstable(feature = "is_ascii_octdigit", issue = "101288")]
#[inline]
pub const fn is_ascii_octdigit(&self) -> bool {
matches!(*self, '0'..='7')
}

/// Checks if the value is an ASCII hexadecimal digit:
///
/// - U+0030 '0' ..= U+0039 '9', or
Expand Down
1 change: 1 addition & 0 deletions library/core/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -164,6 +164,7 @@
#![feature(const_slice_index)]
#![feature(const_is_char_boundary)]
#![feature(const_cstr_methods)]
#![feature(is_ascii_octdigit)]
//
// Language features:
#![feature(abi_unadjusted)]
Expand Down
32 changes: 32 additions & 0 deletions library/core/src/num/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -622,6 +622,38 @@ impl u8 {
matches!(*self, b'0'..=b'9')
}

/// Checks if the value is an ASCII octal digit:
/// U+0030 '0' ..= U+0037 '7'.
///
/// # Examples
///
/// ```
/// #![feature(is_ascii_octdigit)]
///
/// let uppercase_a = b'A';
/// let a = b'a';
/// let zero = b'0';
/// let seven = b'7';
/// let nine = b'9';
/// let percent = b'%';
/// let lf = b'\n';
///
/// assert!(!uppercase_a.is_ascii_octdigit());
/// assert!(!a.is_ascii_octdigit());
/// assert!(zero.is_ascii_octdigit());
/// assert!(seven.is_ascii_octdigit());
/// assert!(!nine.is_ascii_octdigit());
/// assert!(!percent.is_ascii_octdigit());
/// assert!(!lf.is_ascii_octdigit());
/// ```
#[must_use]
#[unstable(feature = "is_ascii_octdigit", issue = "101288")]
#[rustc_const_unstable(feature = "is_ascii_octdigit", issue = "101288")]
#[inline]
pub const fn is_ascii_octdigit(&self) -> bool {
matches!(*self, b'0'..=b'7')
}

/// Checks if the value is an ASCII hexadecimal digit:
///
/// - U+0030 '0' ..= U+0039 '9', or
Expand Down
18 changes: 18 additions & 0 deletions library/core/tests/ascii.rs
Original file line number Diff line number Diff line change
Expand Up @@ -251,6 +251,23 @@ fn test_is_ascii_digit() {
);
}

#[test]
fn test_is_ascii_octdigit() {
assert_all!(is_ascii_octdigit, "", "01234567");
assert_none!(
is_ascii_octdigit,
"abcdefghijklmnopqrstuvwxyz",
"ABCDEFGHIJKLMNOQPRSTUVWXYZ",
"!\"#$%&'()*+,-./:;<=>?@[\\]^_`{|}~",
" \t\n\x0c\r",
"\x00\x01\x02\x03\x04\x05\x06\x07",
"\x08\x09\x0a\x0b\x0c\x0d\x0e\x0f",
"\x10\x11\x12\x13\x14\x15\x16\x17",
"\x18\x19\x1a\x1b\x1c\x1d\x1e\x1f",
"\x7f",
);
}

#[test]
fn test_is_ascii_hexdigit() {
assert_all!(is_ascii_hexdigit, "", "0123456789", "abcdefABCDEF",);
Expand Down Expand Up @@ -454,6 +471,7 @@ fn ascii_ctype_const() {
is_ascii_lowercase => [true, false, false, false, false];
is_ascii_alphanumeric => [true, true, true, false, false];
is_ascii_digit => [false, false, true, false, false];
is_ascii_octdigit => [false, false, false, false, false];
is_ascii_hexdigit => [true, true, true, false, false];
is_ascii_punctuation => [false, false, false, true, false];
is_ascii_graphic => [true, true, true, true, false];
Expand Down
1 change: 1 addition & 0 deletions library/core/tests/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -100,6 +100,7 @@
#![feature(slice_flatten)]
#![feature(provide_any)]
#![feature(utf8_chunks)]
#![feature(is_ascii_octdigit)]
#![deny(unsafe_op_in_unsafe_fn)]

extern crate test;
Expand Down

0 comments on commit 7c7a12f

Please sign in to comment.