Skip to content

Commit

Permalink
Avoid unsafe code in to_ascii_[lower/upper]case()
Browse files Browse the repository at this point in the history
  • Loading branch information
ChayimFriedman2 authored Jan 15, 2023
1 parent 9e75ddd commit 8dbc878
Showing 1 changed file with 6 additions and 8 deletions.
14 changes: 6 additions & 8 deletions library/alloc/src/str.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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
}
}

Expand Down

0 comments on commit 8dbc878

Please sign in to comment.