From 9d60333af95adf64f2489e1e51563f0694abdd2a Mon Sep 17 00:00:00 2001 From: Keith Yeung Date: Sat, 21 Oct 2017 05:12:04 -0700 Subject: [PATCH] Implement string stripping utilities from the Infra spec --- components/script/dom/bindings/str.rs | 23 +++++++++++++++++++++++ components/script/lib.rs | 3 +++ 2 files changed, 26 insertions(+) diff --git a/components/script/dom/bindings/str.rs b/components/script/dom/bindings/str.rs index af33f662ff7..929ff6e20d4 100644 --- a/components/script/dom/bindings/str.rs +++ b/components/script/dom/bindings/str.rs @@ -186,6 +186,29 @@ impl DOMString { pub fn bytes(&self) -> Bytes { self.0.bytes() } + + /// Removes newline characters according to . + pub fn strip_newlines(&mut self) { + self.0.retain(|c| c != '\r' && c != '\n'); + } + + /// Removes leading and trailing ASCII whitespaces according to + /// . + pub fn strip_leading_and_trailing_ascii_whitespace(&mut self) { + if self.0.len() == 0 { return; } + + let last_non_whitespace = match self.0.rfind(|ref c| !char::is_ascii_whitespace(c)) { + Some(idx) => idx + 1, + None => { + self.0.clear(); + return; + } + }; + let first_non_whitespace = self.0.find(|ref c| !char::is_ascii_whitespace(c)).unwrap(); + + self.0.truncate(last_non_whitespace); + let _ = self.0.splice(0..first_non_whitespace, ""); + } } impl Borrow for DOMString { diff --git a/components/script/lib.rs b/components/script/lib.rs index 719275dc3ec..e4b588f1acd 100644 --- a/components/script/lib.rs +++ b/components/script/lib.rs @@ -4,11 +4,14 @@ #![cfg_attr(feature = "unstable", feature(core_intrinsics))] #![cfg_attr(feature = "unstable", feature(on_unimplemented))] +#![feature(ascii_ctype)] #![feature(conservative_impl_trait)] #![feature(const_fn)] #![feature(mpsc_select)] #![feature(plugin)] #![feature(proc_macro)] +#![feature(splice)] +#![feature(string_retain)] #![deny(unsafe_code)] #![allow(non_snake_case)]