Skip to content

Commit 224d35a

Browse files
committed
change replace_ascii to an unsafe fn
1 parent b43d110 commit 224d35a

File tree

1 file changed

+4
-2
lines changed

1 file changed

+4
-2
lines changed

library/alloc/src/str.rs

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -289,7 +289,9 @@ impl str {
289289
let to_utf8_len = to.as_bytes().len();
290290
// Fast path for ascii
291291
if from_utf8_len == 1 && to_utf8_len == 1 {
292-
return replace_ascii(&self.as_bytes(), from_as_utf8[0], to.as_bytes()[0]);
292+
return unsafe {
293+
replace_ascii(&self.as_bytes(), from_as_utf8[0], to.as_bytes()[0])
294+
};
293295
}
294296
capacity = get_minimum_result_capacity(self.bytes().len(), from_utf8_len, to_utf8_len);
295297
}
@@ -690,7 +692,7 @@ fn convert_while_ascii(b: &[u8], convert: fn(&u8) -> u8) -> Vec<u8> {
690692
#[inline]
691693
#[cfg(not(test))]
692694
#[cfg(not(no_global_oom_handling))]
693-
fn replace_ascii(utf8_bytes: &[u8], from: u8, to: u8) -> String {
695+
unsafe fn replace_ascii(utf8_bytes: &[u8], from: u8, to: u8) -> String {
694696
let result: Vec<u8> = utf8_bytes.iter().map(|b| if *b == from { to } else { *b }).collect();
695697
// SAFETY: We replaced ascii with ascii on valid utf8 strings.
696698
unsafe { String::from_utf8_unchecked(result) }

0 commit comments

Comments
 (0)