Skip to content

Commit 8dbc878

Browse files
Avoid unsafe code in to_ascii_[lower/upper]case()
1 parent 9e75ddd commit 8dbc878

File tree

1 file changed

+6
-8
lines changed

1 file changed

+6
-8
lines changed

library/alloc/src/str.rs

+6-8
Original file line numberDiff line numberDiff line change
@@ -559,10 +559,9 @@ impl str {
559559
#[stable(feature = "ascii_methods_on_intrinsics", since = "1.23.0")]
560560
#[inline]
561561
pub fn to_ascii_uppercase(&self) -> String {
562-
let mut bytes = self.as_bytes().to_vec();
563-
bytes.make_ascii_uppercase();
564-
// make_ascii_uppercase() preserves the UTF-8 invariant.
565-
unsafe { String::from_utf8_unchecked(bytes) }
562+
let mut s = self.to_owned();
563+
s.make_ascii_uppercase();
564+
s
566565
}
567566

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

0 commit comments

Comments
 (0)