Avoid unsafe code in to_ascii_[lower/upper]case()

This commit is contained in:
Chayim Refael Friedman 2023-01-16 01:15:06 +02:00 committed by GitHub
parent 9e75dddf60
commit 8dbc878a35
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23

View File

@ -559,10 +559,9 @@ impl str {
#[stable(feature = "ascii_methods_on_intrinsics", since = "1.23.0")]
#[inline]
pub fn to_ascii_uppercase(&self) -> String {
let mut bytes = self.as_bytes().to_vec();
bytes.make_ascii_uppercase();
// make_ascii_uppercase() preserves the UTF-8 invariant.
unsafe { String::from_utf8_unchecked(bytes) }
let mut s = self.to_owned();
s.make_ascii_uppercase();
s
}
/// Returns a copy of this string where each character is mapped to its
@ -592,10 +591,9 @@ impl str {
#[stable(feature = "ascii_methods_on_intrinsics", since = "1.23.0")]
#[inline]
pub fn to_ascii_lowercase(&self) -> String {
let mut bytes = self.as_bytes().to_vec();
bytes.make_ascii_lowercase();
// make_ascii_lowercase() preserves the UTF-8 invariant.
unsafe { String::from_utf8_unchecked(bytes) }
let mut s = self.to_owned();
s.make_ascii_lowercase();
s
}
}