Optimize Str::rev

This commit is contained in:
Laurenz 2023-10-09 15:15:47 +02:00
parent 2a19e7f4dc
commit df49d3f0c6

View File

@ -591,7 +591,11 @@ impl Str {
/// Reverse the string.
#[func(title = "Reverse")]
pub fn rev(&self) -> Str {
self.as_str().graphemes(true).rev().collect::<String>().into()
let mut s = EcoString::with_capacity(self.0.len());
for grapheme in self.as_str().graphemes(true).rev() {
s.push_str(grapheme);
}
s.into()
}
}