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 642256 f6cd03c8d42b0cc189a632ef175c6c372c4ab6c0
parent 642157 2aafe0e23cdb23920c34db2392042e815f8504c3
child 642257 a7e85f85dd4767c9eaa46773b95a32d3ac541d54
push id72694
push userbwerth@mozilla.com
push dateTue, 08 Aug 2017 00:28:48 +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/str.rs
--- a/servo/components/style/str.rs
+++ b/servo/components/style/str.rs
@@ -3,16 +3,17 @@
  * 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::borrow::Cow;
 use std::convert::AsRef;
 use std::iter::{Filter, Peekable};
 use std::str::Split;
 
 /// A static slice of characters.
 pub type StaticCharVec = &'static [char];
 
 /// A static slice of `str`s.
@@ -146,8 +147,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)
+    }
+}