Skip to content

Commit 194bbc7

Browse files
committed
Auto merge of rust-lang#130738 - bjoernager:const-make-ascii, r=jhpratt
Mark `make_ascii_uppercase` and `make_ascii_lowercase` in `[u8]` and `str` as const. Relevant tracking issue: rust-lang#130698 This PR extends rust-lang#130697 and rust-lang#130713 to the similar methods in byte slices (`[u8]`) and string slices (`str`). For the `str` methods, this simply requires adding the `const` specifier to the function signatures. The `[u8]` methods, however, require (at least a temporary) reimplementation due to the use of iterators and `for` loops.
2 parents 668b21a + 622d08c commit 194bbc7

File tree

3 files changed

+20
-6
lines changed

3 files changed

+20
-6
lines changed

core/src/lib.rs

+2
Original file line numberDiff line numberDiff line change
@@ -132,6 +132,7 @@
132132
#![feature(const_ipv4)]
133133
#![feature(const_ipv6)]
134134
#![feature(const_likely)]
135+
#![feature(const_make_ascii)]
135136
#![feature(const_maybe_uninit_assume_init)]
136137
#![feature(const_nonnull_new)]
137138
#![feature(const_num_midpoint)]
@@ -150,6 +151,7 @@
150151
#![feature(const_slice_from_raw_parts_mut)]
151152
#![feature(const_slice_from_ref)]
152153
#![feature(const_slice_split_at_mut)]
154+
#![feature(const_str_as_mut)]
153155
#![feature(const_str_from_utf8_unchecked_mut)]
154156
#![feature(const_strict_overflow_ops)]
155157
#![feature(const_swap)]

core/src/slice/ascii.rs

+14-4
Original file line numberDiff line numberDiff line change
@@ -67,10 +67,15 @@ impl [u8] {
6767
///
6868
/// [`to_ascii_uppercase`]: #method.to_ascii_uppercase
6969
#[stable(feature = "ascii_methods_on_intrinsics", since = "1.23.0")]
70+
#[rustc_const_unstable(feature = "const_make_ascii", issue = "130698")]
7071
#[inline]
71-
pub fn make_ascii_uppercase(&mut self) {
72-
for byte in self {
72+
pub const fn make_ascii_uppercase(&mut self) {
73+
// FIXME(const-hack): We would like to simply iterate using `for` loops but this isn't currently allowed in constant expressions.
74+
let mut i = 0;
75+
while i < self.len() {
76+
let byte = &mut self[i];
7377
byte.make_ascii_uppercase();
78+
i += 1;
7479
}
7580
}
7681

@@ -84,10 +89,15 @@ impl [u8] {
8489
///
8590
/// [`to_ascii_lowercase`]: #method.to_ascii_lowercase
8691
#[stable(feature = "ascii_methods_on_intrinsics", since = "1.23.0")]
92+
#[rustc_const_unstable(feature = "const_make_ascii", issue = "130698")]
8793
#[inline]
88-
pub fn make_ascii_lowercase(&mut self) {
89-
for byte in self {
94+
pub const fn make_ascii_lowercase(&mut self) {
95+
// FIXME(const-hack): We would like to simply iterate using `for` loops but this isn't currently allowed in constant expressions.
96+
let mut i = 0;
97+
while i < self.len() {
98+
let byte = &mut self[i];
9099
byte.make_ascii_lowercase();
100+
i += 1;
91101
}
92102
}
93103

core/src/str/mod.rs

+4-2
Original file line numberDiff line numberDiff line change
@@ -2473,8 +2473,9 @@ impl str {
24732473
/// assert_eq!("GRüßE, JüRGEN ❤", s);
24742474
/// ```
24752475
#[stable(feature = "ascii_methods_on_intrinsics", since = "1.23.0")]
2476+
#[rustc_const_unstable(feature = "const_make_ascii", issue = "130698")]
24762477
#[inline]
2477-
pub fn make_ascii_uppercase(&mut self) {
2478+
pub const fn make_ascii_uppercase(&mut self) {
24782479
// SAFETY: changing ASCII letters only does not invalidate UTF-8.
24792480
let me = unsafe { self.as_bytes_mut() };
24802481
me.make_ascii_uppercase()
@@ -2500,8 +2501,9 @@ impl str {
25002501
/// assert_eq!("grÜße, jÜrgen ❤", s);
25012502
/// ```
25022503
#[stable(feature = "ascii_methods_on_intrinsics", since = "1.23.0")]
2504+
#[rustc_const_unstable(feature = "const_make_ascii", issue = "130698")]
25032505
#[inline]
2504-
pub fn make_ascii_lowercase(&mut self) {
2506+
pub const fn make_ascii_lowercase(&mut self) {
25052507
// SAFETY: changing ASCII letters only does not invalidate UTF-8.
25062508
let me = unsafe { self.as_bytes_mut() };
25072509
me.make_ascii_lowercase()

0 commit comments

Comments
 (0)