Skip to content

Commit bcb1f06

Browse files
YenForYanglopopolo
authored andcommitted
Make char methods const
`escape_unicode`, `escape_default`, `len_utf8`, `len_utf16`, to_ascii_lowercase`, `eq_ignore_ascii_case` `u8` methods `to_ascii_lowercase`, `to_ascii_uppercase` also must be made const u8 methods made const Update methods.rs Update mod.rs Update methods.rs Fix `since` in rustc_const_stable to next stable Fix `since` in rustc_const_stable to next stable Update methods.rs Update mod.rs
1 parent 446d453 commit bcb1f06

File tree

2 files changed

+21
-11
lines changed

2 files changed

+21
-11
lines changed

library/core/src/char/methods.rs

Lines changed: 15 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -384,8 +384,9 @@ impl char {
384384
/// assert_eq!('❤'.escape_unicode().to_string(), "\\u{2764}");
385385
/// ```
386386
#[stable(feature = "rust1", since = "1.0.0")]
387+
#[rustc_const_stable(feature = "const_char_escape_unicode", since = "1.50.0")]
387388
#[inline]
388-
pub fn escape_unicode(self) -> EscapeUnicode {
389+
pub const fn escape_unicode(self) -> EscapeUnicode {
389390
let c = self as u32;
390391

391392
// or-ing 1 ensures that for c==0 the code computes that one
@@ -510,8 +511,9 @@ impl char {
510511
/// assert_eq!('"'.escape_default().to_string(), "\\\"");
511512
/// ```
512513
#[stable(feature = "rust1", since = "1.0.0")]
514+
#[rustc_const_stable(feature = "const_char_escape_default", since = "1.50.0")]
513515
#[inline]
514-
pub fn escape_default(self) -> EscapeDefault {
516+
pub const fn escape_default(self) -> EscapeDefault {
515517
let init_state = match self {
516518
'\t' => EscapeDefaultState::Backslash('t'),
517519
'\r' => EscapeDefaultState::Backslash('r'),
@@ -569,8 +571,9 @@ impl char {
569571
/// assert_eq!(len, tokyo.len());
570572
/// ```
571573
#[stable(feature = "rust1", since = "1.0.0")]
574+
#[rustc_const_stable(feature = "const_char_len_utf", since = "1.50.0")]
572575
#[inline]
573-
pub fn len_utf8(self) -> usize {
576+
pub const fn len_utf8(self) -> usize {
574577
len_utf8(self as u32)
575578
}
576579

@@ -594,8 +597,9 @@ impl char {
594597
/// assert_eq!(len, 2);
595598
/// ```
596599
#[stable(feature = "rust1", since = "1.0.0")]
600+
#[rustc_const_stable(feature = "const_char_len_utf", since = "1.50.0")]
597601
#[inline]
598-
pub fn len_utf16(self) -> usize {
602+
pub const fn len_utf16(self) -> usize {
599603
let ch = self as u32;
600604
if (ch & 0xFFFF) == ch { 1 } else { 2 }
601605
}
@@ -1086,8 +1090,9 @@ impl char {
10861090
/// [`make_ascii_uppercase()`]: #method.make_ascii_uppercase
10871091
/// [`to_uppercase()`]: #method.to_uppercase
10881092
#[stable(feature = "ascii_methods_on_intrinsics", since = "1.23.0")]
1093+
#[rustc_const_stable(feature = "const_ascii_methods_on_intrinsics", since = "1.50.0")]
10891094
#[inline]
1090-
pub fn to_ascii_uppercase(&self) -> char {
1095+
pub const fn to_ascii_uppercase(&self) -> char {
10911096
if self.is_ascii_lowercase() {
10921097
(*self as u8).ascii_change_case_unchecked() as char
10931098
} else {
@@ -1118,8 +1123,9 @@ impl char {
11181123
/// [`make_ascii_lowercase()`]: #method.make_ascii_lowercase
11191124
/// [`to_lowercase()`]: #method.to_lowercase
11201125
#[stable(feature = "ascii_methods_on_intrinsics", since = "1.23.0")]
1126+
#[rustc_const_stable(feature = "const_ascii_methods_on_intrinsics", since = "1.50.0")]
11211127
#[inline]
1122-
pub fn to_ascii_lowercase(&self) -> char {
1128+
pub const fn to_ascii_lowercase(&self) -> char {
11231129
if self.is_ascii_uppercase() {
11241130
(*self as u8).ascii_change_case_unchecked() as char
11251131
} else {
@@ -1143,8 +1149,9 @@ impl char {
11431149
/// assert!(!upper_a.eq_ignore_ascii_case(&lower_z));
11441150
/// ```
11451151
#[stable(feature = "ascii_methods_on_intrinsics", since = "1.23.0")]
1152+
#[rustc_const_stable(feature = "const_ascii_methods_on_intrinsics", since = "1.50.0")]
11461153
#[inline]
1147-
pub fn eq_ignore_ascii_case(&self, other: &char) -> bool {
1154+
pub const fn eq_ignore_ascii_case(&self, other: &char) -> bool {
11481155
self.to_ascii_lowercase() == other.to_ascii_lowercase()
11491156
}
11501157

@@ -1561,7 +1568,7 @@ impl char {
15611568
}
15621569

15631570
#[inline]
1564-
fn len_utf8(code: u32) -> usize {
1571+
const fn len_utf8(code: u32) -> usize {
15651572
if code < MAX_ONE_B {
15661573
1
15671574
} else if code < MAX_TWO_B {

library/core/src/num/mod.rs

Lines changed: 6 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -195,8 +195,9 @@ impl u8 {
195195
///
196196
/// [`make_ascii_uppercase`]: #method.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.50.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,8 +219,9 @@ impl u8 {
218219
///
219220
/// [`make_ascii_lowercase`]: #method.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.50.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
}
@@ -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.50.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)