Rollup merge of #82078 - lopopolo:char-u8-const-fn, r=m-ou-se
Make char and u8 methods const char methods `len_utf8`, `len_utf16`, `to_ascii_lowercase`, `eq_ignore_ascii_case` can be made const. `u8` methods `to_ascii_lowercase`, `to_ascii_uppercase` are required to be const as well. `u8::eq_ignore_ascii_case` was additionally made const. Rebase of https://github.com/rust-lang/rust/pull/79549 originally authored by ``@YenForYang.`` Changes from that PR: - Squashed all commits from #79549. - rebased to latest upstream master. - Removed const attributes for `char::escape_unicode` and `char::escape_default`. - Updated `since` attributes for `const` stabilization to 1.52.0. cc ``@m-ou-se.``
This commit is contained in:
commit
f891af9de5
@ -569,8 +569,9 @@ impl char {
|
|||||||
/// assert_eq!(len, tokyo.len());
|
/// assert_eq!(len, tokyo.len());
|
||||||
/// ```
|
/// ```
|
||||||
#[stable(feature = "rust1", since = "1.0.0")]
|
#[stable(feature = "rust1", since = "1.0.0")]
|
||||||
|
#[rustc_const_stable(feature = "const_char_len_utf", since = "1.52.0")]
|
||||||
#[inline]
|
#[inline]
|
||||||
pub fn len_utf8(self) -> usize {
|
pub const fn len_utf8(self) -> usize {
|
||||||
len_utf8(self as u32)
|
len_utf8(self as u32)
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -594,8 +595,9 @@ impl char {
|
|||||||
/// assert_eq!(len, 2);
|
/// assert_eq!(len, 2);
|
||||||
/// ```
|
/// ```
|
||||||
#[stable(feature = "rust1", since = "1.0.0")]
|
#[stable(feature = "rust1", since = "1.0.0")]
|
||||||
|
#[rustc_const_stable(feature = "const_char_len_utf", since = "1.52.0")]
|
||||||
#[inline]
|
#[inline]
|
||||||
pub fn len_utf16(self) -> usize {
|
pub const fn len_utf16(self) -> usize {
|
||||||
let ch = self as u32;
|
let ch = self as u32;
|
||||||
if (ch & 0xFFFF) == ch { 1 } else { 2 }
|
if (ch & 0xFFFF) == ch { 1 } else { 2 }
|
||||||
}
|
}
|
||||||
@ -1086,8 +1088,9 @@ impl char {
|
|||||||
/// [`make_ascii_uppercase()`]: #method.make_ascii_uppercase
|
/// [`make_ascii_uppercase()`]: #method.make_ascii_uppercase
|
||||||
/// [`to_uppercase()`]: #method.to_uppercase
|
/// [`to_uppercase()`]: #method.to_uppercase
|
||||||
#[stable(feature = "ascii_methods_on_intrinsics", since = "1.23.0")]
|
#[stable(feature = "ascii_methods_on_intrinsics", since = "1.23.0")]
|
||||||
|
#[rustc_const_stable(feature = "const_ascii_methods_on_intrinsics", since = "1.52.0")]
|
||||||
#[inline]
|
#[inline]
|
||||||
pub fn to_ascii_uppercase(&self) -> char {
|
pub const fn to_ascii_uppercase(&self) -> char {
|
||||||
if self.is_ascii_lowercase() {
|
if self.is_ascii_lowercase() {
|
||||||
(*self as u8).ascii_change_case_unchecked() as char
|
(*self as u8).ascii_change_case_unchecked() as char
|
||||||
} else {
|
} else {
|
||||||
@ -1118,8 +1121,9 @@ impl char {
|
|||||||
/// [`make_ascii_lowercase()`]: #method.make_ascii_lowercase
|
/// [`make_ascii_lowercase()`]: #method.make_ascii_lowercase
|
||||||
/// [`to_lowercase()`]: #method.to_lowercase
|
/// [`to_lowercase()`]: #method.to_lowercase
|
||||||
#[stable(feature = "ascii_methods_on_intrinsics", since = "1.23.0")]
|
#[stable(feature = "ascii_methods_on_intrinsics", since = "1.23.0")]
|
||||||
|
#[rustc_const_stable(feature = "const_ascii_methods_on_intrinsics", since = "1.52.0")]
|
||||||
#[inline]
|
#[inline]
|
||||||
pub fn to_ascii_lowercase(&self) -> char {
|
pub const fn to_ascii_lowercase(&self) -> char {
|
||||||
if self.is_ascii_uppercase() {
|
if self.is_ascii_uppercase() {
|
||||||
(*self as u8).ascii_change_case_unchecked() as char
|
(*self as u8).ascii_change_case_unchecked() as char
|
||||||
} else {
|
} else {
|
||||||
@ -1143,8 +1147,9 @@ impl char {
|
|||||||
/// assert!(!upper_a.eq_ignore_ascii_case(&lower_z));
|
/// assert!(!upper_a.eq_ignore_ascii_case(&lower_z));
|
||||||
/// ```
|
/// ```
|
||||||
#[stable(feature = "ascii_methods_on_intrinsics", since = "1.23.0")]
|
#[stable(feature = "ascii_methods_on_intrinsics", since = "1.23.0")]
|
||||||
|
#[rustc_const_stable(feature = "const_ascii_methods_on_intrinsics", since = "1.52.0")]
|
||||||
#[inline]
|
#[inline]
|
||||||
pub fn eq_ignore_ascii_case(&self, other: &char) -> bool {
|
pub const fn eq_ignore_ascii_case(&self, other: &char) -> bool {
|
||||||
self.to_ascii_lowercase() == other.to_ascii_lowercase()
|
self.to_ascii_lowercase() == other.to_ascii_lowercase()
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -1561,7 +1566,7 @@ impl char {
|
|||||||
}
|
}
|
||||||
|
|
||||||
#[inline]
|
#[inline]
|
||||||
fn len_utf8(code: u32) -> usize {
|
const fn len_utf8(code: u32) -> usize {
|
||||||
if code < MAX_ONE_B {
|
if code < MAX_ONE_B {
|
||||||
1
|
1
|
||||||
} else if code < MAX_TWO_B {
|
} else if code < MAX_TWO_B {
|
||||||
|
@ -195,8 +195,9 @@ impl u8 {
|
|||||||
///
|
///
|
||||||
/// [`make_ascii_uppercase`]: Self::make_ascii_uppercase
|
/// [`make_ascii_uppercase`]: Self::make_ascii_uppercase
|
||||||
#[stable(feature = "ascii_methods_on_intrinsics", since = "1.23.0")]
|
#[stable(feature = "ascii_methods_on_intrinsics", since = "1.23.0")]
|
||||||
|
#[rustc_const_stable(feature = "const_ascii_methods_on_intrinsics", since = "1.52.0")]
|
||||||
#[inline]
|
#[inline]
|
||||||
pub fn to_ascii_uppercase(&self) -> u8 {
|
pub const fn to_ascii_uppercase(&self) -> u8 {
|
||||||
// Unset the fifth bit if this is a lowercase letter
|
// Unset the fifth bit if this is a lowercase letter
|
||||||
*self & !((self.is_ascii_lowercase() as u8) * ASCII_CASE_MASK)
|
*self & !((self.is_ascii_lowercase() as u8) * ASCII_CASE_MASK)
|
||||||
}
|
}
|
||||||
@ -218,15 +219,16 @@ impl u8 {
|
|||||||
///
|
///
|
||||||
/// [`make_ascii_lowercase`]: Self::make_ascii_lowercase
|
/// [`make_ascii_lowercase`]: Self::make_ascii_lowercase
|
||||||
#[stable(feature = "ascii_methods_on_intrinsics", since = "1.23.0")]
|
#[stable(feature = "ascii_methods_on_intrinsics", since = "1.23.0")]
|
||||||
|
#[rustc_const_stable(feature = "const_ascii_methods_on_intrinsics", since = "1.52.0")]
|
||||||
#[inline]
|
#[inline]
|
||||||
pub fn to_ascii_lowercase(&self) -> u8 {
|
pub const fn to_ascii_lowercase(&self) -> u8 {
|
||||||
// Set the fifth bit if this is an uppercase letter
|
// Set the fifth bit if this is an uppercase letter
|
||||||
*self | (self.is_ascii_uppercase() as u8 * ASCII_CASE_MASK)
|
*self | (self.is_ascii_uppercase() as u8 * ASCII_CASE_MASK)
|
||||||
}
|
}
|
||||||
|
|
||||||
/// Assumes self is ascii
|
/// Assumes self is ascii
|
||||||
#[inline]
|
#[inline]
|
||||||
pub(crate) fn ascii_change_case_unchecked(&self) -> u8 {
|
pub(crate) const fn ascii_change_case_unchecked(&self) -> u8 {
|
||||||
*self ^ ASCII_CASE_MASK
|
*self ^ ASCII_CASE_MASK
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -243,8 +245,9 @@ impl u8 {
|
|||||||
/// assert!(lowercase_a.eq_ignore_ascii_case(&uppercase_a));
|
/// assert!(lowercase_a.eq_ignore_ascii_case(&uppercase_a));
|
||||||
/// ```
|
/// ```
|
||||||
#[stable(feature = "ascii_methods_on_intrinsics", since = "1.23.0")]
|
#[stable(feature = "ascii_methods_on_intrinsics", since = "1.23.0")]
|
||||||
|
#[rustc_const_stable(feature = "const_ascii_methods_on_intrinsics", since = "1.52.0")]
|
||||||
#[inline]
|
#[inline]
|
||||||
pub fn eq_ignore_ascii_case(&self, other: &u8) -> bool {
|
pub const fn eq_ignore_ascii_case(&self, other: &u8) -> bool {
|
||||||
self.to_ascii_lowercase() == other.to_ascii_lowercase()
|
self.to_ascii_lowercase() == other.to_ascii_lowercase()
|
||||||
}
|
}
|
||||||
|
|
||||||
|
Loading…
x
Reference in New Issue
Block a user