Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Mark make_ascii_uppercase and make_ascii_lowercase in [u8] and str as const. #130738

Merged
merged 1 commit into from
Sep 24, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 2 additions & 0 deletions library/core/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -133,6 +133,7 @@
#![feature(const_ipv4)]
#![feature(const_ipv6)]
#![feature(const_likely)]
#![feature(const_make_ascii)]
#![feature(const_maybe_uninit_assume_init)]
#![feature(const_nonnull_new)]
#![feature(const_num_midpoint)]
Expand All @@ -151,6 +152,7 @@
#![feature(const_slice_from_raw_parts_mut)]
#![feature(const_slice_from_ref)]
#![feature(const_slice_split_at_mut)]
#![feature(const_str_as_mut)]
#![feature(const_str_from_utf8_unchecked_mut)]
#![feature(const_strict_overflow_ops)]
#![feature(const_swap)]
Expand Down
18 changes: 14 additions & 4 deletions library/core/src/slice/ascii.rs
Original file line number Diff line number Diff line change
Expand Up @@ -67,10 +67,15 @@ impl [u8] {
///
/// [`to_ascii_uppercase`]: #method.to_ascii_uppercase
#[stable(feature = "ascii_methods_on_intrinsics", since = "1.23.0")]
#[rustc_const_unstable(feature = "const_make_ascii", issue = "130698")]
#[inline]
pub fn make_ascii_uppercase(&mut self) {
for byte in self {
pub const fn make_ascii_uppercase(&mut self) {
// FIXME(const-hack): We would like to simply iterate using `for` loops but this isn't currently allowed in constant expressions.
let mut i = 0;
while i < self.len() {
let byte = &mut self[i];
byte.make_ascii_uppercase();
i += 1;
}
}

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

Expand Down
6 changes: 4 additions & 2 deletions library/core/src/str/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2473,8 +2473,9 @@ impl str {
/// assert_eq!("GRüßE, JüRGEN ❤", s);
/// ```
#[stable(feature = "ascii_methods_on_intrinsics", since = "1.23.0")]
#[rustc_const_unstable(feature = "const_make_ascii", issue = "130698")]
#[inline]
pub fn make_ascii_uppercase(&mut self) {
pub const fn make_ascii_uppercase(&mut self) {
// SAFETY: changing ASCII letters only does not invalidate UTF-8.
let me = unsafe { self.as_bytes_mut() };
me.make_ascii_uppercase()
Expand All @@ -2500,8 +2501,9 @@ impl str {
/// assert_eq!("grÜße, jÜrgen ❤", s);
/// ```
#[stable(feature = "ascii_methods_on_intrinsics", since = "1.23.0")]
#[rustc_const_unstable(feature = "const_make_ascii", issue = "130698")]
#[inline]
pub fn make_ascii_lowercase(&mut self) {
pub const fn make_ascii_lowercase(&mut self) {
// SAFETY: changing ASCII letters only does not invalidate UTF-8.
let me = unsafe { self.as_bytes_mut() };
me.make_ascii_lowercase()
Expand Down
Loading