mirror of
https://github.com/servo/servo.git
synced 2025-08-05 21:50:18 +01:00
Auto merge of #7776 - frewsxcv:str-join, r=mbrubeck
Avoid allocations when joining strings <!-- Reviewable:start --> [<img src="https://reviewable.io/review_button.png" height=40 alt="Review on Reviewable"/>](https://reviewable.io/reviews/servo/servo/7776) <!-- Reviewable:end -->
This commit is contained in:
commit
4823ec947e
5 changed files with 11 additions and 12 deletions
|
@ -326,8 +326,10 @@ pub unsafe fn c_str_to_string(s: *const c_char) -> String {
|
|||
from_utf8(CStr::from_ptr(s).to_bytes()).unwrap().to_owned()
|
||||
}
|
||||
|
||||
pub fn str_join<T: AsRef<str>>(strs: &[T], join: &str) -> String {
|
||||
strs.iter().fold(String::new(), |mut acc, s| {
|
||||
pub fn str_join<I, T>(strs: I, join: &str) -> String
|
||||
where I: IntoIterator<Item=T>, T: AsRef<str>,
|
||||
{
|
||||
strs.into_iter().fold(String::new(), |mut acc, s| {
|
||||
if !acc.is_empty() { acc.push_str(join); }
|
||||
acc.push_str(s.as_ref());
|
||||
acc
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue