Create and utilize utility for joining strs

This commit is contained in:
Corey Farwell 2015-07-08 05:18:07 +09:00
parent c77b5aa8e2
commit 7159fe749f
3 changed files with 12 additions and 12 deletions

View file

@ -14,7 +14,7 @@ use dom::window::Window;
use dom::virtualmethods::vtable_for; use dom::virtualmethods::vtable_for;
use devtools_traits::AttrInfo; use devtools_traits::AttrInfo;
use util::str::{DOMString, parse_unsigned_integer, split_html_space_chars}; use util::str::{DOMString, parse_unsigned_integer, split_html_space_chars, str_join};
use string_cache::{Atom, Namespace}; use string_cache::{Atom, Namespace};
@ -49,11 +49,7 @@ impl AttrValue {
} }
pub fn from_atomic_tokens(atoms: Vec<Atom>) -> AttrValue { pub fn from_atomic_tokens(atoms: Vec<Atom>) -> AttrValue {
let tokens = atoms.iter().fold(String::new(), |mut s, atom| { let tokens = str_join(&atoms, "\x20");
if !s.is_empty() { s.push('\x20'); }
s.push_str(atom);
s
});
AttrValue::TokenList(tokens, atoms) AttrValue::TokenList(tokens, atoms)
} }

View file

@ -13,7 +13,7 @@ use dom::bindings::utils::{Reflector, reflect_dom_object};
use dom::element::{Element, AttributeHandlers}; use dom::element::{Element, AttributeHandlers};
use dom::node::window_from_node; use dom::node::window_from_node;
use util::str::{DOMString, HTML_SPACE_CHARACTERS}; use util::str::{DOMString, HTML_SPACE_CHARACTERS, str_join};
use string_cache::Atom; use string_cache::Atom;
use std::borrow::ToOwned; use std::borrow::ToOwned;
@ -158,10 +158,6 @@ impl<'a> DOMTokenListMethods for &'a DOMTokenList {
// https://dom.spec.whatwg.org/#stringification-behavior // https://dom.spec.whatwg.org/#stringification-behavior
fn Stringifier(self) -> DOMString { fn Stringifier(self) -> DOMString {
let tokenlist = self.element.root().r().get_tokenlist_attribute(&self.local_name); let tokenlist = self.element.root().r().get_tokenlist_attribute(&self.local_name);
tokenlist.iter().fold(String::new(), |mut s, atom| { str_join(&tokenlist, "\x20")
if !s.is_empty() { s.push('\x20'); }
s.push_str(atom);
s
})
} }
} }

View file

@ -327,3 +327,11 @@ impl Deref for LowercaseString {
pub unsafe fn c_str_to_string(s: *const c_char) -> String { 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 {
strs.iter().fold(String::new(), |mut acc, s| {
if !acc.is_empty() { acc.push_str(join); }
acc.push_str(s.as_ref());
acc
})
}