Skip to content

Add ASCII-related methods from u8 and MIN/MAX to core::ascii::Char #630

@ChaiTRex

Description

@ChaiTRex

Proposal

Problem statement

Add useful things to core::ascii::Char:

  • Add MIN and MAX constants.
  • Add ASCII-related methods from u8.

Motivating examples or use cases

It would be nice if MIN and MAX existed for consistency with other ordered types and for things like iterating over MIN..=MAX. It would also be nice if ascii::Char was at feature parity with u8-as-ASCII so that the relevant methods can be used in cases where they are needed.

Solution sketch

pub const MIN: Self = Self::Null;
pub const MAX: Self = Self::Delete;

pub const fn to_ascii_uppercase(&self) -> Self
pub const fn to_ascii_lowercase(&self) -> Self
pub const fn eq_ignore_ascii_case(&self, other: &Self) -> bool
pub const fn make_ascii_uppercase(&mut self)
pub const fn make_ascii_lowercase(&mut self)
pub const fn is_ascii(&self) -> bool // Returns `true`
pub const fn is_ascii_alphabetic(&self) -> bool
pub const fn is_ascii_uppercase(&self) -> bool
pub const fn is_ascii_lowercase(&self) -> bool
pub const fn is_ascii_alphanumeric(&self) -> bool
pub const fn is_ascii_digit(&self) -> bool
pub const fn is_ascii_octdigit(&self) -> bool
pub const fn is_ascii_hexdigit(&self) -> bool
pub const fn is_ascii_punctuation(&self) -> bool
pub const fn is_ascii_graphic(&self) -> bool
pub const fn is_ascii_whitespace(&self) -> bool
pub const fn is_ascii_control(&self) -> bool
pub fn escape_ascii(self) -> super::EscapeDefault

Alternatives

The following alternatives are possible. I don't feel strongly one way or the other:

  • &self arguments could be changed to self.
  • The is_ascii method can be deleted.
  • ascii could be removed from the method names.

I used &self for consistency with char and u8 is_ascii* methods, which take &self. I included fn is_ascii and added the methods with ascii in the names so that a subset of code can be written that works for ascii::Char, u8, and char in macros and so that converting code from u8 to ascii::Char would be easier.

Links and related work

Metadata

Metadata

Assignees

No one assigned

    Labels

    T-libs-apiapi-change-proposalA proposal to add or alter unstable APIs in the standard libraries

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions