diff options
| author | Dylan DPC <99973273+Dylan-DPC@users.noreply.github.com> | 2023-01-17 20:33:04 +0530 |
|---|---|---|
| committer | GitHub <noreply@github.com> | 2023-01-17 20:33:04 +0530 |
| commit | 1b2d595c142d986598913e4d9148b9bf0bf4bfa5 (patch) | |
| tree | f2796481a7a175422013aa2a336a82f50893993a | |
| parent | f91f369949588d74e35fabbf775db3757d5b206d (diff) | |
| parent | 8dbc878a350c51f4e6c75fab3cdec45008a92f9e (diff) | |
| download | rust-1b2d595c142d986598913e4d9148b9bf0bf4bfa5.tar.gz rust-1b2d595c142d986598913e4d9148b9bf0bf4bfa5.zip | |
Rollup merge of #106922 - ChayimFriedman2:patch-5, r=workingjubilee
Avoid unsafe code in `to_ascii_[lower/upper]case()`
| -rw-r--r-- | library/alloc/src/str.rs | 14 |
1 files changed, 6 insertions, 8 deletions
diff --git a/library/alloc/src/str.rs b/library/alloc/src/str.rs index b28d20cda17..afbe5cfaf8e 100644 --- a/library/alloc/src/str.rs +++ b/library/alloc/src/str.rs @@ -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 } } |
