Bug 1371395 Part 1: Create a Servo method for cheaply getting an ascii lowercase converted string. draft
authorBrad Werth <bwerth@mozilla.com>
Mon, 07 Aug 2017 10:30:48 -0700
changeset 642158 f87f22fa95ff3d03023ae5674f28edd01dd75419
parent 642157 2aafe0e23cdb23920c34db2392042e815f8504c3
child 642159 a4aa53168984adad624f34bb9135ce2bcbeccb78
push id72664
push userbwerth@mozilla.com
push dateMon, 07 Aug 2017 19:42:56 +0000
bugs1371395
milestone57.0a1
Bug 1371395 Part 1: Create a Servo method for cheaply getting an ascii lowercase converted string. MozReview-Commit-ID: 1Tfrs9PBkhA
servo/components/style/lib.rs
servo/components/style/str.rs
--- a/servo/components/style/lib.rs
+++ b/servo/components/style/lib.rs
@@ -32,16 +32,18 @@
 // code by default.
 //
 // [1] https://github.com/rust-lang/rust/issues/15701#issuecomment-251900615
 //#![deny(unsafe_code)]
 #![allow(unused_unsafe)]
 
 #![recursion_limit = "500"]  // For define_css_keyword_enum! in -moz-appearance
 
+#![feature(exclusive_range_pattern)]  // Useful for some ascii char detection.
+
 extern crate app_units;
 extern crate arrayvec;
 extern crate atomic_refcell;
 extern crate bit_vec;
 #[macro_use]
 extern crate bitflags;
 #[allow(unused_extern_crates)] extern crate byteorder;
 #[cfg(feature = "gecko")] #[macro_use] #[no_link] extern crate cfg_if;
--- a/servo/components/style/str.rs
+++ b/servo/components/style/str.rs
@@ -1,21 +1,23 @@
 /* This Source Code Form is subject to the terms of the Mozilla Public
  * License, v. 2.0. If a copy of the MPL was not distributed with this
  * file, You can obtain one at http://mozilla.org/MPL/2.0/. */
 
 //! String utils for attributes and similar stuff.
 
 #![deny(missing_docs)]
 
+
 use num_traits::ToPrimitive;
 use std::ascii::AsciiExt;
 use std::convert::AsRef;
 use std::iter::{Filter, Peekable};
 use std::str::Split;
+use std::borrow::Cow;
 
 /// A static slice of characters.
 pub type StaticCharVec = &'static [char];
 
 /// A static slice of `str`s.
 pub type StaticStringVec = &'static [&'static str];
 
 /// A "space character" according to:
@@ -146,8 +148,18 @@ pub fn str_join<I, T>(strs: I, join: &st
     })
 }
 
 /// Returns true if a given string has a given prefix with case-insensitive match.
 pub fn starts_with_ignore_ascii_case(string: &str, prefix: &str) -> bool {
     string.len() >= prefix.len() &&
       string.as_bytes()[0..prefix.len()].eq_ignore_ascii_case(prefix.as_bytes())
 }
+
+/// Returns an ascii lowercase version of a string, only allocating if needed.
+pub fn string_as_ascii_lowercase<'a>(input: &'a str) -> Cow<'a, str> {
+    if input.bytes().any(|c| matches!(c, b'A'..b'Z')) {
+        input.to_ascii_lowercase().into()
+    } else {
+        // Already ascii lowercase.
+        Cow::Borrowed(input)
+    }
+}