Auto merge of #130738 - bjoernager:const-make-ascii, r=jhpratt
Mark `make_ascii_uppercase` and `make_ascii_lowercase` in `[u8]` and `str` as const. Relevant tracking issue: #130698 This PR extends #130697 and #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.
This commit is contained in:
commit
11e760b7f4
3 changed files with 20 additions and 6 deletions
|
@ -132,6 +132,7 @@
|
||||||
#![feature(const_ipv4)]
|
#![feature(const_ipv4)]
|
||||||
#![feature(const_ipv6)]
|
#![feature(const_ipv6)]
|
||||||
#![feature(const_likely)]
|
#![feature(const_likely)]
|
||||||
|
#![feature(const_make_ascii)]
|
||||||
#![feature(const_maybe_uninit_assume_init)]
|
#![feature(const_maybe_uninit_assume_init)]
|
||||||
#![feature(const_nonnull_new)]
|
#![feature(const_nonnull_new)]
|
||||||
#![feature(const_num_midpoint)]
|
#![feature(const_num_midpoint)]
|
||||||
|
@ -150,6 +151,7 @@
|
||||||
#![feature(const_slice_from_raw_parts_mut)]
|
#![feature(const_slice_from_raw_parts_mut)]
|
||||||
#![feature(const_slice_from_ref)]
|
#![feature(const_slice_from_ref)]
|
||||||
#![feature(const_slice_split_at_mut)]
|
#![feature(const_slice_split_at_mut)]
|
||||||
|
#![feature(const_str_as_mut)]
|
||||||
#![feature(const_str_from_utf8_unchecked_mut)]
|
#![feature(const_str_from_utf8_unchecked_mut)]
|
||||||
#![feature(const_strict_overflow_ops)]
|
#![feature(const_strict_overflow_ops)]
|
||||||
#![feature(const_swap)]
|
#![feature(const_swap)]
|
||||||
|
|
|
@ -67,10 +67,15 @@ impl [u8] {
|
||||||
///
|
///
|
||||||
/// [`to_ascii_uppercase`]: #method.to_ascii_uppercase
|
/// [`to_ascii_uppercase`]: #method.to_ascii_uppercase
|
||||||
#[stable(feature = "ascii_methods_on_intrinsics", since = "1.23.0")]
|
#[stable(feature = "ascii_methods_on_intrinsics", since = "1.23.0")]
|
||||||
|
#[rustc_const_unstable(feature = "const_make_ascii", issue = "130698")]
|
||||||
#[inline]
|
#[inline]
|
||||||
pub fn make_ascii_uppercase(&mut self) {
|
pub const fn make_ascii_uppercase(&mut self) {
|
||||||
for byte in 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();
|
byte.make_ascii_uppercase();
|
||||||
|
i += 1;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -84,10 +89,15 @@ impl [u8] {
|
||||||
///
|
///
|
||||||
/// [`to_ascii_lowercase`]: #method.to_ascii_lowercase
|
/// [`to_ascii_lowercase`]: #method.to_ascii_lowercase
|
||||||
#[stable(feature = "ascii_methods_on_intrinsics", since = "1.23.0")]
|
#[stable(feature = "ascii_methods_on_intrinsics", since = "1.23.0")]
|
||||||
|
#[rustc_const_unstable(feature = "const_make_ascii", issue = "130698")]
|
||||||
#[inline]
|
#[inline]
|
||||||
pub fn make_ascii_lowercase(&mut self) {
|
pub const fn make_ascii_lowercase(&mut self) {
|
||||||
for byte in 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();
|
byte.make_ascii_lowercase();
|
||||||
|
i += 1;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
|
@ -2473,8 +2473,9 @@ impl str {
|
||||||
/// assert_eq!("GRüßE, JüRGEN ❤", s);
|
/// assert_eq!("GRüßE, JüRGEN ❤", s);
|
||||||
/// ```
|
/// ```
|
||||||
#[stable(feature = "ascii_methods_on_intrinsics", since = "1.23.0")]
|
#[stable(feature = "ascii_methods_on_intrinsics", since = "1.23.0")]
|
||||||
|
#[rustc_const_unstable(feature = "const_make_ascii", issue = "130698")]
|
||||||
#[inline]
|
#[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.
|
// SAFETY: changing ASCII letters only does not invalidate UTF-8.
|
||||||
let me = unsafe { self.as_bytes_mut() };
|
let me = unsafe { self.as_bytes_mut() };
|
||||||
me.make_ascii_uppercase()
|
me.make_ascii_uppercase()
|
||||||
|
@ -2500,8 +2501,9 @@ impl str {
|
||||||
/// assert_eq!("grÜße, jÜrgen ❤", s);
|
/// assert_eq!("grÜße, jÜrgen ❤", s);
|
||||||
/// ```
|
/// ```
|
||||||
#[stable(feature = "ascii_methods_on_intrinsics", since = "1.23.0")]
|
#[stable(feature = "ascii_methods_on_intrinsics", since = "1.23.0")]
|
||||||
|
#[rustc_const_unstable(feature = "const_make_ascii", issue = "130698")]
|
||||||
#[inline]
|
#[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.
|
// SAFETY: changing ASCII letters only does not invalidate UTF-8.
|
||||||
let me = unsafe { self.as_bytes_mut() };
|
let me = unsafe { self.as_bytes_mut() };
|
||||||
me.make_ascii_lowercase()
|
me.make_ascii_lowercase()
|
||||||
|
|
Loading…
Add table
Reference in a new issue