Skip to content

Commit f891af9

Browse files
authored
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 #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.``
2 parents c562913 + 1ed9dd4 commit f891af9

File tree

2 files changed

+18
-10
lines changed

2 files changed

+18
-10
lines changed

library/core/src/char/methods.rs

+11-6
Original file line numberDiff line numberDiff line change
@@ -569,8 +569,9 @@ impl char {
569569
/// assert_eq!(len, tokyo.len());
570570
/// ```
571571
#[stable(feature = "rust1", since = "1.0.0")]
572+
#[rustc_const_stable(feature = "const_char_len_utf", since = "1.52.0")]
572573
#[inline]
573-
pub fn len_utf8(self) -> usize {
574+
pub const fn len_utf8(self) -> usize {
574575
len_utf8(self as u32)
575576
}
576577

@@ -594,8 +595,9 @@ impl char {
594595
/// assert_eq!(len, 2);
595596
/// ```
596597
#[stable(feature = "rust1", since = "1.0.0")]
598+
#[rustc_const_stable(feature = "const_char_len_utf", since = "1.52.0")]
597599
#[inline]
598-
pub fn len_utf16(self) -> usize {
600+
pub const fn len_utf16(self) -> usize {
599601
let ch = self as u32;
600602
if (ch & 0xFFFF) == ch { 1 } else { 2 }
601603
}
@@ -1086,8 +1088,9 @@ impl char {
10861088
/// [`make_ascii_uppercase()`]: #method.make_ascii_uppercase
10871089
/// [`to_uppercase()`]: #method.to_uppercase
10881090
#[stable(feature = "ascii_methods_on_intrinsics", since = "1.23.0")]
1091+
#[rustc_const_stable(feature = "const_ascii_methods_on_intrinsics", since = "1.52.0")]
10891092
#[inline]
1090-
pub fn to_ascii_uppercase(&self) -> char {
1093+
pub const fn to_ascii_uppercase(&self) -> char {
10911094
if self.is_ascii_lowercase() {
10921095
(*self as u8).ascii_change_case_unchecked() as char
10931096
} else {
@@ -1118,8 +1121,9 @@ impl char {
11181121
/// [`make_ascii_lowercase()`]: #method.make_ascii_lowercase
11191122
/// [`to_lowercase()`]: #method.to_lowercase
11201123
#[stable(feature = "ascii_methods_on_intrinsics", since = "1.23.0")]
1124+
#[rustc_const_stable(feature = "const_ascii_methods_on_intrinsics", since = "1.52.0")]
11211125
#[inline]
1122-
pub fn to_ascii_lowercase(&self) -> char {
1126+
pub const fn to_ascii_lowercase(&self) -> char {
11231127
if self.is_ascii_uppercase() {
11241128
(*self as u8).ascii_change_case_unchecked() as char
11251129
} else {
@@ -1143,8 +1147,9 @@ impl char {
11431147
/// assert!(!upper_a.eq_ignore_ascii_case(&lower_z));
11441148
/// ```
11451149
#[stable(feature = "ascii_methods_on_intrinsics", since = "1.23.0")]
1150+
#[rustc_const_stable(feature = "const_ascii_methods_on_intrinsics", since = "1.52.0")]
11461151
#[inline]
1147-
pub fn eq_ignore_ascii_case(&self, other: &char) -> bool {
1152+
pub const fn eq_ignore_ascii_case(&self, other: &char) -> bool {
11481153
self.to_ascii_lowercase() == other.to_ascii_lowercase()
11491154
}
11501155

@@ -1561,7 +1566,7 @@ impl char {
15611566
}
15621567

15631568
#[inline]
1564-
fn len_utf8(code: u32) -> usize {
1569+
const fn len_utf8(code: u32) -> usize {
15651570
if code < MAX_ONE_B {
15661571
1
15671572
} else if code < MAX_TWO_B {

library/core/src/num/mod.rs

+7-4
Original file line numberDiff line numberDiff line change
@@ -195,8 +195,9 @@ impl u8 {
195195
///
196196
/// [`make_ascii_uppercase`]: Self::make_ascii_uppercase
197197
#[stable(feature = "ascii_methods_on_intrinsics", since = "1.23.0")]
198+
#[rustc_const_stable(feature = "const_ascii_methods_on_intrinsics", since = "1.52.0")]
198199
#[inline]
199-
pub fn to_ascii_uppercase(&self) -> u8 {
200+
pub const fn to_ascii_uppercase(&self) -> u8 {
200201
// Unset the fifth bit if this is a lowercase letter
201202
*self & !((self.is_ascii_lowercase() as u8) * ASCII_CASE_MASK)
202203
}
@@ -218,15 +219,16 @@ impl u8 {
218219
///
219220
/// [`make_ascii_lowercase`]: Self::make_ascii_lowercase
220221
#[stable(feature = "ascii_methods_on_intrinsics", since = "1.23.0")]
222+
#[rustc_const_stable(feature = "const_ascii_methods_on_intrinsics", since = "1.52.0")]
221223
#[inline]
222-
pub fn to_ascii_lowercase(&self) -> u8 {
224+
pub const fn to_ascii_lowercase(&self) -> u8 {
223225
// Set the fifth bit if this is an uppercase letter
224226
*self | (self.is_ascii_uppercase() as u8 * ASCII_CASE_MASK)
225227
}
226228

227229
/// Assumes self is ascii
228230
#[inline]
229-
pub(crate) fn ascii_change_case_unchecked(&self) -> u8 {
231+
pub(crate) const fn ascii_change_case_unchecked(&self) -> u8 {
230232
*self ^ ASCII_CASE_MASK
231233
}
232234

@@ -243,8 +245,9 @@ impl u8 {
243245
/// assert!(lowercase_a.eq_ignore_ascii_case(&uppercase_a));
244246
/// ```
245247
#[stable(feature = "ascii_methods_on_intrinsics", since = "1.23.0")]
248+
#[rustc_const_stable(feature = "const_ascii_methods_on_intrinsics", since = "1.52.0")]
246249
#[inline]
247-
pub fn eq_ignore_ascii_case(&self, other: &u8) -> bool {
250+
pub const fn eq_ignore_ascii_case(&self, other: &u8) -> bool {
248251
self.to_ascii_lowercase() == other.to_ascii_lowercase()
249252
}
250253

0 commit comments

Comments
 (0)