Optimize str::repeat

This commit is contained in:
Shotaro Yamada 2018-03-02 13:50:59 +09:00
parent a85417f593
commit 08504fbb0b
2 changed files with 35 additions and 3 deletions

View file

@ -124,6 +124,7 @@
#![feature(allocator_internals)]
#![feature(on_unimplemented)]
#![feature(exact_chunks)]
#![feature(pointer_methods)]
#![cfg_attr(not(test), feature(fused, fn_traits, placement_new_protocol, swap_with_slice, i128))]
#![cfg_attr(test, feature(test, box_heap))]

View file

@ -43,6 +43,7 @@ use core::str as core_str;
use core::str::pattern::Pattern;
use core::str::pattern::{Searcher, ReverseSearcher, DoubleEndedSearcher};
use core::mem;
use core::ptr;
use core::iter::FusedIterator;
use std_unicode::str::{UnicodeStr, Utf16Encoder};
@ -2066,9 +2067,39 @@ impl str {
/// ```
#[stable(feature = "repeat_str", since = "1.16.0")]
pub fn repeat(&self, n: usize) -> String {
let mut s = String::with_capacity(self.len() * n);
s.extend((0..n).map(|_| self));
s
if n == 0 {
return String::new();
}
// n = 2^j + k (2^j > k)
// 2^j:
let mut s = Vec::with_capacity(self.len() * n);
s.extend(self.as_bytes());
let mut m = n >> 1;
while m > 0 {
let len = s.len();
unsafe {
ptr::copy_nonoverlapping(s.as_ptr(), (s.as_mut_ptr() as *mut u8).add(len), len);
s.set_len(len * 2);
}
m >>= 1;
}
// k:
let res_len = n * self.len();
if res_len > s.len() {
unsafe {
ptr::copy_nonoverlapping(
s.as_ptr(),
(s.as_mut_ptr() as *mut u8).add(s.len()),
res_len - s.len(),
);
s.set_len(res_len);
}
}
unsafe { String::from_utf8_unchecked(s) }
}
/// Checks if all characters in this string are within the ASCII range.