]> git.lizzy.rs Git - rust.git/commitdiff
Avoid unsafe code in `to_ascii_[lower/upper]case()`
authorChayim Refael Friedman <chayimfr@gmail.com>
Sun, 15 Jan 2023 23:15:06 +0000 (01:15 +0200)
committerGitHub <noreply@github.com>
Sun, 15 Jan 2023 23:15:06 +0000 (01:15 +0200)
library/alloc/src/str.rs

index b28d20cda179ec32054515d56dad13d303e387ee..afbe5cfaf8ef9dbe0cbe87436cc50e249c1d7ffa 100644 (file)
@@ -559,10 +559,9 @@ pub fn repeat(&self, n: usize) -> String {
     #[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 @@ pub fn to_ascii_uppercase(&self) -> String {
     #[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
     }
 }