mirror of
https://github.com/servo/servo.git
synced 2025-08-03 20:50:07 +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
|
@ -15,11 +15,10 @@ use selectors::parser::PseudoElement;
|
||||||
use std::ascii::AsciiExt;
|
use std::ascii::AsciiExt;
|
||||||
use std::borrow::ToOwned;
|
use std::borrow::ToOwned;
|
||||||
use std::cell::Ref;
|
use std::cell::Ref;
|
||||||
use std::slice::SliceConcatExt;
|
|
||||||
use string_cache::Atom;
|
use string_cache::Atom;
|
||||||
use style::properties::PropertyDeclaration;
|
use style::properties::PropertyDeclaration;
|
||||||
use style::properties::{is_supported_property, longhands_from_shorthand, parse_one_declaration};
|
use style::properties::{is_supported_property, longhands_from_shorthand, parse_one_declaration};
|
||||||
use util::str::DOMString;
|
use util::str::{DOMString, str_join};
|
||||||
|
|
||||||
// http://dev.w3.org/csswg/cssom/#the-cssstyledeclaration-interface
|
// http://dev.w3.org/csswg/cssom/#the-cssstyledeclaration-interface
|
||||||
#[dom_struct]
|
#[dom_struct]
|
||||||
|
@ -50,8 +49,8 @@ macro_rules! css_properties(
|
||||||
);
|
);
|
||||||
|
|
||||||
fn serialize_list(list: &[Ref<PropertyDeclaration>]) -> DOMString {
|
fn serialize_list(list: &[Ref<PropertyDeclaration>]) -> DOMString {
|
||||||
let strings: Vec<_> = list.iter().map(|d| d.value()).collect();
|
let str_iter = list.iter().map(|d| d.value());
|
||||||
strings.join(" ")
|
str_join(str_iter, " ")
|
||||||
}
|
}
|
||||||
|
|
||||||
impl CSSStyleDeclaration {
|
impl CSSStyleDeclaration {
|
||||||
|
|
|
@ -98,7 +98,7 @@ use std::sync::mpsc::channel;
|
||||||
use string_cache::{Atom, QualName};
|
use string_cache::{Atom, QualName};
|
||||||
use time;
|
use time;
|
||||||
use url::Url;
|
use url::Url;
|
||||||
use util::str::{DOMString, split_html_space_chars};
|
use util::str::{DOMString, split_html_space_chars, str_join};
|
||||||
|
|
||||||
#[derive(JSTraceable, PartialEq, HeapSizeOf)]
|
#[derive(JSTraceable, PartialEq, HeapSizeOf)]
|
||||||
pub enum IsHTMLDocument {
|
pub enum IsHTMLDocument {
|
||||||
|
@ -1443,7 +1443,7 @@ impl DocumentMethods for Document {
|
||||||
Some(ref title) => {
|
Some(ref title) => {
|
||||||
// Steps 3-4.
|
// Steps 3-4.
|
||||||
let value = Node::collect_text_contents(title.r().children());
|
let value = Node::collect_text_contents(title.r().children());
|
||||||
split_html_space_chars(&value).collect::<Vec<_>>().join(" ")
|
str_join(split_html_space_chars(&value), " ")
|
||||||
},
|
},
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
|
@ -18,7 +18,7 @@ use dom::htmlelement::{HTMLElement, HTMLElementTypeId};
|
||||||
use dom::node::{Node, NodeTypeId};
|
use dom::node::{Node, NodeTypeId};
|
||||||
use dom::virtualmethods::VirtualMethods;
|
use dom::virtualmethods::VirtualMethods;
|
||||||
use std::cell::Cell;
|
use std::cell::Cell;
|
||||||
use util::str::{DOMString, split_html_space_chars};
|
use util::str::{DOMString, split_html_space_chars, str_join};
|
||||||
|
|
||||||
#[dom_struct]
|
#[dom_struct]
|
||||||
pub struct HTMLOptionElement {
|
pub struct HTMLOptionElement {
|
||||||
|
@ -93,8 +93,7 @@ impl HTMLOptionElementMethods for HTMLOptionElement {
|
||||||
let node = NodeCast::from_ref(self);
|
let node = NodeCast::from_ref(self);
|
||||||
let mut content = String::new();
|
let mut content = String::new();
|
||||||
collect_text(&node, &mut content);
|
collect_text(&node, &mut content);
|
||||||
let v: Vec<&str> = split_html_space_chars(&content).collect();
|
str_join(split_html_space_chars(&content), " ")
|
||||||
v.join(" ")
|
|
||||||
}
|
}
|
||||||
|
|
||||||
// https://www.whatwg.org/html/#dom-option-text
|
// https://www.whatwg.org/html/#dom-option-text
|
||||||
|
|
|
@ -24,7 +24,6 @@
|
||||||
#![feature(str_utf16)]
|
#![feature(str_utf16)]
|
||||||
#![feature(unicode)]
|
#![feature(unicode)]
|
||||||
#![feature(vec_push_all)]
|
#![feature(vec_push_all)]
|
||||||
#![feature(slice_concat_ext)]
|
|
||||||
|
|
||||||
#![deny(unsafe_code)]
|
#![deny(unsafe_code)]
|
||||||
#![allow(non_snake_case)]
|
#![allow(non_snake_case)]
|
||||||
|
|
|
@ -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()
|
from_utf8(CStr::from_ptr(s).to_bytes()).unwrap().to_owned()
|
||||||
}
|
}
|
||||||
|
|
||||||
pub fn str_join<T: AsRef<str>>(strs: &[T], join: &str) -> String {
|
pub fn str_join<I, T>(strs: I, join: &str) -> String
|
||||||
strs.iter().fold(String::new(), |mut acc, s| {
|
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); }
|
if !acc.is_empty() { acc.push_str(join); }
|
||||||
acc.push_str(s.as_ref());
|
acc.push_str(s.as_ref());
|
||||||
acc
|
acc
|
||||||
|
|
Loading…
Add table
Add a link
Reference in a new issue