Avoid unsafe code in to_ascii_[lower/upper]case()
This commit is contained in:
parent
9e75dddf60
commit
8dbc878a35
@ -559,10 +559,9 @@ impl str {
|
|||||||
#[stable(feature = "ascii_methods_on_intrinsics", since = "1.23.0")]
|
#[stable(feature = "ascii_methods_on_intrinsics", since = "1.23.0")]
|
||||||
#[inline]
|
#[inline]
|
||||||
pub fn to_ascii_uppercase(&self) -> String {
|
pub fn to_ascii_uppercase(&self) -> String {
|
||||||
let mut bytes = self.as_bytes().to_vec();
|
let mut s = self.to_owned();
|
||||||
bytes.make_ascii_uppercase();
|
s.make_ascii_uppercase();
|
||||||
// make_ascii_uppercase() preserves the UTF-8 invariant.
|
s
|
||||||
unsafe { String::from_utf8_unchecked(bytes) }
|
|
||||||
}
|
}
|
||||||
|
|
||||||
/// Returns a copy of this string where each character is mapped to its
|
/// 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")]
|
#[stable(feature = "ascii_methods_on_intrinsics", since = "1.23.0")]
|
||||||
#[inline]
|
#[inline]
|
||||||
pub fn to_ascii_lowercase(&self) -> String {
|
pub fn to_ascii_lowercase(&self) -> String {
|
||||||
let mut bytes = self.as_bytes().to_vec();
|
let mut s = self.to_owned();
|
||||||
bytes.make_ascii_lowercase();
|
s.make_ascii_lowercase();
|
||||||
// make_ascii_lowercase() preserves the UTF-8 invariant.
|
s
|
||||||
unsafe { String::from_utf8_unchecked(bytes) }
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
Loading…
x
Reference in New Issue
Block a user