Bug 1371395 Part 1: Servo: Create a method for cheaply getting an ascii lowercase converted string. draft
authorBrad Werth <bwerth@mozilla.com>
Mon, 07 Aug 2017 10:30:48 -0700
changeset 643313 952691c7aa1bc06c7d067bb3126dd564f2539376
parent 643312 92476b2b156e6031f153e079d1865b7d96f81a1a
child 643314 30402b1a09aefd7a63e6b9cd363fa34416cef42e
push id73063
push userbwerth@mozilla.com
push dateWed, 09 Aug 2017 16:35:11 +0000
bugs1371395
milestone57.0a1
Bug 1371395 Part 1: Servo: Create a 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)
+    }
+}