diff options
author | Anthony Ramine <n.oxyde@gmail.com> | 2016-05-22 13:25:07 +0200 |
---|---|---|
committer | Anthony Ramine <n.oxyde@gmail.com> | 2016-05-24 10:54:57 +0200 |
commit | cdc7bca944b6ffebc59169d42331fffbee7f71ee (patch) | |
tree | b727287bb85a82b9ff16c1eac1737d09749be2f4 /components/script/dom/bindings/str.rs | |
parent | 7b467ee52d59d1d2f078948acc9e4d188eac7338 (diff) | |
download | servo-cdc7bca944b6ffebc59169d42331fffbee7f71ee.tar.gz servo-cdc7bca944b6ffebc59169d42331fffbee7f71ee.zip |
Move DOMString back to script
This entirely removes the 'non-geckolib' feature of the util crate.
Diffstat (limited to 'components/script/dom/bindings/str.rs')
-rw-r--r-- | components/script/dom/bindings/str.rs | 121 |
1 files changed, 120 insertions, 1 deletions
diff --git a/components/script/dom/bindings/str.rs b/components/script/dom/bindings/str.rs index f1de6297ceb..4c5f4b4fd6a 100644 --- a/components/script/dom/bindings/str.rs +++ b/components/script/dom/bindings/str.rs @@ -6,11 +6,14 @@ use std::ascii::AsciiExt; use std::borrow::ToOwned; +use std::fmt; use std::hash::{Hash, Hasher}; use std::mem; use std::ops; +use std::ops::{Deref, DerefMut}; use std::str; -use std::str::FromStr; +use std::str::{Bytes, FromStr}; +use string_cache::Atom; /// Encapsulates the IDL `ByteString` type. #[derive(JSTraceable, Clone, Eq, PartialEq, HeapSizeOf)] @@ -114,3 +117,119 @@ pub fn is_token(s: &[u8]) -> bool { } }) } + +/// A DOMString. +#[derive(Clone, Debug, Eq, Hash, HeapSizeOf, Ord, PartialEq, PartialOrd)] +pub struct DOMString(String); + +impl !Send for DOMString {} + +impl DOMString { + /// Creates a new `DOMString`. + pub fn new() -> DOMString { + DOMString(String::new()) + } + + /// Creates a new `DOMString` from a `String`. + pub fn from_string(s: String) -> DOMString { + DOMString(s) + } + + /// Appends a given string slice onto the end of this String. + pub fn push_str(&mut self, string: &str) { + self.0.push_str(string) + } + + /// Truncates this `DOMString`, removing all contents. + pub fn clear(&mut self) { + self.0.clear() + } + + /// An iterator over the bytes of this `DOMString`. + pub fn bytes(&self) -> Bytes { + self.0.bytes() + } +} + +impl Default for DOMString { + fn default() -> Self { + DOMString(String::new()) + } +} + +impl Deref for DOMString { + type Target = str; + + #[inline] + fn deref(&self) -> &str { + &self.0 + } +} + +impl DerefMut for DOMString { + #[inline] + fn deref_mut(&mut self) -> &mut str { + &mut self.0 + } +} + +impl AsRef<str> for DOMString { + fn as_ref(&self) -> &str { + &self.0 + } +} + +impl fmt::Display for DOMString { + #[inline] + fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result { + fmt::Display::fmt(&**self, f) + } +} + +impl PartialEq<str> for DOMString { + fn eq(&self, other: &str) -> bool { + &**self == other + } +} + +impl<'a> PartialEq<&'a str> for DOMString { + fn eq(&self, other: &&'a str) -> bool { + &**self == *other + } +} + +impl From<String> for DOMString { + fn from(contents: String) -> DOMString { + DOMString(contents) + } +} + +impl<'a> From<&'a str> for DOMString { + fn from(contents: &str) -> DOMString { + DOMString::from(String::from(contents)) + } +} + +impl From<DOMString> for Atom { + fn from(contents: DOMString) -> Atom { + Atom::from(contents.0) + } +} + +impl From<DOMString> for String { + fn from(contents: DOMString) -> String { + contents.0 + } +} + +impl Into<Vec<u8>> for DOMString { + fn into(self) -> Vec<u8> { + self.0.into() + } +} + +impl Extend<char> for DOMString { + fn extend<I>(&mut self, iterable: I) where I: IntoIterator<Item=char> { + self.0.extend(iterable) + } +} |